diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-12-30 01:28:43 +0900 |
---|---|---|
committer | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-12-30 01:42:46 +0900 |
commit | 84c164a89c4eb0ccc6dcb939d0c9dbb185f842b8 (patch) | |
tree | b5fb0bdf4c639ebaa83492715765156d86aface5 /server/bmp.go | |
parent | 30a072030d7a4e003e5026ca772a006298cee949 (diff) |
bmp: detect bmp connection lost earlier and recover earlier
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'server/bmp.go')
-rw-r--r-- | server/bmp.go | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/server/bmp.go b/server/bmp.go index 33011048..b439f1e6 100644 --- a/server/bmp.go +++ b/server/bmp.go @@ -54,13 +54,30 @@ func newBMPClient(conf config.BmpServers, connCh chan *bmpConn) (*bmpClient, err b.ch = make(chan *broadcastBMPMsg) b.connCh = connCh + endCh := make(chan *net.TCPConn) + tryConnect := func(host string) { + interval := 1 for { + log.Debug("connecting bmp server: ", host) conn, err := net.Dial("tcp", host) if err != nil { - time.Sleep(30 * time.Second) + time.Sleep(time.Duration(interval) * time.Second) + if interval < 30 { + interval *= 2 + } } else { log.Info("bmp server is connected, ", host) + go func() { + buf := make([]byte, 1) + for { + _, err := conn.Read(buf) + if err != nil { + endCh <- conn.(*net.TCPConn) + return + } + } + }() connCh <- &bmpConn{ conn: conn.(*net.TCPConn), host: host, @@ -122,14 +139,16 @@ func newBMPClient(conf config.BmpServers, connCh chan *bmpConn) (*bmpClient, err } b, _ := msg.Serialize() - _, err := conn.Write(b) - if err != nil { - delete(connMap, host) - go tryConnect(host) + if _, err := conn.Write(b); err != nil { break } } } + case conn := <-endCh: + host := conn.RemoteAddr().String() + log.Debugf("bmp connection to %s killed", host) + delete(connMap, host) + go tryConnect(host) } } }() |