重命名客户端相关术语为节点;移动 utils 包到根路径;优化网关对节点各种连接状态的处理,并在节点断联后统一清理资源
This commit is contained in:
44
utils/sync.go
Normal file
44
utils/sync.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type WaitGroup interface {
|
||||
Add(delta int)
|
||||
Done()
|
||||
Wait()
|
||||
}
|
||||
|
||||
type CountWaitGroup struct {
|
||||
wg sync.WaitGroup
|
||||
num atomic.Int64
|
||||
}
|
||||
|
||||
func (c *CountWaitGroup) Add(delta int) {
|
||||
c.wg.Add(delta)
|
||||
c.num.Add(int64(delta))
|
||||
}
|
||||
|
||||
func (c *CountWaitGroup) Done() {
|
||||
c.wg.Done()
|
||||
c.num.Add(-1)
|
||||
}
|
||||
|
||||
func (c *CountWaitGroup) Wait() {
|
||||
c.wg.Wait()
|
||||
}
|
||||
|
||||
func (c *CountWaitGroup) Count() uint64 {
|
||||
return uint64(c.num.Load())
|
||||
}
|
||||
|
||||
func WgWait[T WaitGroup](wg T) <-chan struct{} {
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
ch <- struct{}{}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
Reference in New Issue
Block a user