1 Star 1 Fork 0

vincent/gcutil

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
chanUtils.go 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
vincent 提交于 2022-02-08 13:44 . feat(): 20220208 年后的更新
package chanUtil
import (
"errors"
)
var (
ErrNoLongerReceive = errors.New("receiver no longer receives value from channel")
ErrFullChan = errors.New("channel is full")
ErrEmptyChan = errors.New("channel is empty")
)
type Chan1Sender1Receiver interface {
TrySend(val interface{}) error // 尝试发送
Send(val interface{}) // 接收
StopSend() // 发送方停止发送
TryReceive() (interface{}, error) // 尝试接收
Receive() interface{} // 接收 v := <-c
ReceiveWithBoolean() (interface{}, bool) // 接收 v, ok := <-c
StopReceive() // 接收方停止接收
Range(func(val interface{}) bool) // for-range语法 类似于sync.Map.Range
}
type defaultChan1Sender1Receiver struct {
buffer chan interface{}
signal chan struct{}
}
func NewDefaultChan1Sender1Receiver(size uint) Chan1Sender1Receiver {
return &defaultChan1Sender1Receiver{
buffer: make(chan interface{}, size),
signal: make(chan struct{}),
}
}
func (c *defaultChan1Sender1Receiver) TryReceive() (interface{}, error) {
select {
case v := <-c.buffer:
return v, nil
default:
return nil, ErrEmptyChan
}
}
func (c *defaultChan1Sender1Receiver) Receive() interface{} {
return <-c.buffer
}
func (c *defaultChan1Sender1Receiver) ReceiveWithBoolean() (v interface{}, ok bool) {
v, ok = <-c.buffer
return
}
func (c *defaultChan1Sender1Receiver) StopSend() {
close(c.buffer)
}
func (c *defaultChan1Sender1Receiver) StopReceive() {
close(c.signal)
}
func (c *defaultChan1Sender1Receiver) Send(v interface{}) {
c.buffer <- v
}
func (c *defaultChan1Sender1Receiver) TrySend(v interface{}) error {
// 接收方不再接收的优先级是最高的
// 调用者在收到ErrNoLongerReceive之后就不应该再向管道发送数据了
select {
case c.buffer <- v:
// 虽然成功发了一个数据 但接收方已经不再接收倒也没啥关系
select {
case <-c.signal:
return ErrNoLongerReceive
default:
return nil
}
case <-c.signal:
return ErrNoLongerReceive
default:
return ErrFullChan
}
}
func (c *defaultChan1Sender1Receiver) Range(f func(interface{}) bool) {
for v := range c.buffer {
if !f(v) {
break
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/vincent78/gcutil.git
git@gitee.com:vincent78/gcutil.git
vincent78
gcutil
gcutil
v1.0.1

搜索帮助