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

群辉做网站服务器云南微网站制作

群辉做网站服务器,云南微网站制作,新建免费网站,解决wordpress注册作用#xff1a;用于在go协程中 传递上下文、超时、取消、传值 底层实现#xff1a;是由互斥锁、channel、map来实现的 互斥锁#xff1a;保护临界资源 channel: 用于信号通知#xff0c;比如ctx.Done() map: 保存父ctx下派生的所有子ctx, 父ctx关闭#xff0c;子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 childs lock while holding parents 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.pierceye.com/news/853339/

相关文章:

  • 佛山网站如何制作网站建设公司哪家强
  • 韩城市网站建设编程培训机构加盟哪家好
  • 已备案网站更换域名广东工厂网站建设
  • 营销型网站有哪些特点建设官方网站的费用账务处理
  • 区域网站设计WordPress无法发布
  • html网站开发主要涉及哪些技术百度域名的ip
  • 织梦网站数据下载wordpress如何播放百度云视频
  • 建站的费用服务器搭建网站环境
  • 查看公司信息的网站旅游网站效果图
  • 娄底网站制作重庆专题片制作
  • 网站建设佰金手指科杰十七织梦淘客网站
  • 财务系统seo西安
  • 如何做好网站建设的关键重点网站地图那么建设
  • 打开山东城市建设职业学院网站自己网站做优化的有权利卖么
  • 境外电商网站建设sem推广优化
  • 五站合一自建网站制作网站用什么软件有哪些
  • 查法人信息的网站开发公司一季度汇报
  • 国外的购物网站有哪些安徽省住房和城乡建设厅官方网站
  • 网站策划需要什么能力网页游戏平台软件
  • phpmysql网站开发网络结构
  • 微官网和移动网站区别论坛网站建设多少钱
  • 怎么做公司网站优化凡科h5登录入口
  • 做电影网站如何推广方案房产网络平台
  • 站长工具 seo查询python爬数据做网站
  • 网站 底部医院网站建设的要求
  • asp网站静态化seo关键词排名优化软件怎么选
  • wordpress apache版本北京seo招聘
  • 南京玄武网站建设信息服务公司的经营范围有哪些
  • 旅游网站建设与翻译wordpress 显示作者
  • 网站建设与维护报告总结国家外汇管理局网站怎么做报告