win7 iis建立网站正邦网站建设 优帮云
win7 iis建立网站,正邦网站建设 优帮云,网页设计代码信件怎么写,网站开发的常用流程Context
上下文context.Context 是用来设置截止时间、同步信号#xff0c;传递请求相关值的结构体。
context.Context 定义了四个需要实现的方法#xff1a;
Deadline: 返回 context.Context 被取消的时间。Done: 返回一个 Channel#xff0c;这个 Channel 会在当前工作完…Context
上下文context.Context 是用来设置截止时间、同步信号传递请求相关值的结构体。
context.Context 定义了四个需要实现的方法
Deadline: 返回 context.Context 被取消的时间。Done: 返回一个 Channel这个 Channel 会在当前工作完成或者上下文被取消后关闭多次调用 Done 方法会返回同一个 Channel。Err: 返回 context.Context 结束的原因它只会在 Done 方法对应的 Channel 关闭时返回非空的值。 如果 context.Context 被取消会返回 Canceled 错误。如果 context.Context 超时会返回 DeadlineExceeded 错误。 Value: 从 context.Context 中获取键对应的值对于同一个上下文来说多次调用 Value 并传入相同的 Key 会返回相同的结果该方法可以用来传递请求特定的数据。
type Context interface {Deadline() (deadline time.Time, ok bool)Done() -chan struct{}Err() errorValue(key any) any
}设计原理
在 Goroutine 构成的树形结构中对信号进行同步以减少计算资源的浪费是 context.Context 的最大作用。Go 服务的每一个请求都是通过单独的 Goroutine 处理的HTTP/RPC 请求的处理器会启动新的 Goroutine 访问数据库和其他服务。
如下图所示图片来自《go设计与实现》我们可能会创建多个 Goroutine 来处理一次请求而 context.Context 的作用是在不同 Goroutine 之间同步请求特定数据、取消信号以及处理请求的截止日期。 下层 goroutine 通过监听上层 goroutine context 的 Done 方法同步取消。
func main() {ctx, cancel : context.WithTimeout(context.Background(), 1*time.Second)defer cancel()go handle(ctx, 500*time.Millisecond)select {case -ctx.Done():fmt.Println(main, ctx.Err())}
}
func handle(ctx context.Context, duration time.Duration) {select {case -ctx.Done():fmt.Println(handle, ctx.Err())case -time.After(duration):fmt.Println(process request with, duration)}
}默认上下文
context 包中有两个默认的 context context.Background、context.TODO这两个方法都会返回预先初始化好的私有变量 background 和 todo它们会在同一个 Go 程序中被复用。
这两个私有变量都是通过 new(emptyCtx) 语句初始化的它们是指向私有结构体 context.emptyCtx 的指针这是最简单、最常用的上下文类型。
type emptyCtx int
var (background new(emptyCtx)todo new(emptyCtx)
)
func Background() Context {return background
}
func TODO() Context {return todo
}
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {return
}
func (*emptyCtx) Done() -chan struct{} {return nil
}
func (*emptyCtx) Err() error {return nil
}
func (*emptyCtx) Value(key any) any {return nil
}context.Background和context.TODO 没有差别只是在使用和语义上不同
context.Background 是上下文的默认值所有其他的上下文都应该从它衍生出来。context.TODO 应该仅在不确定应该使用哪种上下文时使用。
这两个是顶级Context其他内置Context都是由它们派生出来的。
取消
context.WithCancel 函数能够从 context.Context 中衍生出一个新的子上下文并返回用于取消该上下文的函数。一旦我们执行返回的取消函数当前上下文以及它的子上下文都会被取消所有的 Goroutine 都会同步收到这一取消信号。 图片来自《go设计与实现》
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {if parent nil {panic(cannot create context from nil parent)}c : newCancelCtx(parent)propagateCancel(parent, c)return c, func() { c.cancel(true, Canceled) }
}context.newCancelCtx 将传入的上下文包装成私有结构体 context.cancelCtx。context.propagateCancel 会构建父子上下文之间的关联当父上下文被取消时子上下文也会被取消。
重点是 propagateCancel:
func propagateCancel(parent Context, child canceler) {done : parent.Done()if done nil {return // parent is never canceled}select {case -done:// parent is already canceledchild.cancel(false, parent.Err())returndefault:}if p, ok : parentCancelCtx(parent); ok {p.mu.Lock()if p.err ! nil {// parent has already been canceledchild.cancel(false, p.err)} else {if p.children nil {p.children make(map[canceler]struct{})}p.children[child] struct{}{}}p.mu.Unlock()} else {atomic.AddInt32(goroutines, 1)go func() {select {case -parent.Done():child.cancel(false, parent.Err())case -child.Done():}}()}
}propagateCancel 与父上下文相关的三种不同情况
当 parent.Done() nil也就是 parent 不会触发取消事件时当前函数会直接返回。当 child 的继承链包含可以取消的上下文时会判断 parent 是否已经触发了取消信号。 如果已经被取消child 会立刻被取消如果没有被取消child 会被加入 parent 的 children 列表中等待 parent 释放取消信号。children列表是一个map。 当父上下文是开发者自定义的类型、实现了 context.Context 接口并在 Done() 方法中返回了非空的管道时。 运行一个新的 Goroutine 同时监听 parent.Done() 和 child.Done() 两个 Channel。在 parent.Done() 关闭时调用 child.cancel 取消子上下文。
context.cancelCtx 实现了了 context.cancelCtx.cancel该方法会关闭上下文中的 Channel 并向所有的子上下文同步取消信号。
func (c *cancelCtx) cancel(removeFromParent bool, err error) {if err nil {panic(context: internal error: missing cancel error)}c.mu.Lock()if c.err ! nil {c.mu.Unlock()return // already canceled}c.err errd, _ : c.done.Load().(chan struct{})if d nil {c.done.Store(closedchan)} else {close(d)}for child : range c.children {// NOTE: acquiring the childs lock while holding parents lock.child.cancel(false, err)}c.children nilc.mu.Unlock()if removeFromParent {removeChild(c.Context, c)}
}cancel 会关闭上下文中的 Channel同时会遍历children同步取消所有子上下文。如果 removeFromParent 为 true会把上下文从父上下文去除。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/87779.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!