首页 > 解决方案 > 如何在 http 处理程序中运行 x 秒

问题描述

我想运行我的函数InsertRecords30 秒并测试在给定时间内可以插入多少条记录。

如何InsertRecords在 x 秒后停止处理,然后从我的处理程序返回结果?

func benchmarkHandler(w http.ResponseWriter, r *http.Request) {

    counter := InsertRecords()

    w.WriteHeader(200)
    io.WriteString(w, fmt.Sprintf("counter is %d", counter))    
}

func InsertRecords() int {
  counter := 0
  // db code goes here
  return counter
}

标签: go

解决方案


取消和超时通常使用context.Context.

虽然这个简单的示例可以单独使用通道完成,但使用此处的上下文使其更加灵活,并且还可以考虑客户端断开连接。

func benchmarkHandler(w http.ResponseWriter, r *http.Request) {
    ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
    defer cancel()

    counter := InsertRecords(ctx)

    w.WriteHeader(200)
    io.WriteString(w, fmt.Sprintf("counter is %d", counter))
}

func InsertRecords(ctx context.Context) int {
    counter := 0
    done := ctx.Done()
    for {
        select {
        case <-done:
            return counter
        default:
        }

        // db code goes here
        counter++
    }
    return counter
}

这将运行至少 30 秒,返回完整的数据库迭代次数。如果你想确保处理程序总是在 30 秒后立即返回,即使 DB 调用被阻塞,那么你需要将 DB 代码推送到另一个 goroutine 并让它稍后返回。最短的示例是使用与上述类似的模式,但同步访问计数器变量,因为它可以在返回时由 DB 循环写入。

func InsertRecords(ctx context.Context) int {
    counter := int64(0)
    done := ctx.Done()

    go func() {
        for {
            select {
            case <-done:
                return
            default:
            }

            // db code goes here
            atomic.AddInt64(&counter, 1)
        }
    }()

    <-done
    return int(atomic.LoadInt64(&counter))
}

有关生产者和超时的示例,请参阅@JoshuaKolden 的答案,它也可以与现有的请求上下文结合使用。


推荐阅读