summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-12-30 01:28:43 +0900
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-12-30 01:42:46 +0900
commit84c164a89c4eb0ccc6dcb939d0c9dbb185f842b8 (patch)
treeb5fb0bdf4c639ebaa83492715765156d86aface5
parent30a072030d7a4e003e5026ca772a006298cee949 (diff)
bmp: detect bmp connection lost earlier and recover earlier
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r--server/bmp.go29
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)
}
}
}()