diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-11-12 21:37:57 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-11-12 22:07:01 +0900 |
commit | 9cad94f3bab7c7fe0f87ab5b76a3bcf5a404c25e (patch) | |
tree | bda2c71ac22a866f1f4cb52dcca87cdee5c97826 | |
parent | 0eb3dcede55433facbeab23e753d3ab991b5e33b (diff) |
cmd/gobgp: don't use timeout context for requests
timeout context is for only connecting. We should refactor the code to
avoid global context variant.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | cmd/gobgp/common.go | 11 | ||||
-rw-r--r-- | cmd/gobgp/root.go | 5 |
2 files changed, 8 insertions, 8 deletions
diff --git a/cmd/gobgp/common.go b/cmd/gobgp/common.go index b7a04f4b..add3d811 100644 --- a/cmd/gobgp/common.go +++ b/cmd/gobgp/common.go @@ -23,6 +23,7 @@ import ( "net" "os" "strconv" + "time" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -176,7 +177,7 @@ func extractReserved(args []string, keys map[string]int) (map[string][]string, e return m, nil } -func newClient(ctx context.Context) (api.GobgpApiClient, error) { +func newClient(ctx context.Context) (api.GobgpApiClient, context.CancelFunc, error) { grpcOpts := []grpc.DialOption{grpc.WithBlock()} if globalOpts.TLS { var creds credentials.TransportCredentials @@ -198,12 +199,12 @@ func newClient(ctx context.Context) (api.GobgpApiClient, error) { if target == "" { target = ":50051" } - - conn, err := grpc.DialContext(ctx, target, grpcOpts...) + cc, cancel := context.WithTimeout(ctx, time.Second) + conn, err := grpc.DialContext(cc, target, grpcOpts...) if err != nil { - return nil, err + return nil, cancel, err } - return api.NewGobgpApiClient(conn), nil + return api.NewGobgpApiClient(conn), cancel, nil } func addr2AddressFamily(a net.IP) *api.Family { diff --git a/cmd/gobgp/root.go b/cmd/gobgp/root.go index b8cfef1e..37555e3d 100644 --- a/cmd/gobgp/root.go +++ b/cmd/gobgp/root.go @@ -20,7 +20,6 @@ import ( "fmt" "net/http" _ "net/http/pprof" - "time" api "github.com/osrg/gobgp/api" "github.com/spf13/cobra" @@ -58,8 +57,8 @@ func newRootCmd() *cobra.Command { if !globalOpts.GenCmpl { var err error - ctx, cancel = context.WithTimeout(context.Background(), time.Second) - client, err = newClient(ctx) + ctx = context.Background() + client, cancel, err = newClient(ctx) if err != nil { cancel() exitWithError(err) |