Golang自玩项目中一个重连机制实现.

前端之家收集整理的这篇文章主要介绍了Golang自玩项目中一个重连机制实现.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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{}}
}
原文链接:/go/189549.html

猜你在找的Go相关文章