Skip to content

Commit

Permalink
Merge pull request #68 from hxzhouh/master
Browse files Browse the repository at this point in the history
add #70
  • Loading branch information
smallnest authored Apr 21, 2024
2 parents 17e6ff5 + df1aca8 commit 3f600de
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 67 | How to deal with long function signatures | richzw |
| 68 | Use the deadcode tool to find and remove unused functions | richzw |
| 69 | Manage multiple goroutines with errgroup | richzw |
| 70 | | |
| 70 | Implement a context-aware sleep function | hxzhouh |
| 71 | sync.Pool, make it typed-safe with generics | QingyaFan |
| 72 | | |
| 73 | Implement String() for enum with the stringer tool | syjs10 |
Expand Down
40 changes: 40 additions & 0 deletions src/070.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Tip #70 实现一个感知context的sleep 函数

> 原始链接: [Golang Tip #70: Implement a context-aware sleep function.](https://twitter.com/func25/status/1776936187137229071)
**常规 time.Sleep ()不关心`context`。如果您将其设置为暂停 5 分钟,无论如何,它都会暂停整个过程.**

即使我们尝试在`context`中使用它,就像下面的例子一样,它也要等到 10 秒后才会停止:

![tips-070-img1](./images/070/tips070-img1.png)

> 我们在Tips #63 中讨论了解决方案:[避免 time.Sleep ()](https://twitter.com/func25/status/1774070336214253734)
我们之前讨论的解决方案是可行的,但是一遍又一遍地写就有点麻烦了。

因此,让我们制作一个更加用户友好的版本,仍然让我们使用 Sleep() 函数,但尊重`context`被取消的时间。
我们可以创建一个“假”睡眠函数,如果`context`告诉它停止,它就会停止:

![tips070-img3](./images/070/tips070-img2.png)

https://go.dev/play/p/FErMIDKoulb

这样,我们可以在 Go 中暂停代码,但如果有东西告诉`context`停止,Sleep将提前结束:

![](./images/070/tips070-img3.png)

>“等等,你为什么不处理sleep()的错误呢?”
嗯,我们通常不需要。

大多数时候,当`context`被取消时,与sleep功能无关。这通常是因为程序中有一个更大的问题,其中包括sleep部分。

例如,如果代码中`sleep()`之后有步骤,并且它们被设置为监听上下文,那么如果`context`被取消,它们也会停止。

因此,让我们的sleep时间更短更加重要。

![tips070-img1](./images/070/tips070-img4.png)

---

译者注: 评论区里面提到了一个 stackoverflow 的讨论,[How can I sleep with responsive context cancelation?](https://stackoverflow.com/questions/55135239/how-can-i-sleep-with-responsive-context-cancelation/77415571#77415571) 感兴趣的朋友可以看看。
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [Tip #67 如何处理长函数签名](./067.md)
- [Tip #68 使用deadcode工具来找到和删除无用的函数](./068.md)
- [Tip #69 通过errgrup管理多个goroutine](./069.md)
- [Tip #70 实现一个感知context的sleep 函数](./070.md)
- [Tip #73 用stringer工具给枚举类型实现String()方法](./073.md)
- [Tip #74 使 time.Duration 清晰易懂](./074.md)
- [Tip #75 使用singleflight优化多次调用](./075.md)
Expand Down
Binary file added src/images/070/tips070-img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/070/tips070-img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/070/tips070-img3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/070/tips070-img4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3f600de

Please sign in to comment.