合肥网站建设设计,科技网页模板,做面点的网站,wordpress 获取标签id文章目录一、为什么需要context二、context 接口三、Background 方法四、 with 系列函数1、WithCancel 方法2、WithDeadline 方法3、WithTimeout 方法4、WithValue 方法五、使用注意事项一、为什么需要context
在 Go http包的Server中#xff0c;每一个请求在都有一个对应的 …
文章目录一、为什么需要context二、context 接口三、Background 方法四、 with 系列函数1、WithCancel 方法2、WithDeadline 方法3、WithTimeout 方法4、WithValue 方法五、使用注意事项一、为什么需要context
在 Go http包的Server中每一个请求在都有一个对应的 goroutine 去处理。请求处理函数通常会启动额外的 goroutine 用来访问后端服务比如数据库和RPC服务。
用来处理一个请求的 goroutine 通常需要访问一些与请求特定的数据比如终端用户的身份认证信息、验证相关的token、请求的截止时间。 当一个请求被取消或超时时所有用来处理该请求的 goroutine 都应该迅速退出然后系统才能释放这些 goroutine 占用的资源。
二、context 接口
context.Context是一个接口该接口定义了四个需要实现的方法。
语法 type Context interface {Deadline() (deadline time.Time, ok bool)Done() -chan struct{}Err() errorValue(key interface{}) interface{}
}方法解释Deadline()返回当前Context被取消的时间也就是完成工作的截止时间deadlineDone()返回一个Channel这个Channel会在当前工作完成或者上下文被取消之后关闭多次调用Done方法会返回同一个ChannelErr()返回当前Context结束的原因它只会在Done返回的Channel被关闭时才会返回非空的值如果当前Context被取消就会返回Canceled错误如果当前Context超时就会返回DeadlineExceeded错误Value()从Context中返回键对应的值对于同一个上下文来说多次调用Value 并传入相同的Key会返回相同的结果
三、Background 方法
语法: context.Background() Context
分析 Background()主要用于main函数、初始化以及测试代码中作为Context这个树结构的最顶层的Context也就是根Context。
四、 with 系列函数
1、WithCancel 方法
WithCancel返回带有新Done通道的父节点的副本。当调用返回的cancel函数或当关闭父上下文的Done通道时将关闭返回的上下文的Done通道。
语法 func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
2、WithDeadline 方法
返回父上下文的副本并将deadline调整为不迟于d。如果父上下文的deadline已经早于d则WithDeadline(parent, d)在语义上等同于父上下文。当截止日过期时或调用返回的cancel函数时或当父上下文的Done通道关闭时返回上下文的Done通道将被关闭以最先发生的情况为准。
语法 func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
3、WithTimeout 方法
WithTimeout返回WithDeadline(parent, time.Now().Add(timeout))。
语法 func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
4、WithValue 方法
WithValue函数能够将请求作用域的数据与 Context 对象建立关系。WithValue返回父节点的副本其中与key关联的值为val。
语法 func WithValue(parent Context, key, val interface{}) Context
五、使用注意事项
推荐以参数的方式显示传递Context以Context作为参数的函数方法应该把Context作为第一个参数。给一个函数方法传递Context的时候不要传递nil如果不知道传递什么就使用context.Background()Context的Value相关方法应该传递请求域的必要数据不应该用于传递可选参数Context是线程安全的可以放心的在多个goroutine中传递 文章转载自: http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.ljngm.cn.gov.cn.ljngm.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.vtbtje.cn.gov.cn.vtbtje.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn