专栏名称: HULK一线技术杂谈
HULK是360的私有云平台,丰富的一线实战经验,为你带来最有料的技术分享
目录
相关文章推荐
国泰君安证券研究  ·  DeepSeek 影响几何(三)|国君热点研究 ·  昨天  
国泰君安证券研究  ·  元宵节|华灯如昼,福满元夕 ·  昨天  
中国证券报  ·  降费 ·  昨天  
上海证券报  ·  AI最强黑马!6个交易日涨超260% ·  2 天前  
中国证券报  ·  “公主请发财”……黄金手机贴火了 ·  3 天前  
51好读  ›  专栏  ›  HULK一线技术杂谈

Go 上下文取消操作

HULK一线技术杂谈  · 公众号  ·  · 2018-07-11 18:00

正文

女主宣言

本篇文章将解释我们如何利用上下文库的取消特性,并通过一些模式和最佳实践来使用取消,使你的程序更快、更健壮。

PS:丰富的一线技术、多元化的表现形式,尽在“ HULK一线技术杂谈 ”,点关注哦!

许多使用Go的人,都会用到它的上下文库。大多数使用 context 进行下游操作,比如发出HTTP调用,或者从数据库获取数据,或者在协程中执行异步操作。最常见的用法是传递可由所有下游操作使用的公共数据。然而,一个不太为人所知,但非常有用的上下文特性是,它能够在中途取消或停止一个操作。

本篇文章将解释我们如何利用上下文库的取消特性,并通过一些模式和最佳实践来使用取消,使你的程序更快、更健壮。

为什么需要取消?

简而言之,我们需要取消,以防止我们的系统做不不需要的工作。

考虑HTTP服务器对数据库的调用的常见情况,并将查询的数据返回给客户端:

时间图,如果一切都很完美,就会是这样的:

但是,如果客户端取消了中间的请求,会发生什么呢?例如,如果客户端关闭了他们的浏览器,这可能会发生。如果没有取消,应用服务器和数据库将继续执行它们的工作,即使工作的结果将被浪费:

理想情况下,如果我们知道进程(在本例中是HTTP请求)停止了,我们希望流程的所有下游组件停止工作:

1

上下文取消

现在我们知道了为什么需要取消,让我们来看看如何实现它。因为“取消”的事件与交易或正在执行的操作高度相关,所以它与上下文捆绑在一起是很自然的。

取消的有两个方面,你可能想要实现:

  1. 监听取消事件

  2. 提交取消事件

2

监听取消事件

上下文类型提供了 Done() 方法,每当上下文收到取消事件时,它都会返回接收空 struct{} 类型的通道。监听取消事件就像等待

例如,让我们考虑一个HTTP服务器,它需要两秒钟来处理一个事件。如果在此之前请求被取消,我们希望立即返回:

func main() {
       // Create an HTTP server that listens on port 8000 http.ListenAndServe(":8000", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context()
// This prints to STDOUT to show that processing has started fmt.Fprint(os.Stdout, "processing request\n")
// We use `select` to execute a peice of code depending on which // channel receives a message first select {
       
case 2 * time.Second):
// If we receive a message after 2 seconds // that means the request has been processed // We then write this as the response w.Write([]byte("request processed"))
       
case // If the request gets cancelled, log it // to STDERR fmt.Fprint(os.Stderr, "request cancelled\n") } })) }
你可以通过运行服务器并在浏览器上打开localhost:8000来测试。如果你在2秒前关闭浏览器,你应该会看到在终端窗口上打印的“请求取消”。

3

提交取消事件

如果你有一个可以被取消的操作,你将不得不通过上下文发出取消事件。这可以通过 context 包中的 WithCancel 函数来完成,它返回一个上下文对象和一个函数。这个函数没有参数,也不返回任何东西,当你想要取消上下文时调用。

考虑两个从属操作的情况。在这里,“依赖”意味着如果一个失败了,另一个就没有意义了。在这种情况下,如果我们在早期就知道其中一个操作失败了,我们想要取消所有的依赖操作。

func operation1(ctx context.Context) error {
// Let's assume that this operation failed for some reason // We use time.Sleep to simulate a resource intensive operation time.Sleep(100 * time.Millisecond)
return errors.New("failed") }

func operation2(ctx context.Context) {
// We use a similar pattern to the HTTP server // that we saw in the earlier example select {
     
case 500 * time.Millisecond): fmt.Println("done")
   
case "halted operation2") } }

func main() {
// Create a new context ctx := context.Background()
// Create a new context, with its cancellation function // from the original context ctx, cancel := context.WithCancel(ctx)
// Run two operations: one in a different go routine go func() { err := operation1(ctx)
// If this operation returns an error // cancel all operations using this context if err != nil { cancel() } }()
// Run operation2 with the same context we use for operation1 operation2(ctx) }

4

基于时间取消

任何需要在请求的最大持续时间内维护SLA(服务水平协议)的应用程序都应该使用基于时间的取消。该API几乎与前面的示例相同,并添加了一些内容:

// The context will be cancelled after 3 seconds
// If it needs to be cancelled earlier, the `cancel` function can
// be used, like before

ctx, cancel := context.WithTimeout(ctx, 3*time.Second)

// The context will be cancelled on 2009-11-10 23:00:00
ctx, cancel := context.WithDeadline(ctx, time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))

例如,考虑对外部服务进行HTTP API调用。如果服务花费的时间太长,最好是尽早失败并取消请求:

func main() {
// Create a new context // With a deadline of 100 milliseconds ctx := context.Background() ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)






请到「今天看啥」查看全文