diff options
author | 2019-04-04 13:37:53 +0200 | |
---|---|---|
committer | 2019-04-04 13:44:42 +0200 | |
commit | 6fe52d72255456e7d73df9d2f6b4a8f724ed447d (patch) | |
tree | 905e2203d9daf1f45db02466c36fb4e6f167f948 /adapter | |
parent | 345201eedce4cefae4db6914067a34000a7f978f (diff) |
socketclient: wait for socket to be created
domain sockets are checked/watched for existence in same
manner as shm prefixes
also the connection attempts and timeout in between can
be configured for async-connect
Change-Id: I084a3efaefea10d106866968deab90d3fda77cfe
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
Diffstat (limited to 'adapter')
-rw-r--r-- | adapter/socketclient/socketclient.go | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/adapter/socketclient/socketclient.go b/adapter/socketclient/socketclient.go index eec4fd0..f281955 100644 --- a/adapter/socketclient/socketclient.go +++ b/adapter/socketclient/socketclient.go @@ -4,9 +4,11 @@ import ( "bufio" "bytes" "fmt" + "github.com/fsnotify/fsnotify" "io" "net" "os" + "path/filepath" "strings" "sync" "time" @@ -72,9 +74,39 @@ func nilCallback(msgID uint16, data []byte) { Log.Warnf("no callback set, dropping message: ID=%v len=%d", msgID, len(data)) } -func (*vppClient) WaitReady() error { - // TODO: add watcher for socket file? - return nil +// WaitReady checks socket file existence and waits for it if necessary +func (c *vppClient) WaitReady() error { + // verify file existence + if _, err := os.Stat(c.sockAddr); err == nil { + return nil + } else if os.IsExist(err) { + return err + } + + // if not, watch for it + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer func() { + if err := watcher.Close(); err != nil { + Log.Errorf("failed to close file watcher: %v", err) + } + }() + path := filepath.Dir(c.sockAddr) + if err := watcher.Add(path); err != nil { + return err + } + + for { + ev := <-watcher.Events + if ev.Name == path { + if (ev.Op & fsnotify.Create) == fsnotify.Create { + // socket ready + return nil + } + } + } } func (c *vppClient) SetMsgCallback(cb adapter.MsgCallback) { |