Skip to content

Commit

Permalink
feat: super 包新增 TryWriteChannelByHandler 函数,支持尝试写入 channel,如果 channel…
Browse files Browse the repository at this point in the history
… 无法写入则执行 handler
  • Loading branch information
kercylan98 committed Dec 29, 2023
1 parent 80f38ff commit efbde3e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions utils/super/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit efbde3e

Please sign in to comment.