feat: super 包新增 TryWriteChannelByHandler 函数,支持尝试写入 channel,如果 channel 无法写入则执行 handler

This commit is contained in:
kercylan98 2023-12-29 12:16:51 +08:00
parent 80f38ffe9c
commit efbde3e3f8
1 changed files with 10 additions and 0 deletions

View File

@ -10,3 +10,13 @@ func TryWriteChannel[T any](ch chan<- T, data T) bool {
return false
}
}
// TryWriteChannelByHandler 尝试写入 channel如果 channel 无法写入则执行 handler
// - 无法写入的情况包括channel 已满、channel 已关闭
func TryWriteChannelByHandler[T any](ch chan<- T, data T, handler func()) {
select {
case ch <- data:
default:
handler()
}
}