diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/connection.go | 69 | ||||
-rw-r--r-- | core/request_handler.go | 3 |
2 files changed, 20 insertions, 52 deletions
diff --git a/core/connection.go b/core/connection.go index c4048f0..042a2af 100644 --- a/core/connection.go +++ b/core/connection.go @@ -65,11 +65,6 @@ type ConnectionEvent struct { Error error } -var ( - connLock sync.RWMutex // lock for the global connection - conn *Connection // global handle to the Connection (used in the message receive callback) -) - // Connection represents a shared memory connection to VPP via vppAdapter. type Connection struct { vppClient adapter.VppAPI // VPP binary API client adapter @@ -111,10 +106,7 @@ func newConnection(binapi adapter.VppAPI) *Connection { // This call blocks until VPP is connected, or an error occurs. Only one connection attempt will be performed. func Connect(binapi adapter.VppAPI) (*Connection, error) { // create new connection handle - c, err := createConnection(binapi) - if err != nil { - return nil, err - } + c := newConnection(binapi) // blocking attempt to connect to VPP if err := c.connectVPP(); err != nil { @@ -130,10 +122,7 @@ func Connect(binapi adapter.VppAPI) (*Connection, error) { // Connected/Disconnected events. In case of disconnect, the library will asynchronously try to reconnect. func AsyncConnect(binapi adapter.VppAPI) (*Connection, chan ConnectionEvent, error) { // create new connection handle - c, err := createConnection(binapi) - if err != nil { - return nil, nil, err - } + c := newConnection(binapi) // asynchronously attempt to connect to VPP connChan := make(chan ConnectionEvent, NotificationChanBufSize) @@ -142,35 +131,6 @@ func AsyncConnect(binapi adapter.VppAPI) (*Connection, chan ConnectionEvent, err return c, connChan, nil } -// Disconnect disconnects from VPP and releases all connection-related resources. -func (c *Connection) Disconnect() { - if c == nil { - return - } - - connLock.Lock() - defer connLock.Unlock() - - if c.vppClient != nil { - c.disconnectVPP() - } - conn = nil -} - -// newConnection returns new connection handle. -func createConnection(binapi adapter.VppAPI) (*Connection, error) { - connLock.Lock() - defer connLock.Unlock() - - if conn != nil { - return nil, errors.New("only one connection per process is supported") - } - - conn = newConnection(binapi) - - return conn, nil -} - // connectVPP performs blocking attempt to connect to VPP. func (c *Connection) connectVPP() error { log.Debug("Connecting to VPP..") @@ -193,6 +153,24 @@ func (c *Connection) connectVPP() error { return nil } +// Disconnect disconnects from VPP and releases all connection-related resources. +func (c *Connection) Disconnect() { + if c == nil { + return + } + + if c.vppClient != nil { + c.disconnectVPP() + } +} + +// disconnectVPP disconnects from VPP in case it is connected. +func (c *Connection) disconnectVPP() { + if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) { + c.vppClient.Disconnect() + } +} + func (c *Connection) NewAPIChannel() (api.Channel, error) { return c.newAPIChannel(RequestChanBufSize, ReplyChanBufSize) } @@ -314,13 +292,6 @@ func (c *Connection) retrieveMessageIDs() (err error) { return nil } -// disconnectVPP disconnects from VPP in case it is connected. -func (c *Connection) disconnectVPP() { - if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) { - c.vppClient.Disconnect() - } -} - // connectLoop attempts to connect to VPP until it succeeds. // Then it continues with healthCheckLoop. func (c *Connection) connectLoop(connChan chan ConnectionEvent) { diff --git a/core/request_handler.go b/core/request_handler.go index 545f235..4004d15 100644 --- a/core/request_handler.go +++ b/core/request_handler.go @@ -132,9 +132,6 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error { // msgCallback is called whenever any binary API message comes from VPP. func (c *Connection) msgCallback(msgID uint16, data []byte) { - connLock.RLock() - defer connLock.RUnlock() - if c == nil { log.Warn("Already disconnected, ignoring the message.") return |