在进行批量调用时,使用Golang中的HTTP请求的上下文(context)。

huangapple go评论77阅读模式
英文:

Golang context in http request when making bulk calls

问题

我正在寻找理解使用Go标准库进行HTTP调用时应该期望的行为。

我了解如果我这样做:

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)

那个特定的请求将在200毫秒内超时。这很好,我明白了。

我的疑问是,如果我这样做:

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
for i := range 1 to 20 {
  req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
}

所以在后面的例子中,我使用同一个上下文进行了一系列的请求。所有的请求会在200毫秒内超时,还是它们会在我开始遍历后的200毫秒后开始失败?

注意:

FATHER_CONTEXT, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)

defer cancel()
for i := range 1 to 20 {
  ctx, cancel := context.WithTimeout(FATHER_CONTEXT, 200*time.Millisecond)
  req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
} 
英文:

I am looking for to understand the behaviour I should expect when making http calls using go standard library with Context timeout.

I understand that if I do this :

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)

That specific request will TO in 200 milliseconds. That works quite well and I get it.

My doubt is, what happens if I do this:

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
for loop with a range from 1 to 20 {
  req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
}

So in the latter example I am doing a bunch of requests using that very context. Will all requests timeout with 200 milliseconds individually, or will they start to fail after 200 milliseconds have passed from the moment I started ranging over?

Note:

FATHER_CONTEXT, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)

defer cancel()
for loop with a range from 1 to 20 {
  ctx, cancel := context.WithTimeout(FATHER_CONTEXT, 200*time.Millisecond)
  req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
} 

答案1

得分: 1

上下文文档中:

> WithCancel、WithDeadline和WithTimeout函数接受一个上下文(父级)并返回一个派生的上下文(子级)和一个CancelFunc。调用CancelFunc会取消子级及其子级,删除父级对子级的引用,并停止任何关联的计时器。

因此,上下文由子级及其子级共享。所有请求将在200毫秒后超时。

英文:

From Context Documentation

> The WithCancel, WithDeadline, and WithTimeout functions take a Context
> (the parent) and return a derived Context (the child) and a
> CancelFunc. Calling the CancelFunc cancels the child and its children,
> removes the parent's reference to the child, and stops any associated
> timers

So the context is shared by the child and its children. All requests will be timeout after 200 milliseconds.

huangapple
  • 本文由 发表于 2021年9月8日 22:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69105134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定