Skip to content

Commit

Permalink
add transport timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ivcosla committed Mar 7, 2019
1 parent 178a724 commit bae94cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cmd/skywire-cli/commands/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"strings"
"time"

"github.com/google/uuid"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,6 +54,7 @@ func makeTransportsCmds() *cobra.Command {
logs bool
transportType string
public bool
timeout time.Duration
)

tabPrint := func(trList ...*node.TransportSummary) {
Expand Down Expand Up @@ -95,7 +97,7 @@ func makeTransportsCmds() *cobra.Command {
catch(pk.Set(args[0]))

tr, err := client().AddTransport(pk,
transportType, public)
transportType, public, timeout)

catch(err)

Expand All @@ -106,6 +108,7 @@ func makeTransportsCmds() *cobra.Command {
"type of the transport to add")
add.Flags().BoolVar(&public, "public", true,
"whether to add the transport as public or private")
add.Flags().DurationVarP(&timeout, "timeout", "t", 0, "specifies the timeout; no timeout if 0")
c.AddCommand(add)

list := &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (m *Node) postTransport() http.HandlerFunc {
httputil.WriteJSON(w, r, http.StatusBadRequest, err)
return
}
summary, err := ctx.RPC.AddTransport(reqBody.Remote, reqBody.TpType, reqBody.Public)
summary, err := ctx.RPC.AddTransport(reqBody.Remote, reqBody.TpType, reqBody.Public, 30*time.Second) // TODO(evanlinjin): add timeout
if err != nil {
httputil.WriteJSON(w, r, http.StatusInternalServerError, err)
return
Expand Down
5 changes: 3 additions & 2 deletions pkg/manager/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ func (rc *rpcClient) Transport(tid uuid.UUID) (*node.TransportSummary, error) {
}

// AddTransport calls AddTransport.
func (rc *rpcClient) AddTransport(remote cipher.PubKey, tpType string, public bool) (*node.TransportSummary, error) {
func (rc *rpcClient) AddTransport(remote cipher.PubKey, tpType string, public bool, timeout time.Duration) (*node.TransportSummary, error) {
var summary node.TransportSummary
err := rc.Call("AddTransport", &node.AddTransportIn{
RemotePK: remote,
TpType: tpType,
Public: public,
Timeout: timeout,
}, &summary)
return &summary, err
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func (mc *mockRPCClient) Transport(tid uuid.UUID) (*node.TransportSummary, error
}

// AddTransport implements RPCClient.
func (mc *mockRPCClient) AddTransport(remote cipher.PubKey, tpType string, public bool) (*node.TransportSummary, error) {
func (mc *mockRPCClient) AddTransport(remote cipher.PubKey, tpType string, public bool, _ time.Duration) (*node.TransportSummary, error) {
summary := &node.TransportSummary{
ID: uuid.New(),
Local: mc.s.PubKey,
Expand Down
12 changes: 9 additions & 3 deletions pkg/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type RPCClient interface {
TransportTypes() ([]string, error)
Transports(types []string, pks []cipher.PubKey, logs bool) ([]*TransportSummary, error)
Transport(tid uuid.UUID) (*TransportSummary, error)
AddTransport(remote cipher.PubKey, tpType string, public bool) (*TransportSummary, error)
AddTransport(remote cipher.PubKey, tpType string, public bool, timeout time.Duration) (*TransportSummary, error)
RemoveTransport(tid uuid.UUID) error

RoutingRules() ([]*RoutingEntry, error)
Expand Down Expand Up @@ -202,12 +202,18 @@ type AddTransportIn struct {
RemotePK cipher.PubKey
TpType string
Public bool
Timeout time.Duration
}

// AddTransport creates a transport for the node.
func (r *RPC) AddTransport(in *AddTransportIn, out *TransportSummary) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
ctx := context.Background()
if in.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*20)
defer cancel()
}

tp, err := r.node.tm.CreateTransport(ctx, in.RemotePK, in.TpType, in.Public)
if err != nil {
return err
Expand Down

0 comments on commit bae94cd

Please sign in to comment.