Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 13, 2019
1 parent 9db2f8b commit 01b7a70
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (c *Client) Dial(remote appnet.Addr) (net.Conn, error) {
}

conn.freeConnMx.Lock()

free, err := c.cm.Add(connID, conn)

if err != nil {
Expand All @@ -120,7 +121,9 @@ func (c *Client) Dial(remote appnet.Addr) (net.Conn, error) {

return nil, err
}

conn.freeConn = free

conn.freeConnMx.Unlock()

return conn, nil
Expand Down Expand Up @@ -148,16 +151,20 @@ func (c *Client) Listen(n appnet.Type, port routing.Port) (net.Listener, error)
}

listener.freeLisMx.Lock()

freeLis, err := c.lm.Add(lisID, listener)
if err != nil {
listener.freeLisMx.Unlock()

if err := listener.Close(); err != nil {
c.log.WithError(err).Error("error closing listener")
}

return nil, err
}

listener.freeLis = freeLis

listener.freeLisMx.Unlock()

return listener, nil
Expand All @@ -167,6 +174,7 @@ func (c *Client) Listen(n appnet.Type, port routing.Port) (net.Listener, error)
// listeners and connections.
func (c *Client) Close() {
var listeners []net.Listener

c.lm.DoRange(func(_ uint16, v interface{}) bool {
lis, err := idmanager.AssertListener(v)
if err != nil {
Expand All @@ -179,6 +187,7 @@ func (c *Client) Close() {
})

var conns []net.Conn

c.cm.DoRange(func(_ uint16, v interface{}) bool {
conn, err := idmanager.AssertConn(v)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pkg/app/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@ func TestClient_Close(t *testing.T) {
l := logging.MustGetLogger("app2_client")
visorPK, _ := cipher.GenerateKeyPair()

var closeNoErr error
closeErr := errors.New("close error")
var (
closeNoErr error
closeErr = errors.New("close error")
)

rpc := &MockRPCClient{}

Expand All @@ -321,11 +323,13 @@ func TestClient_Close(t *testing.T) {
lis1 := &Listener{id: lisID1, rpc: rpc, cm: idmanager.New()}
freeLis1, err := lm.Add(lisID1, lis1)
require.NoError(t, err)

lis1.freeLis = freeLis1

lis2 := &Listener{id: lisID2, rpc: rpc, cm: idmanager.New()}
freeLis2, err := lm.Add(lisID2, lis2)
require.NoError(t, err)

lis2.freeLis = freeLis2

connID1 := uint16(1)
Expand All @@ -339,11 +343,13 @@ func TestClient_Close(t *testing.T) {
conn1 := &Conn{id: connID1, rpc: rpc}
freeConn1, err := cm.Add(connID1, conn1)
require.NoError(t, err)

conn1.freeConn = freeConn1

conn2 := &Conn{id: connID2, rpc: rpc}
freeConn2, err := cm.Add(connID2, conn2)
require.NoError(t, err)

conn2.freeConn = freeConn2

cl := prepClient(l, visorPK, rpc)
Expand Down
1 change: 1 addition & 0 deletions pkg/app/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *Conn) Write(b []byte) (int, error) {
func (c *Conn) Close() error {
c.freeConnMx.RLock()
defer c.freeConnMx.RUnlock()

if c.freeConn != nil {
if freed := c.freeConn(); !freed {
return errors.New("conn is already closed")
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Listener struct {
// Accept accepts a connection from listener.
func (l *Listener) Accept() (net.Conn, error) {
l.log.Infoln("Calling app RPC Accept")

connID, remote, err := l.rpc.Accept(l.id)
if err != nil {
return nil, err
Expand All @@ -47,6 +48,7 @@ func (l *Listener) Accept() (net.Conn, error) {
// the conn without `freeConn` while the next few lines are running,
// the panic may raise without this lock
conn.freeConnMx.Lock()

free, err := l.cm.Add(connID, conn)
if err != nil {
conn.freeConnMx.Unlock()
Expand All @@ -68,6 +70,7 @@ func (l *Listener) Accept() (net.Conn, error) {
func (l *Listener) Close() error {
l.freeLisMx.RLock()
defer l.freeLisMx.RUnlock()

if l.freeLis != nil {
if freed := l.freeLis(); !freed {
return errors.New("listener is already closed")
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/log_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func newBoltDB(path, appName string) (_ LogStore, err error) {

return nil
})

if err != nil && !strings.Contains(err.Error(), bbolt.ErrBucketExists.Error()) {
return nil, err
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func (l *boltDBappLogs) LogsSince(t time.Time) (logs []string, err error) {
if err != nil {
return nil, err
}

defer func() {
cErr := db.Close()
err = cErr
Expand Down
1 change: 1 addition & 0 deletions pkg/app/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func prepNetworkerWithListener(t *testing.T, lis *appcommon.MockListener, local
var noErr error

appnet.ClearNetworkers()

n := &appnet.MockNetworker{}
n.On("ListenContext", mock.Anything, local).Return(lis, noErr)

Expand Down

0 comments on commit 01b7a70

Please sign in to comment.