douwei7976 2017-03-27 05:44
浏览 56

使用Postgresql进行1.8上下文超时

In the beginning of my controller I create a context with a timeout like this:

ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
defer cancel()

And then I use this context in each database request like this:

QueryRowContext(ctx, query, ...args) // where ctx is context

However, when I load my test program, I notice that some of my requests return error "pq: canceling statement due to user request". And the more requests I make, the more errors I get.

But, if I don't use context with timeout, but rather use just context.Background(), I get no errors regardless of the amount of requests.

Each request takes about 50ms, so timeout cannot occur. I tried context with cancel context.WithCancel(context.Background()) and I also get these errors.

What may be a reason that these errors occur?

// create context to cancel working if 10 seconds passed
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
defer cancel()


// parses queries
var response []byte
request := model.GetProductByBarcodeRequest{}
barcode := r.URL.Query().Get("barcode")
storeID := r.URL.Query().Get("store_id")
transactionType := r.URL.Query().Get("type")


request.Barcode = &barcode
request.StoreID = &storeID
request.Type = &transactionType

// processing
result, err := model.GetProductByBarcode(ctx, request)
response = controller.ToJson(result, err)

// writing response to user
w.Write(response)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。