go-超时控制time.After()

业务逻辑中,对一些逻辑进行合理的超时控制,往往能够极大的提升程序的稳定性,特别是对一些接口的实现上。

go语言中,通过time.After()可以很优雅的实现超时控制,且不会造成阻塞。

time.After()底层

// After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
	return NewTimer(d).C
}

time.After()表示time.Duration长的时候后返回一条time.Time类型的通道消息,但是在取出channel内容之前不阻塞,后续程序可以继续执行。

特性样例

func TestOther(t *testing.T) {
	t.Log(time.Now().Unix())
	tm := time.After(3  * time.Second)

	t.Log("waiting...")

	t.Log(<-tm)
	t.Log(time.Now().Unix())
}

输出:

time.After()

可以看到,调用time.After()后,程序非阻塞,继续执行,知道读取tm管道信息时,阻塞3s后,才读取到管道信息

超时控制

func waitForStopOrTimeOut(stopCh <- chan struct{}, timeout time.Duration) <- chan bool {
	stopWithTimeOut := make(chan bool)
	go func() {
		select {
		case <- stopCh:
			// 若接收到业务逻辑正常结束的消息,则为自然结束
			fmt.Println("自然结束")
			stopWithTimeOut <- false
		case <- time.After(timeout):
			// 若timeout时间内,未接收到业务逻辑正常结束的消息,则为超时
			// timeout时间后,time.After(timeout)可读取到管道信息
			fmt.Println("超时")
			stopWithTimeOut <- true
		}
		close(stopWithTimeOut)
	}()
	return stopWithTimeOut
}

func doSomething()  {
	time.Sleep(time.Second * 2)
}

func TestOther(t *testing.T) {
	t.Log("start")

	stopCh := make(chan struct{})
	go func() {
		// 需要控制超时时间的业务逻辑
		doSomething()

		// 业务逻辑正常执行完,通知管道stopCh,执行完成
		stopCh <- struct {}{}
	}()

	// 启用超时控制
	stopWithTimeOut := waitForStopOrTimeOut(stopCh, 3 * time.Second)

	select {
	// 若为超时,正常结束,isTimeOut=false, 否则为true
	case isTimeOut := <- stopWithTimeOut:
		if isTimeOut {
			t.Log("end timeout")
		} else {
			t.Log("end ok")
		}
	}
	return
}

上述代码,限制的超时时间是3s, waitForStopOrTimeOut(stopCh, 3 * time.Second), doSomething()中,time.Sleep()分别为2s, 4s时的执行结果分别为:

可见,我们已经实现了符合预期的超时控制逻辑。

0 0 投票数
文章评分
规则怪谈生成器 - 三步生成完整规则怪谈

⬡ 规则怪谈生成器

三步选择,一键生成完整的规则怪谈场景

欢迎使用 规则怪谈生成器 (GuiZeGT) —— 专为恐怖创作者、跑团主持人和怪谈爱好者设计的在线工具。 零AI成本 · 纯模板拼接 · 无限制生成 · 免费使用

1 选择场景
2 选择氛围
3 选择难度
一键生成
  • 🏚️ 12个经典场景 核心
    从废弃医院到深海孤岛,丰富模板即选即用
  • 👻 8种恐怖风格 氛围
    心理恐怖、克苏鲁、都市传说……塑造独特惊悚感
  • 📜 3档规则数量 难度
    从简洁到复杂,控制怪谈的细节密度与挑战性
  • ⚡ 逐条浮现生成 体验
    规则逐条动态呈现,营造紧张有序的阅读节奏
⚡ 或选择一个场景快速开始
🏥 废弃医院 🚢 幽灵邮轮 🌲 黑森林 🏫 午夜学校
订阅评论
提醒

0 评论
最新
最旧 最多投票
创世工坊 - 专业世界观构建工具

🚀 创世工坊 - 构建你的世界观

欢迎使用 创世工坊 (CreateAUniverse) —— 专为世界构建者、故事创作者和幻想宇宙缔造者设计的在线平台。将您的想象力,变成可分享的完整世界。

49,867 创作者
8,924 世界观
33,496 角色
  • 世界观构建:从地理环境到文化背景,全方位打造独一无二的架空世界。
  • 人物关系网络:可视化梳理复杂的人物关系网,让爱恨情仇一目了然。
  • 地图编辑器:强大的绘图工具,支持多种地图类型,绘制属于你的世界地图。
  • 时间线管理:梳理世界历史脉络,记录每个重要时刻,让时间线清晰有序。
  • 灵感与伏笔管理:随时记录灵感碎片,追踪每个伏笔的铺设与回收,确保逻辑严密。
滚动至顶部