package tcp_listen
type Connection interface {
Read() (value interface{}, tag byte, err error)
Write(tag byte, body interface{}) (err error)
Close() error
LocalAddr() string
RemoteAddr() string
}
type isReconn struct {
reconn bool
connFunc func()
continueChan []chan bool
}
func (self *isReconn) success() {
self.reconn = true
self.connFunc()
for _, Chan := range self.continueChan {
Chan <- true
}
self.reconn = false
}
func (self *isReconn) Reconnection() <-chan bool {
Chan := make(chan bool, 1)
if !self.reconn {
self.continueChan = []chan bool{}
self.continueChan = append(self.continueChan, Chan)
go self.success()
} else {
self.continueChan = append(self.continueChan, Chan)
}
return Chan
}
func NewReconnection(f func()) *isReconn {
return &isReconn{connFunc: f, continueChan: []chan bool{}}
}
Golang自玩项目中一个重连机制实现.
最新推荐文章于 2024-07-16 11:55:20 发布