go笔记-理解几种Context
https://golang.org/pkg/context/#pkg-overview
https://blog.csdn.net/yzf279533105/article/details/107290645
https://blog.csdn.net/u013210620/article/details/78596861
https://studygolang.com/articles/18931?fr=sidebar
网络请求超时控制
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
Deadline()返回一个time.Time,是当前 Context 的应该结束的时间,ok 表示是否有 deadline
Done()返回一个struct{}类型的只读 channel
Err()返回 Context 被取消时的错误。如果Done没有关闭,Err()返回nil。若不为nil,返回的理由可能为:1. context被终止了 2.context的deadline到了
Value(key interface{}) 是 Context 自带的 K-V 存储功能
WithCancel
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
WithCancel返回一个新的parent拷贝Done channel。当调用返回的cancel函数
或parent的上下文的Done channel被关闭
时(以先发生的为准),将关闭返回的上下文的Done channel。
取消此上下文将释放与其关联的资源,因此在此上下文中运行的操作完成后,代码应立即调用cancel。
示例
此示例演示了使用可取消的context来防止goroutine泄漏。 在示例函数结束时,由gen方法启动的goroutine将返回而不会泄漏。
package main
import (
"context"
"fmt"
)
func main() {
// gen generates integers in a separate goroutine and
// sends them to the returned channel.
// The callers of gen need to cancel the context once
// they are done consuming generated integers not to leak
// the internal goroutine started by gen.
gen := func(ctx context.Context) <-chan int {
dst := make(chan int)
n := 1
go func() {
for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine
case dst <- n:
n++
}
}
}()
return dst
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel when we are finished consuming integers
for n := range gen(ctx) {
fmt.Println(n)
if n == 5 {
break
}
}
}
WithTimeout
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
WithTimeout内部调用了WithDeadline,返回结果为WithDeadline(parent, time.Now().Add(timeout)).
取消此上下文将释放与之关联的资源,因此在此上下文中运行的操作完成后,代码应立即调用cancel:
示例
此示例传递了带有超时的上下文,以告知阻塞函数在超时结束后应放弃任务。
package main
import (
"context"
"fmt"
"time"
)
const shortDuration = 1 * time.Millisecond
func main() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
defer cancel()
select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}
}
// 执行后1ms立刻输出 "context deadline exceeded"
示例2
package main
import (
"context"
"fmt"
"time"
)
const shortDuration = 500 * time.Millisecond
func work(ctx context.Context, doneChan chan struct{}) {
defer func() {
fmt.Println("work end")
}()
fmt.Println("work start")
time.Sleep(2 * time.Second)
fmt.Println("work finished")
doneChan <- struct{}{}
}
func main() {
doneChan := make(chan struct{}, 1)
dlCtx, cancel := context.WithTimeout(context.Background(), shortDuration)
fmt.Println("main start")
go work(dlCtx, doneChan)
select{
case <- dlCtx.Done(): // 取出值,说明已经结束
fmt.Println("timeout")
cancel()
case <- doneChan:
fmt.Println("finish")
}
fmt.Println("go work end")
time.Sleep(3 * time.Second)
fmt.Println("main end")
}
输出:
main start
work start
timeout
go work end
work finished
work end
main end
WithDeadline
WithDeadline返回parent上下文的副本,并将截止时间调整为不迟于d。 如果parent的deadline早于d,则WithDeadline(parent,d)在语义上等效于parent。 当deadline到期、调用返回的cancel函数、关闭parent上下文的Done channel(以先到者为准)时,将关闭返回的上下文的Done channel。
示例:
package main
import (
"context"
"fmt"
"time"
)
const shortDuration = 1 * time.Millisecond
func main() {
d := time.Now().Add(shortDuration)
ctx, cancel := context.WithDeadline(context.Background(), d)
// Even though ctx will be expired, it is good practice to call its
// cancellation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
defer cancel()
select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}
WithValue
func WithValue(parent Context, key, val interface{}) Context
WithValue返回parent的副本,其中与键关联的值为val。
仅将上下文值用于传递流程和API的请求范围的数据,而不用于将可选参数传递给函数。
提供的key必须具有可比性,并且不能为字符串类型或任何其他内置类型,以避免使用上下文在程序包之间发生冲突。 WithValue的用户应定义自己的key类型。 为了避免在分配给interface{}时进行分配,context key常具有具体的类型struct {}。 或者,导出的context key变量的静态类型应为指针或接口。
package main
import (
"context"
"fmt"
)
func main() {
type favContextKey string
f := func(ctx context.Context, k favContextKey) {
if v := ctx.Value(k); v != nil {
fmt.Println("found value:", v)
return
}
fmt.Println("key not found:", k)
}
k := favContextKey("language")
ctx := context.WithValue(context.Background(), k, "Go")
f(ctx, k)
f(ctx, favContextKey("color"))
}