From 4e862fa8f0017383ecdae22c757753214ba7815c Mon Sep 17 00:00:00 2001 From: Hitoshi Irino Date: Sat, 29 Dec 2018 21:53:10 +0900 Subject: zebra: Introducing MIN_ZAPIVER and MAX_ZAPIVER. And avoiding double close channel when sequential retry to connect zebra. - Introducing MIN_ZAPIVER and MAX_ZAPIVER for avoiding editing files except for zapi.go when new ZAPI version will be supported in future. - Fix a bug avoiding panic by double close for a channel. - Changing algorithm for sequential retry to connect zebra. --- pkg/server/zclient.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'pkg/server/zclient.go') diff --git a/pkg/server/zclient.go b/pkg/server/zclient.go index a814e20a..3e4fb3e6 100644 --- a/pkg/server/zclient.go +++ b/pkg/server/zclient.go @@ -341,6 +341,9 @@ func (z *zebraClient) loop() { case <-z.dead: return case msg := <-z.client.Receive(): + if msg == nil { + break + } switch body := msg.Body.(type) { case *zebra.IPRouteBody: if path := newPathFromIPRouteMessage(msg, z.client.Version); path != nil { @@ -419,19 +422,39 @@ func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nh } var cli *zebra.Client var err error - for _, ver := range []uint8{version, 2, 3, 4, 5, 6} { + var usingVersion uint8 + var zapivers [zebra.MaxZapiVer - zebra.MinZapiVer + 1]uint8 + zapivers[0] = version + for elem, ver := 1, zebra.MinZapiVer; elem < len(zapivers) && ver <= zebra.MaxZapiVer; elem++ { + if version == ver && ver < zebra.MaxZapiVer { + ver++ + } + zapivers[elem] = ver + ver++ + } + for elem, ver := range zapivers { cli, err = zebra.NewClient(l[0], l[1], zebra.ROUTE_BGP, ver) - if err == nil { + if cli != nil && err == nil { + usingVersion = ver break } // Retry with another Zebra message version log.WithFields(log.Fields{ "Topic": "Zebra", - }).Warnf("cannot connect to Zebra with message version %d. going to retry another version...", ver) + }).Warnf("cannot connect to Zebra with message version %d.", ver) + if elem < len(zapivers)-1 { + log.WithFields(log.Fields{ + "Topic": "Zebra", + }).Warnf("going to retry another version %d.", zapivers[elem+1]) + } } - if cli == nil { + if cli == nil || err != nil { return nil, err } + log.WithFields(log.Fields{ + "Topic": "Zebra", + }).Infof("success to connect to Zebra with message version %d.", usingVersion) + // Note: HELLO/ROUTER_ID_ADD messages are automatically sent to negotiate // the Zebra message version in zebra.NewClient(). // cli.SendHello() -- cgit v1.2.3