diff options
author | Vladimir Lavor <vlavor@cisco.com> | 2021-02-18 16:05:30 +0100 |
---|---|---|
committer | Vladimir Lavor <vlavor@cisco.com> | 2021-02-23 13:28:47 +0100 |
commit | 4459b648e9fb53c34abbf52a00e63ad384fb9ee2 (patch) | |
tree | fbe5885b5aa2e4d3ff10255327d22ddd56a86c92 /examples/stats-client | |
parent | 4c1cccf48cd144414c7233f167087aff770ef67b (diff) |
Added asynchronous connection for stats socket
The stats socket now allows an option to connect asynchronously in
the same manner as the api socket connection. New method
AsyncConnectStats returns a channel where notificaitons
of type ConnectionEvent will be sent.
Fixed the stats reconnect procedure which sometimes failed to re-eneable
the connection.
Change-Id: I0bdb19f0d57e3a1ea259b8b1ba0a5e5fa49a09db
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
Diffstat (limited to 'examples/stats-client')
-rw-r--r-- | examples/stats-client/stats_api.go | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/examples/stats-client/stats_api.go b/examples/stats-client/stats_api.go index b1846a6..66dd451 100644 --- a/examples/stats-client/stats_api.go +++ b/examples/stats-client/stats_api.go @@ -39,6 +39,7 @@ var ( statsSocket = flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket") dumpAll = flag.Bool("all", false, "Dump all stats including ones with zero values") pollPeriod = flag.Duration("period", time.Second*5, "Polling interval period") + async = flag.Bool("async", false, "Use asynchronous connection") ) func init() { @@ -58,11 +59,33 @@ func main() { patterns = flag.Args()[1:] } - client := statsclient.NewStatsClient(*statsSocket) - - c, err := core.ConnectStats(client) - if err != nil { - log.Fatalln("Connecting failed:", err) + var ( + client *statsclient.StatsClient + c *core.StatsConnection + err error + ) + + if *async { + var statsChan chan core.ConnectionEvent + client = statsclient.NewStatsClient(*statsSocket) + c, statsChan, err = core.AsyncConnectStats(client, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval) + if err != nil { + log.Fatalln("Asynchronous connecting failed:", err) + } + select { + case e := <-statsChan: + if e.State == core.Connected { + // OK + } else { + log.Fatalf("VPP stats asynchronous connection failed: %s\n", e.State.String()) + } + } + } else { + client = statsclient.NewStatsClient(*statsSocket) + c, err = core.ConnectStats(client) + if err != nil { + log.Fatalln("Connecting failed:", err) + } } defer c.Disconnect() |