当前位置: 首页 > news >正文

沧州兼职网站建设网站怎么改域名

沧州兼职网站建设,网站怎么改域名,wordpress说说墙,阿里云虚拟主机安装wordpress作用:用于在go协程中 传递上下文、超时、取消、传值 底层实现:是由互斥锁、channel、map来实现的 互斥锁:保护临界资源 channel: 用于信号通知,比如ctx.Done() map: 保存父ctx下派生的所有子ctx, 父ctx关闭,子ctx都关…

作用:用于在go协程中 传递上下文、超时、取消、传值

底层实现:是由互斥锁、channel、map来实现的
互斥锁:保护临界资源
channel: 用于信号通知,比如ctx.Done()
map: 保存父ctx下派生的所有子ctx, 父ctx关闭,子ctx都关闭

实现的接口

type Context interface {Deadline() (deadline time.Time, ok bool)Done() <-chan struct{}Err() errorValue(key interface{}) interface{}
}

空ctx

type emptyCtx intfunc (*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 interface{}) interface{} {return nil
}

cancel ctx

使用map保存所有子ctx,确保父ctx cancel后,子ctx也cancel

type cancelCtx struct {Contextmu       sync.Mutex            // protects following fieldsdone     chan struct{}         // created lazily, closed by first cancel callchildren map[canceler]struct{} // set to nil by the first cancel callerr      error                 // set to non-nil by the first cancel call
}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) }
}// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {return cancelCtx{Context: parent}
}func propagateCancel(parent Context, child canceler) {fmt.Println("propagateCancel")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{})}// 保存子ctxp.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():}}()}
}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 = errif c.done == nil {c.done = closedchan} else {close(c.done) // 关闭channel, 用于通知ctx.Done()}// 关闭所有子ctxfor child := range c.children {// NOTE: acquiring the child's lock while holding parent's lock.child.cancel(false, err)}c.children = nilc.mu.Unlock()if removeFromParent {removeChild(c.Context, c)}
}

timeout ctx

在cancelctx的基础上实现,只是多了个定时器自动调用cancel

type timerCtx struct {cancelCtxtimer *time.Timer // Under cancelCtx.mu.deadline time.Time
}func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {if parent == nil {panic("cannot create context from nil parent")}if cur, ok := parent.Deadline(); ok && cur.Before(d) {// The current deadline is already sooner than the new one.return WithCancel(parent)}c := &timerCtx{cancelCtx: newCancelCtx(parent),deadline:  d,}propagateCancel(parent, c)dur := time.Until(d)if dur <= 0 {c.cancel(true, DeadlineExceeded) // deadline has already passedreturn c, func() { c.cancel(false, Canceled) }}c.mu.Lock()defer c.mu.Unlock()if c.err == nil {c.timer = time.AfterFunc(dur, func() {c.cancel(true, DeadlineExceeded)})}return c, func() { c.cancel(true, Canceled) }
}

value ctx

type valueCtx struct {Contextkey, val interface{}
}
func WithValue(parent Context, key, val interface{}) Context {if parent == nil {panic("cannot create context from nil parent")}if key == nil {panic("nil key")}//	if !reflectlite.TypeOf(key).Comparable() {//	panic("key is not comparable")//}return &valueCtx{parent, key, val}
}
func (c *valueCtx) Value(key interface{}) interface{} {if c.key == key {return c.val}return c.Context.Value(key)
}
http://www.yayakq.cn/news/368855/

相关文章:

  • 网站推广位怎么设置淘宝网页
  • 网站服务器设置地点icp备案 网站
  • 华艺网络网站开发网络培训视频如何快速完成
  • 2019年开公司做网站可以吗专业做公墓 陵园的网站
  • 亦庄网站建设深圳中装建设公司
  • 百度公司网站制作百度网页游戏大厅
  • 梁山专业网站建设企业门户网站静态模板
  • 海门市住房和城乡建设局网站做自行车车队网站的名字
  • 厦门企业制作网站方案建设网站企业公司
  • 网站建设公司方维做网站前端开发的必备软件
  • 网站建设目标怎么看阿里巴巴logo图片
  • 延边延吉网站建设如何在搜索引擎做网站
  • 手机开网站做网站哪些
  • 网站是如何建立的呢安卓app上架费用
  • 企业网站备案信息查询系统正保建设工程网站
  • 做网站用电脑自带的wordpress插件 一键登录
  • 网站开发要学的课程类似wordpress的程序
  • 怎么做qq钓鱼网站如何做自助搜券网站
  • 可以做点赞的网站网站编程所用的语言有
  • 上海人才网站官网入口做网站什么软件好
  • 网站建设一般做什么wordpress添加视频集
  • 上海知名网站建设公外贸企业网站建设公司
  • 网站模板怎么修改教程wordpress网站换字体
  • 微网站免费制作百度安全中心
  • html5导航网站银川网站建设哪家好叫啥名字
  • 交易所网站建设中国建设银行u盾官方网站
  • 中国建设局网站查询佛山网站建设收费标准
  • 网站建设从零开始 教程企业网站搭建方案
  • 全屏响应式网站你就知道
  • 爱做网站软件有初中生做的网站吗