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

火车站网站建设方案陕西有没有做网站好的公司

火车站网站建设方案,陕西有没有做网站好的公司,网站添加在线支付,找网上公司做网站文章目录 1. 概述1.1 什么是 Context1.2 设计原理1.3 使用场景源码分析核心:Context接口4个实现6个方法TODO 和 BackgroundWithCancelcancelpropagateCancel 绑定父对象WithTimeout 和 WithDeadlineWithValue总结参考1. 概述 基于版本: go1.22.3/src/context/context.go 1.1…

文章目录

  • 1. 概述
    • 1.1 什么是 Context
    • 1.2 设计原理
    • 1.3 使用场景
  • 源码分析
    • 核心:Context接口
    • 4个实现
    • 6个方法
      • TODO 和 Background
      • WithCancel
        • cancel
        • propagateCancel 绑定父对象
      • WithTimeout 和 WithDeadline
      • WithValue
  • 总结
  • 参考

1. 概述

基于版本: go1.22.3/src/context/context.go

1.1 什么是 Context

上下文 context.Context在Go 语言中用来设置截止日期、同步信号,传递请求相关值的结构体。上下文与 Goroutine 有比较密切的关系,是 Go 语言中独特的设计,在其他编程语言中我们很少见到类似的概念。

主要用于超时控制和多Goroutine间的数据传递。

看一下官方定义

Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
Incoming requests to a server should create a Context, and outgoing calls to servers should accept a Context. The chain of function calls between them must propagate the Context, optionally replacing it with a derived Context created using WithCancel, WithDeadline, WithTimeout, or WithValue. When a Context is canceled, all Contexts derived from it are also canceled.

  • Package context defines the Context type:Go语言中的context包定义了一个名为Context的类型。

  • which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes:Context类型用于在API边界之间以及不同进程之间传递诸如截止时间(deadlines)、取消信号(cancellation signals)和其他请求作用域内的值。

  • Incoming requests to a server should create a Context:当服务器接收到一个请求时,应该创建一个Context。

  • and outgoing calls to servers should accept a Context:当服务器向外发起调用时,应该接受一个Context。

  • The chain of function calls between them must propagate the Context:在这些函数调用链中,必须传递Context。

  • optionally replacing it with a derived Context created using WithCancel, WithDeadline, WithTimeout, or WithValue:可以选择用WithCancel、WithDeadline、WithTimeout或WithValue方法创建的派生Context替换原有的Context。

  • When a Context is canceled, all Contexts derived from it are also canceled:当一个Context被取消时,所有从它派生出来的Context也会被取消。

简而言之,context包和Context类型在Go语言中用于控制请求的生命周期,包括传递截止时间、取消信号等信息,并确保这些信息能够在服务器之间的函数调用链中传递。当需要取消请求时,所有相关的Context都会被取消,这样可以优雅地终止请求处理。

1.2 设计原理

因为context.Context主要作用就是进行超时控制,然后外部程序监听到超时后就可以停止执行任务,取消 Goroutine。

网上有很多用 Context 来取消 Goroutine 的字眼,初学者(比如笔者)可能误会,以为 Context 可以直接取消 Goroutine。

实际,Context 只是完成了一个信号的传递,具体的取消逻辑需要由程序自己监听这个信号,然后手动处理。

Go 语言中的 Context 通过构建一颗 Context 树,从而将没有层级的 Goroutine 关联起来。

在超时或者手动取消的时候信号都会从最顶层的 Goroutine 一层一层传递到最下层。这样该 Context 关联的所有 Goroutine 都能收到信号,然后进入自定义的退出逻辑。

在这里插入图片描述

比如这里手动取消了 ctxB1,然后 ctxB1的两个子ctx(C1和C2)也会收到取消信号,这样3个Goroutine都能收到取消信号进行退出了。

1.3 使用场景

最常见的就是 后台 HTTP/RPC Server。

在 Go 的 server 里,通常每来一个请求都会启动若干个 goroutine 同时工作:有些去数据库拿数据,有些调用下游接口获取相关数据,具体如下图:
在这里插入图片描述

而客户端一般不会无限制的等待,都会被请求设定超时时间,比如100ms。

比如这里GoroutineA消耗80ms,GoroutineB3消耗30ms,已经超时了,那么后续的GoroutineCDEF都没必要执行了,客户端已经超时返回了,服务端就算计算出结果也没有任何意义了。

所以这里就可以使用 Context 来在多个 Goroutine 之间进行超时信号传递。

同时引入超时控制后有两个好处:

  • 1)客户端可以快速返回,提升用户体验
  • 2)服务端可以减少无效的计算

源码分析

Context 在 Go 1.7 版本引入标准库中,主要内容可以概括为:

  • 1 个接口
    • Context
  • 4 种实现
    • emptyCtx
    • cancelCtx
    • timerCtx
    • valueCtx
  • 6 个方法
    • Background
    • TODO
    • WithCancel
    • WithDeadline
    • WithTimeout
    • WithValue

核心:Context接口

Context 它是一个接口,定义了几个方法:

type Context interface {// 如果返回 ok==false 则表示没有设置Deadline时间Deadline() (deadline time.Time, ok bool)// Done():返回一个只读chan,如果可以从该 chan 中读取到数据,则说明 ctx 被取消了Done() <-chan struct{}// 如果ctx没有被取消,返回nil;如果是,则返回相应的原因(错误类型)Err() error// 用于储存一些键值对。要注意使用类型断言。Value(key interface{}) interface{}
}

4个实现

context 包的核心是 context.Context 接口,另外有四个 struct 实现了 Context 接口,分别是

  • emptyCtx,
  • cancelCtx
  • timerCtx
  • valueCtx,

其中 emptyCtx 是一个默认的空结构体,其余三个都是在其基础上添加了各自功能的实现,针对 emptyCtx ,context 包中暴露了两个方法 Background()TODO() 去创建一个空的 emptyCtx

而针对后面三种具体的 struct ,context 包总共暴露了四个方法去产生对应的 struct, 他们分别是: WithCancel(), WithDeadLine(), WithTimeout(), WithValue(),对应关系如下:

在这里插入图片描述

6个方法

TODO 和 Background

TODO 和 Background 方法用来返回一个 emptyCtx 类型,他们在实现上都一样:

var (background = new(emptyCtx)todo       = new(emptyCtx)
)func Background() Context {return background
}func TODO() Context {return todo
}

这两个方法都会返回一个非空的上下文 emptyCtx,他永远不会被取消,用于传递给其他方法去构建更加复杂的上下文对象,一般默认使用 Background(), 只有在不确定时使用TODO(), 但实际上他们只是名字不同而已。

下面是 emptyCtx 的实现,他确实没做任何事。

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
}

WithCancel

type cancelCtx struct {Contextmu       sync.Mutex            // 用于同步done     chan struct{}         // 会在 Done 中返回children map[canceler]struct{} // 子上下文列表,done 被关闭后,会遍历这个 map,关闭所有的子上下文err   
http://www.yayakq.cn/news/108201/

相关文章:

  • 查找公司注册信息的网站杨浦区网站建设
  • 做汽车配件招聘网站电子商务设计网站建设
  • 如何购买网站流量斗鱼企业网站建设的目的
  • 做网站平方根怎么表示金华网站建设yw126
  • 营销型网站建设好不好wordpress图片分页插件下载
  • 深圳网站设计公司广元市利州区建设局网站
  • 温州多语言网站建设张店做网站
  • 如何做公司企业网站软件开发方案怎么写
  • 大连专业做网站ppt素材模板
  • 群推广网站电脑系统网站建设
  • 贵阳网站推广有几家网站怎么做吸引人
  • 网站海外推广技巧马鞍山北京网站建设
  • 广州定制型网站wordpress美图插件
  • 相亲网站上做投资的女生中国铁建企业门户网站
  • 手机网站制作织梦网站模板织梦模板网站怎么上线
  • 网站标签怎么做跳转成都网站建站公司
  • 中国建设银行官网站e路通下载营销推广方案设计
  • 公司网站关键词优化无锡做网站、
  • 域名访问网站应该怎么做wordpress 点点
  • 网站建设实战视频教程温州阿里巴巴网站建设
  • 文成网站制作网站怎么做购物车
  • 做集团网站搜狗官网
  • 徐州网站制作苏视网站开发工作介绍
  • 深圳团购网站设计公司金融集团网站模板
  • 免费站推广网站不用下载网站如何制作
  • 面试问你如何快速优化网站株洲网红餐厅
  • 我们的爱情网站制作wordpress 免费APP
  • 多后缀域名查询网站小程序有做门户网站
  • 石家庄网站制作模板北京云网站建设
  • 中国临海门户网站工程建设网站后台更新没有变化