Skip to content

Commit

Permalink
Make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 12, 2019
1 parent 1efe90e commit 29ff199
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 41 deletions.
1 change: 1 addition & 0 deletions pkg/app/appnet/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestConvertAddr(t *testing.T) {
}

for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
addr, err := ConvertAddr(tc.addr)
require.Equal(t, err, tc.want.err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r *SkywireNetworker) Dial(addr Addr) (net.Conn, error) {
return r.DialContext(context.Background(), addr)
}

// Dial dials remote `addr` via `skynet` with context.
// DialContext dials remote `addr` via `skynet` with context.
func (r *SkywireNetworker) DialContext(ctx context.Context, addr Addr) (net.Conn, error) {
localPort, freePort, err := r.porter.ReserveEphemeral(ctx, nil)
if err != nil {
Expand All @@ -59,7 +59,7 @@ func (r *SkywireNetworker) Listen(addr Addr) (net.Listener, error) {
return r.ListenContext(context.Background(), addr)
}

// Listen starts listening on local `addr` in the skynet with context.
// ListenContext starts listening on local `addr` in the skynet with context.
func (r *SkywireNetworker) ListenContext(ctx context.Context, addr Addr) (net.Listener, error) {
lis := &skywireListener{
addr: addr,
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/appserver/rpc_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appserver

import (
"context"
"errors"
"math"
"net"
"strings"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
Expand Down
5 changes: 3 additions & 2 deletions pkg/app/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package app

import (
"errors"
"fmt"
"net"
"net/rpc"
"os"

"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/pkg/errors"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
Expand Down Expand Up @@ -77,7 +78,7 @@ type Client struct {
func NewClient(log *logging.Logger, config ClientConfig) (*Client, error) {
rpcCl, err := rpc.Dial("unix", config.SockFile)
if err != nil {
return nil, errors.Wrap(err, "error connecting to the app server")
return nil, fmt.Errorf("error connecting to the app server: %v", err)
}

return &Client{
Expand Down
5 changes: 2 additions & 3 deletions pkg/app/client_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package app

import (
"errors"
"os"
"testing"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"

"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/idmanager"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
Expand Down
14 changes: 12 additions & 2 deletions pkg/app/conn.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app

import (
"errors"
"net"
"sync"
"time"

"github.com/pkg/errors"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
)

Expand All @@ -21,6 +20,7 @@ type Conn struct {
freeConnMx sync.RWMutex
}

// Read reads from connection.
func (c *Conn) Read(b []byte) (int, error) {
n, err := c.rpc.Read(c.id, b)
if err != nil {
Expand All @@ -30,10 +30,12 @@ func (c *Conn) Read(b []byte) (int, error) {
return n, err
}

// Write writes to connection.
func (c *Conn) Write(b []byte) (int, error) {
return c.rpc.Write(c.id, b)
}

// Close closes connection.
func (c *Conn) Close() error {
c.freeConnMx.RLock()
defer c.freeConnMx.RUnlock()
Expand All @@ -48,22 +50,30 @@ func (c *Conn) Close() error {
return nil
}

// LocalAddr returns local address of connection.
func (c *Conn) LocalAddr() net.Addr {
return c.local
}

// RemoteAddr returns remote address of connection.
func (c *Conn) RemoteAddr() net.Addr {
return c.remote
}

// SetDeadline sets read and write deadlines for connection.
// TODO: implement
func (c *Conn) SetDeadline(t time.Time) error {
return errMethodNotImplemented
}

// SetReadDeadline sets read deadline for connection.
// TODO: implement
func (c *Conn) SetReadDeadline(t time.Time) error {
return errMethodNotImplemented
}

// SetWriteDeadline sets write deadline for connection.
// TODO: implement
func (c *Conn) SetWriteDeadline(t time.Time) error {
return errMethodNotImplemented
}
4 changes: 2 additions & 2 deletions pkg/app/idmanager/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAssertListener(t *testing.T) {

l, err := AssertListener(ifc)
require.Error(t, err)
require.True(t, "wrong type of value stored for listener" == err.Error())
require.EqualError(t, err, "wrong type of value stored for listener")
require.Nil(t, l)
})
}
Expand All @@ -41,7 +41,7 @@ func TestAssertConn(t *testing.T) {

conn, err := AssertConn(ifc)
require.Error(t, err)
require.True(t, "wrong type of value stored for conn" == err.Error())
require.EqualError(t, err, "wrong type of value stored for conn")
require.Nil(t, conn)
})
}
3 changes: 3 additions & 0 deletions pkg/app/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Listener struct {
freeLisMx sync.RWMutex
}

// 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)
Expand Down Expand Up @@ -60,6 +61,7 @@ func (l *Listener) Accept() (net.Conn, error) {
return conn, nil
}

// Close closes listener.
func (l *Listener) Close() error {
l.freeLisMx.RLock()
defer l.freeLisMx.RUnlock()
Expand Down Expand Up @@ -92,6 +94,7 @@ func (l *Listener) Close() error {
return nil
}

// Addr returns address listener listens on.
func (l *Listener) Addr() net.Addr {
return l.addr
}
3 changes: 1 addition & 2 deletions pkg/app/listener_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app

import (
"errors"
"testing"

"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/pkg/errors"

"github.com/stretchr/testify/require"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package app

import (
"context"
"errors"
"net"
"net/rpc"
"testing"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"
Expand Down
14 changes: 10 additions & 4 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ func (timeoutError) Error() string { return "timeout" }
func (timeoutError) Timeout() bool { return true }
func (timeoutError) Temporary() bool { return true }

// RouteGroupConfig configures RouteGroup.
type RouteGroupConfig struct {
ReadChBufSize int
KeepAliveInterval time.Duration
}

// DefaultRouteGroupConfig returns default RouteGroup config.
// Used by default if config is nil.
func DefaultRouteGroupConfig() *RouteGroupConfig {
return &RouteGroupConfig{
KeepAliveInterval: defaultRouteGroupKeepAliveInterval,
Expand Down Expand Up @@ -86,6 +89,7 @@ type RouteGroup struct {
writeDeadline deadline.PipeDeadline
}

// NewRouteGroup creates a new RouteGroup.
func NewRouteGroup(cfg *RouteGroupConfig, rt routing.Table, desc routing.RouteDescriptor) *RouteGroup {
if cfg == nil {
cfg = DefaultRouteGroupConfig()
Expand Down Expand Up @@ -187,6 +191,7 @@ func (r *RouteGroup) Write(p []byte) (n int, err error) {
packet := routing.MakeDataPacket(rule.KeyRouteID(), p)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

errCh := make(chan error)

Expand All @@ -203,10 +208,8 @@ func (r *RouteGroup) Write(p []byte) (n int, err error) {

select {
case <-r.writeDeadline.Wait():
cancel()
return 0, timeoutError{}
case <-timeout.C:
cancel()
return 0, io.EOF
case err := <-errCh:
if err != nil {
Expand Down Expand Up @@ -255,16 +258,17 @@ func (r *RouteGroup) Close() error {
return nil
}

// LocalAddr returns destination address of underlying RouteDescriptor.
func (r *RouteGroup) LocalAddr() net.Addr {
return r.desc.Dst()
}

// RemoteAddr returns source address of underlying RouteDescriptor.
func (r *RouteGroup) RemoteAddr() net.Addr {
return r.desc.Src()
}

// https://golang.org/src/internal/poll/fd_plan9.go#L103
// https://golang.org/src/internal/poll/fd_poll_runtime.go#L126
// SetDeadline sets both read and write deadlines.
func (r *RouteGroup) SetDeadline(t time.Time) error {
if err := r.SetReadDeadline(t); err != nil {
return err
Expand All @@ -273,11 +277,13 @@ func (r *RouteGroup) SetDeadline(t time.Time) error {
return r.SetWriteDeadline(t)
}

// SetReadDeadline sets read deadline.
func (r *RouteGroup) SetReadDeadline(t time.Time) error {
r.readDeadline.Set(t)
return nil
}

// SetWriteDeadline sets write deadline.
func (r *RouteGroup) SetWriteDeadline(t time.Time) error {
r.writeDeadline.Set(t)
return nil
Expand Down
7 changes: 6 additions & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ func (c *Config) SetDefaults() {
}
}

// DialOptions describes dial options.
type DialOptions struct {
MinForwardRts int
MaxForwardRts int
MinConsumeRts int
MaxConsumeRts int
}

// DefaultDialOptions returns default dial options.
// Used by default if nil is passed as options.
func DefaultDialOptions() *DialOptions {
return &DialOptions{
MinForwardRts: 1,
Expand All @@ -72,6 +75,8 @@ func DefaultDialOptions() *DialOptions {
}
}

// Router is responsible for creating and keeping track of routes.
// Internally, it uses the routing table, route finder client and setup client.
type Router interface {
io.Closer

Expand Down Expand Up @@ -478,7 +483,7 @@ func (r *router) RemoveRouteDescriptor(desc routing.RouteDescriptor) {
func (r *router) fetchBestRoutes(src, dst cipher.PubKey, opts *DialOptions) (fwd, rev routing.Path, err error) {
// TODO(nkryuchkov): use opts
if opts == nil {
opts = DefaultDialOptions()
opts = DefaultDialOptions() // nolint
}

r.logger.Infof("Requesting new routes from %s to %s", src, dst)
Expand Down
1 change: 1 addition & 0 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Test_router_AcceptRoutes(t *testing.T) {
desc := routing.NewRouteDescriptor(srcPK, dstPK, srcPort, dstPort)

dstRtIDs, err := r0.ReserveKeys(2)
require.NoError(t, err)

fwdRule := routing.ForwardRule(1*time.Hour, dstRtIDs[0], routing.RouteID(3), uuid.UUID{}, keys[0].PK, keys[1].PK, 4, 5)
cnsmRule := routing.ConsumeRule(1*time.Hour, dstRtIDs[1], keys[1].PK, keys[0].PK, 5, 4)
Expand Down
5 changes: 0 additions & 5 deletions pkg/router/routerclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package routerclient

import (
"context"
"fmt"
"net/rpc"

"github.com/SkycoinProject/dmsg/cipher"
Expand All @@ -20,15 +19,11 @@ type Client struct {

// NewClient creates a new Client.
func NewClient(ctx context.Context, dialer snet.Dialer, pk cipher.PubKey) (*Client, error) {
fmt.Printf("Dialing to %s\n", pk) // TODO: remove debug logging

s, err := dialer.Dial(ctx, pk, snet.AwaitSetupPort)
if err != nil {
return nil, err
}

fmt.Printf("Dialed to %s\n", pk) // TODO: remove debug logging

client := &Client{
rpc: rpc.NewClient(s),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/routerclient/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
)

// TODO: remove this
// TODO(nkryuchkov): remove this
// dmsgClientWrapper is a temporary workaround to make dmsg client implement `snet.Dialer`.
// The only reason to use this is because client's `Dial` returns `*dmsg.Stream` instead of `net.Conn`,
// so this stuff should be removed as soon as the func's signature changes
Expand Down
Loading

0 comments on commit 29ff199

Please sign in to comment.