aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVladimir Lavor <vlavor@cisco.com>2021-03-22 13:34:37 +0100
committerOndrej Fabry <ofabry@cisco.com>2021-04-07 11:30:08 +0000
commit671f16c7b4ca24788fc503b4344fa22306548a3b (patch)
treef34bd0631ceec47c3065bdd6b611f7bf186bbc61
parent8ff6fc436ce5a5be694e7d4cc9e56ded10184d80 (diff)
Add statsclient options and fix wait for socket
Change-Id: Ib5674fee5862a2b16f4b0044b6f6af533a7b6b33 Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
-rw-r--r--adapter/statsclient/statsclient.go54
-rw-r--r--examples/stats-client/stats_api.go3
2 files changed, 46 insertions, 11 deletions
diff --git a/adapter/statsclient/statsclient.go b/adapter/statsclient/statsclient.go
index e99d787..b2d91db 100644
--- a/adapter/statsclient/statsclient.go
+++ b/adapter/statsclient/statsclient.go
@@ -36,12 +36,12 @@ const (
// DefaultSocketName is default VPP stats socket file path.
DefaultSocketName = adapter.DefaultStatsSocket
- // SocketRetryPeriod is the time period after the socket availability
+ // DefaultSocketRetryPeriod is the time period after the socket availability
// will be re-checked
- SocketRetryPeriod = 50 * time.Millisecond
+ DefaultSocketRetryPeriod = 50 * time.Millisecond
- // SocketRetryTimeout is the maximum time for the stats socket
- SocketRetryTimeout = 3 * time.Second
+ // DefaultSocketRetryTimeout is the maximum time for the stats socket
+ DefaultSocketRetryTimeout = 3 * time.Second
)
var (
@@ -72,7 +72,9 @@ var _ adapter.StatsAPI = (*StatsClient)(nil)
// StatsClient is the pure Go implementation for VPP stats API.
type StatsClient struct {
- socket string
+ socket string
+ retryPeriod time.Duration
+ retryTimeout time.Duration
headerData []byte
@@ -85,15 +87,44 @@ type StatsClient struct {
statSegment
}
+// Option is a StatsClient option
+type Option func(*StatsClient)
+
+// SetSocketRetryPeriod is and optional parameter to define a custom
+// retry period while waiting for the VPP socket
+func SetSocketRetryPeriod(t time.Duration) Option {
+ return func(c *StatsClient) {
+ c.retryPeriod = t
+ }
+}
+
+// SetSocketRetryTimeout is and optional parameter to define a custom
+// timeout while waiting for the VPP socket
+func SetSocketRetryTimeout(t time.Duration) Option {
+ return func(c *StatsClient) {
+ c.retryTimeout = t
+ }
+}
+
// NewStatsClient returns a new StatsClient using socket.
// If socket is empty string DefaultSocketName is used.
-func NewStatsClient(socket string) *StatsClient {
+func NewStatsClient(socket string, options ...Option) *StatsClient {
if socket == "" {
socket = DefaultSocketName
}
- return &StatsClient{
+ s := &StatsClient{
socket: socket,
}
+ for _, option := range options {
+ option(s)
+ }
+ if s.retryPeriod == 0 {
+ s.retryPeriod = DefaultSocketRetryPeriod
+ }
+ if s.retryTimeout == 0 {
+ s.retryTimeout = DefaultSocketRetryTimeout
+ }
+ return s
}
// Connect to validated VPP stats socket and start monitoring
@@ -309,15 +340,18 @@ func (sc *StatsClient) UpdateDir(dir *adapter.StatDir) (err error) {
func (sc *StatsClient) waitForSocket() error {
if _, err := os.Stat(sc.socket); err != nil {
if os.IsNotExist(err) {
- ticker := time.NewTicker(SocketRetryPeriod)
+ n := time.Now()
+ ticker := time.NewTicker(sc.retryPeriod)
+ timeout := time.After(sc.retryTimeout)
for {
select {
case <-ticker.C:
if _, err := os.Stat(sc.socket); err == nil {
return nil
}
- case <-time.After(SocketRetryTimeout):
- return fmt.Errorf("stats socket file %s is not ready within timeout ", sc.socket)
+ case <-timeout:
+ return fmt.Errorf("stats socket file %s is not ready within timeout (after %.2f s) ",
+ sc.socket, time.Since(n).Seconds())
}
}
} else {
diff --git a/examples/stats-client/stats_api.go b/examples/stats-client/stats_api.go
index 0ccfa89..fa39b54 100644
--- a/examples/stats-client/stats_api.go
+++ b/examples/stats-client/stats_api.go
@@ -67,7 +67,8 @@ func main() {
if *async {
var statsChan chan core.ConnectionEvent
- client = statsclient.NewStatsClient(*statsSocket)
+ client = statsclient.NewStatsClient(*statsSocket, statsclient.SetSocketRetryPeriod(1*time.Second),
+ statsclient.SetSocketRetryTimeout(10*time.Second))
c, statsChan, err = core.AsyncConnectStats(client, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
if err != nil {
log.Fatalln("Asynchronous connecting failed:", err)