From efbde3e3f80973800854b3e9c1f3bb27f8004b38 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 29 Dec 2023 12:16:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20super=20=E5=8C=85=E6=96=B0=E5=A2=9E=20T?= =?UTF-8?q?ryWriteChannelByHandler=20=E5=87=BD=E6=95=B0=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B0=9D=E8=AF=95=E5=86=99=E5=85=A5=20channel?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=20channel=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=88=99=E6=89=A7=E8=A1=8C=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/super/channel.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/utils/super/channel.go b/utils/super/channel.go index d91560a..242510c 100644 --- a/utils/super/channel.go +++ b/utils/super/channel.go @@ -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() + } +}