Ai
2 Star 0 Fork 0

上海网仕科技/go-socket-io

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ack.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
Henrique Vicente 提交于 2018-04-05 00:37 +08:00 . Major changes on the client library.
package ack
import (
"math"
"sync"
)
// Waiter for registering acks to be fulfilled.
type Waiter struct {
counter int
counterLock sync.Mutex
message map[int](chan string)
lock sync.RWMutex
}
// Next gets a new ID for an ack message.
func (w *Waiter) Next() int {
w.counterLock.Lock()
if w.counter == math.MaxInt32 {
w.counter = -1
}
w.counter++
c := w.counter
w.counterLock.Unlock()
return c
}
// Set message.
func (w *Waiter) Set(id int, msg chan string) {
w.lock.Lock()
if w.message == nil {
w.message = map[int](chan string){}
}
w.message[id] = msg
w.lock.Unlock()
}
// Delete message.
func (w *Waiter) Delete(id int) {
w.lock.Lock()
delete(w.message, id)
w.lock.Unlock()
}
// Load a stored ack, or nil if no value is present. The ok result indicates whether a value was found.
func (w *Waiter) Load(id int) (chan string, bool) {
w.lock.RLock()
waiter, ok := w.message[id]
w.lock.RUnlock()
return waiter, ok
}
// Size of the map.
func (w *Waiter) Size() int {
w.lock.RLock()
s := len(w.message)
w.lock.RUnlock()
return s
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/anystreaming/go-socket-io.git
git@gitee.com:anystreaming/go-socket-io.git
anystreaming
go-socket-io
go-socket-io
v0.0.13

搜索帮助