Skip to content

Commit

Permalink
Merge pull request #398 from ivcosla/feature/route-finder-config-timeout
Browse files Browse the repository at this point in the history
timeout config optionn for route finder
  • Loading branch information
jdknives authored Jun 7, 2019
2 parents f77e6f4 + 22765db commit bcae8da
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/skywire-cli/commands/node/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
5 changes: 4 additions & 1 deletion cmd/skywire-cli/commands/rtfind/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rtfind

import (
"fmt"
"time"

"github.com/spf13/cobra"

Expand All @@ -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.
Expand All @@ -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]))
Expand Down
7 changes: 4 additions & 3 deletions pkg/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions pkg/route-finder/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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)

Expand Down

0 comments on commit bcae8da

Please sign in to comment.