Skip to content

Commit

Permalink
Improve appnet code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 16, 2019
1 parent 291bbfc commit 37372f8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/yamux v0.0.0-20190923154419-df201c70410d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -171,6 +172,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions pkg/app/appnet/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (a Addr) String() string {
if a.Port == 0 {
return fmt.Sprintf("%s:~", a.PubKey)
}

return fmt.Sprintf("%s:%d", a.PubKey, a.Port)
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/app/appnet/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func AddNetworker(t Type, n Networker) error {
// ResolveNetworker resolves Networker by `network`.
func ResolveNetworker(t Type) (Networker, error) {
networkersMx.RLock()

n, ok := networkers[t]
if !ok {
networkersMx.RUnlock()
return nil, ErrNoSuchNetworker
}

networkersMx.RUnlock()

return n, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/app/appnet/networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestAddNetworker(t *testing.T) {
ClearNetworkers()

nType := TypeDMSG

var n Networker

err := AddNetworker(nType, n)
Expand All @@ -28,6 +29,7 @@ func TestResolveNetworker(t *testing.T) {
ClearNetworkers()

nType := TypeDMSG

var n Networker

n, err := ResolveNetworker(nType)
Expand Down
30 changes: 18 additions & 12 deletions pkg/app/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appnet
import (
"context"
"errors"
"io"
"net"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (r *SkywireNetworker) ListenContext(ctx context.Context, addr Addr) (net.Li

if atomic.CompareAndSwapInt32(&r.isServing, 0, 1) {
go func() {
if err := r.serve(ctx); err != nil {
if err := r.serveLoop(ctx); err != nil {
r.log.WithError(err).Error("error serving")
}
}()
Expand All @@ -88,10 +89,11 @@ func (r *SkywireNetworker) ListenContext(ctx context.Context, addr Addr) (net.Li
return lis, nil
}

// serve accepts and serves routes.
func (r *SkywireNetworker) serve(ctx context.Context) error {
// serveLoop accepts and serves routes.
func (r *SkywireNetworker) serveLoop(ctx context.Context) error {
for {
r.log.Infoln("Trying to accept routing group...")

rg, err := r.r.AcceptRoutes(ctx)
if err != nil {
r.log.Infof("Error accepting routing group: %v", err)
Expand All @@ -100,39 +102,42 @@ func (r *SkywireNetworker) serve(ctx context.Context) error {

r.log.Infoln("Accepted routing group")

go r.serveRG(rg)
go r.serve(rg)
}
}

// serveRG passes accepted router group to the corresponding listener.
func (r *SkywireNetworker) serveRG(rg *router.RouteGroup) {
localAddr, ok := rg.LocalAddr().(routing.Addr)
func (r *SkywireNetworker) serve(conn net.Conn) {
localAddr, ok := conn.LocalAddr().(routing.Addr)
if !ok {
r.closeRG(rg)
r.close(conn)
r.log.Error("wrong type of addr in accepted conn")

return
}

lisIfc, ok := r.porter.PortValue(uint16(localAddr.Port))
if !ok {
r.closeRG(rg)
r.close(conn)
r.log.Errorf("no listener on port %d", localAddr.Port)

return
}

lis, ok := lisIfc.(*skywireListener)
if !ok {
r.closeRG(rg)
r.close(conn)
r.log.Errorf("wrong type of listener on port %d", localAddr.Port)

return
}

lis.putConn(rg)
lis.putConn(conn)
}

// closeRG closes router group and logs error if any.
func (r *SkywireNetworker) closeRG(rg *router.RouteGroup) {
if err := rg.Close(); err != nil {
func (r *SkywireNetworker) close(closer io.Closer) {
if err := closer.Close(); err != nil {
r.log.Error(err)
}
}
Expand Down Expand Up @@ -191,6 +196,7 @@ type skywireConn struct {
// Close closes connection.
func (c *skywireConn) Close() error {
var err error

c.once.Do(func() {
defer func() {
c.freePortMx.RLock()
Expand Down
1 change: 1 addition & 0 deletions pkg/app/appnet/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestType_IsValid(t *testing.T) {
}

for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
valid := tc.t.IsValid()
require.Equal(t, tc.want, valid)
Expand Down

0 comments on commit 37372f8

Please sign in to comment.