From b6f9d62c62109ca9e683d585c0b4d9a992861a41 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Mon, 11 Nov 2019 20:50:15 +0300 Subject: [PATCH 1/2] Fix timeouts for proxy client --- .../therealproxy-client/therealproxy-client.go | 11 ++++++++++- internal/skyenv/const.go | 17 ++++++++++++----- internal/therealproxy/client.go | 16 ++++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cmd/apps/therealproxy-client/therealproxy-client.go b/cmd/apps/therealproxy-client/therealproxy-client.go index 53cf3ff45..643d55ff8 100644 --- a/cmd/apps/therealproxy-client/therealproxy-client.go +++ b/cmd/apps/therealproxy-client/therealproxy-client.go @@ -6,6 +6,7 @@ package main import ( "flag" "net" + "time" "github.com/SkycoinProject/dmsg/cipher" @@ -18,9 +19,11 @@ import ( func main() { log := app.NewLogger(skyenv.SkyproxyClientName) therealproxy.Log = log.PackageLogger(skyenv.SkyproxyClientName) + defaultTimeout := skyenv.SkyproxyClientTimeout var addr = flag.String("addr", skyenv.SkyproxyClientAddr, "Client address to listen on") var serverPK = flag.String("srv", "", "PubKey of the server to connect to") + var timeout = flag.String("timeout", defaultTimeout.String(), "Connection timeout duration") flag.Parse() config := &app.Config{AppName: skyenv.SkyproxyClientName, AppVersion: skyenv.SkyproxyClientVersion, ProtocolVersion: skyenv.AppProtocolVersion} @@ -51,7 +54,13 @@ func main() { remote := routing.Addr{PubKey: pk, Port: routing.Port(skyenv.SkyproxyPort)} - client, err := therealproxy.NewClient(l, socksApp, remote) + clientTimeout, err := time.ParseDuration(*timeout) + if err != nil { + log.Warnf("Bad duration format, using default (%v)", defaultTimeout) + clientTimeout = defaultTimeout + } + + client, err := therealproxy.NewClient(clientTimeout, l, socksApp, remote) if err != nil { log.Fatal("Failed to create a new client: ", err) } diff --git a/internal/skyenv/const.go b/internal/skyenv/const.go index 25014054e..710feffbb 100644 --- a/internal/skyenv/const.go +++ b/internal/skyenv/const.go @@ -1,5 +1,9 @@ package skyenv +import ( + "time" +) + // Constants for default services. const ( DefaultTpDiscAddr = "http://transport.discovery.skywire.skycoin.com" @@ -45,9 +49,12 @@ const ( SkyproxyVersion = "1.0" SkyproxyPort = uint16(3) - SkyproxyClientName = "socksproxy-client" - SkyproxyClientVersion = "1.0" - SkyproxyClientPort = uint16(13) - SkyproxyClientAddr = ":1080" - // TODO(evanlinjin): skyproxy-client requires + SkyproxyClientName = "socksproxy-client" + SkyproxyClientVersion = "1.0" + SkyproxyClientPort = uint16(13) + SkyproxyClientAddr = ":1080" + SkyproxyClientTimeout = 5 * time.Second + SkyproxyReconnectInterval = 1 * time.Second + SkyproxyRetryTimes = 0 + SkyproxyRetryFactor = 1 ) diff --git a/internal/therealproxy/client.go b/internal/therealproxy/client.go index b0135dc1f..0000133f2 100644 --- a/internal/therealproxy/client.go +++ b/internal/therealproxy/client.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/yamux" "github.com/SkycoinProject/skywire-mainnet/internal/netutil" + "github.com/SkycoinProject/skywire-mainnet/internal/skyenv" "github.com/SkycoinProject/skywire-mainnet/pkg/app" "github.com/SkycoinProject/skywire-mainnet/pkg/routing" ) @@ -19,6 +20,7 @@ var Log = logging.MustGetLogger("therealproxy") // Client implement multiplexing proxy client using yamux. type Client struct { + timeout time.Duration session *yamux.Session listener net.Listener app *app.App @@ -26,8 +28,9 @@ type Client struct { } // NewClient constructs a new Client. -func NewClient(lis net.Listener, app *app.App, addr routing.Addr) (*Client, error) { +func NewClient(timeout time.Duration, lis net.Listener, app *app.App, addr routing.Addr) (*Client, error) { c := &Client{ + timeout: timeout, listener: lis, app: app, addr: addr, @@ -40,7 +43,7 @@ func NewClient(lis net.Listener, app *app.App, addr routing.Addr) (*Client, erro } func (c *Client) connect() error { - r := netutil.NewRetrier(time.Second, 0, 1) + r := netutil.NewRetrier(skyenv.SkyproxyReconnectInterval, skyenv.SkyproxyRetryTimes, skyenv.SkyproxyRetryFactor) var conn net.Conn err := r.Do(func() error { @@ -52,7 +55,12 @@ func (c *Client) connect() error { return fmt.Errorf("failed to dial to a server: %v", err) } - session, err := yamux.Client(conn, nil) + // If connection fails, yamux client doesn't wait, fails early and reconnects. + yamuxCfg := yamux.DefaultConfig() + yamuxCfg.KeepAliveInterval = c.timeout + yamuxCfg.ConnectionWriteTimeout = c.timeout + + session, err := yamux.Client(conn, yamuxCfg) if err != nil { return fmt.Errorf("failed to create client: %s", err) } @@ -84,7 +92,7 @@ func (c *Client) createStream() net.Conn { Log.Warnf("Failed to open yamux session: %v", err) - delay := 1 * time.Second + delay := skyenv.SkyproxyReconnectInterval Log.Warnf("Restarting in %v", delay) time.Sleep(delay) From a6324ae48d187ee812cb0be9f1bd29e00f23f54b Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 12 Nov 2019 04:18:16 +0300 Subject: [PATCH 2/2] Fix argument order --- cmd/apps/therealproxy-client/therealproxy-client.go | 2 +- internal/therealproxy/client.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/apps/therealproxy-client/therealproxy-client.go b/cmd/apps/therealproxy-client/therealproxy-client.go index 643d55ff8..96c9079cb 100644 --- a/cmd/apps/therealproxy-client/therealproxy-client.go +++ b/cmd/apps/therealproxy-client/therealproxy-client.go @@ -60,7 +60,7 @@ func main() { clientTimeout = defaultTimeout } - client, err := therealproxy.NewClient(clientTimeout, l, socksApp, remote) + client, err := therealproxy.NewClient(l, socksApp, remote, clientTimeout) if err != nil { log.Fatal("Failed to create a new client: ", err) } diff --git a/internal/therealproxy/client.go b/internal/therealproxy/client.go index 0000133f2..655051f2f 100644 --- a/internal/therealproxy/client.go +++ b/internal/therealproxy/client.go @@ -20,20 +20,20 @@ var Log = logging.MustGetLogger("therealproxy") // Client implement multiplexing proxy client using yamux. type Client struct { - timeout time.Duration - session *yamux.Session listener net.Listener app *app.App addr routing.Addr + timeout time.Duration + session *yamux.Session } // NewClient constructs a new Client. -func NewClient(timeout time.Duration, lis net.Listener, app *app.App, addr routing.Addr) (*Client, error) { +func NewClient(lis net.Listener, app *app.App, addr routing.Addr, timeout time.Duration) (*Client, error) { c := &Client{ - timeout: timeout, listener: lis, app: app, addr: addr, + timeout: timeout, } if err := c.connect(); err != nil { return nil, err