From 22765dbbc2055de4dcd29feb761becb094580fe0 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Fri, 7 Jun 2019 13:40:49 +0200 Subject: [PATCH] timeout config optionn for route finder --- cmd/skywire-cli/commands/node/gen-config.go | 1 + cmd/skywire-cli/commands/rtfind/root.go | 5 ++++- pkg/node/config.go | 7 ++++--- pkg/node/node.go | 2 +- pkg/route-finder/client/client.go | 20 +++++++++++++------- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cmd/skywire-cli/commands/node/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go index e0379fd5e5..c7e88ab89a 100644 --- a/cmd/skywire-cli/commands/node/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -103,6 +103,7 @@ func defaultConfig() *node.Config { conf.Routing.SetupNodes = []cipher.PubKey{sPK} conf.Routing.Table.Type = "boltdb" conf.Routing.Table.Location = "./skywire/routing.db" + conf.Routing.RouteFinderTimeout = node.Duration(10 * time.Second) conf.ManagerNodes = []node.ManagerConfig{} diff --git a/cmd/skywire-cli/commands/rtfind/root.go b/cmd/skywire-cli/commands/rtfind/root.go index fcb0bfeda8..cc2c9e8596 100644 --- a/cmd/skywire-cli/commands/rtfind/root.go +++ b/cmd/skywire-cli/commands/rtfind/root.go @@ -2,6 +2,7 @@ package rtfind import ( "fmt" + "time" "github.com/spf13/cobra" @@ -12,11 +13,13 @@ import ( var frAddr string var frMinHops, frMaxHops uint16 +var timeout time.Duration func init() { RootCmd.Flags().StringVar(&frAddr, "addr", "https://routefinder.skywire.skycoin.net", "address in which to contact route finder service") RootCmd.Flags().Uint16Var(&frMinHops, "min-hops", 1, "min hops for the returning routeFinderRoutesCmd") RootCmd.Flags().Uint16Var(&frMaxHops, "max-hops", 1000, "max hops for the returning routeFinderRoutesCmd") + RootCmd.Flags().DurationVar(&timeout, "timeout", 10*time.Second, "timeout for remote server requests") } // RootCmd is the command that queries the route-finder. @@ -25,7 +28,7 @@ var RootCmd = &cobra.Command{ Short: "Queries the Route Finder for available routes between two nodes", Args: cobra.MinimumNArgs(2), Run: func(_ *cobra.Command, args []string) { - rfc := client.NewHTTP(frAddr) + rfc := client.NewHTTP(frAddr, timeout) var srcPK, dstPK cipher.PubKey internal.Catch(srcPK.Set(args[0])) diff --git a/pkg/node/config.go b/pkg/node/config.go index 5475e44832..b6b358cc1c 100644 --- a/pkg/node/config.go +++ b/pkg/node/config.go @@ -40,9 +40,10 @@ type Config struct { } `json:"transport"` Routing struct { - SetupNodes []cipher.PubKey `json:"setup_nodes"` - RouteFinder string `json:"route_finder"` - Table struct { + SetupNodes []cipher.PubKey `json:"setup_nodes"` + RouteFinder string `json:"route_finder"` + RouteFinderTimeout Duration `json:"route_finder_timeout"` + Table struct { Type string `json:"type"` Location string `json:"location"` } `json:"table"` diff --git a/pkg/node/node.go b/pkg/node/node.go index 24c829ab5e..4e5c4775d5 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -149,7 +149,7 @@ func NewNode(config *Config) (*Node, error) { SecKey: sk, TransportManager: node.tm, RoutingTable: node.rt, - RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder), + RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)), SetupNodes: config.Routing.SetupNodes, } r := router.New(rConfig) diff --git a/pkg/route-finder/client/client.go b/pkg/route-finder/client/client.go index d11813cb99..d80abd8167 100644 --- a/pkg/route-finder/client/client.go +++ b/pkg/route-finder/client/client.go @@ -15,7 +15,7 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) -const contextTimeout = 10 * time.Second +const defaultContextTimeout = 10 * time.Second // GetRoutesRequest parses json body for /routes endpoint request type GetRoutesRequest struct { @@ -50,15 +50,21 @@ type Client interface { // APIClient implements Client interface type apiClient struct { - addr string - client http.Client + addr string + client http.Client + apiTimeout time.Duration } // NewHTTP constructs new Client that communicates over http. -func NewHTTP(addr string) Client { +func NewHTTP(addr string, apiTimeout time.Duration) Client { + if apiTimeout == 0 { + apiTimeout = defaultContextTimeout + } + return &apiClient{ - addr: sanitizedAddr(addr), - client: http.Client{}, + addr: sanitizedAddr(addr), + client: http.Client{}, + apiTimeout: apiTimeout, } } @@ -81,7 +87,7 @@ func (c *apiClient) PairedRoutes(source, destiny cipher.PubKey, minHops, maxHops return nil, nil, err } req.Header.Set("Content-Type", "application/json") - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.apiTimeout) defer cancel() req = req.WithContext(ctx)