From 5f5ce7ba9f45c550d1365a967f359c2af74a4431 Mon Sep 17 00:00:00 2001 From: Hitoshi Mitake Date: Mon, 14 Sep 2015 18:20:48 +0900 Subject: [PATCH] etcdctl: use user specified timeout value for entire RPC etcdctl should use a user specified timeout value. This commit adds a new option --total-timeout to the command. The value passed via this option is used as a timeout value of entire RPC (API call of client). Fixes https://github.com/coreos/etcd/issues/3517 --- etcdctl/command/auth_commands.go | 2 +- etcdctl/command/member_commands.go | 13 ++++++------- etcdctl/command/role_commands.go | 14 +++++++------- etcdctl/command/user_commands.go | 18 +++++++++--------- etcdctl/command/util.go | 2 +- etcdctl/main.go | 1 + 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/etcdctl/command/auth_commands.go b/etcdctl/command/auth_commands.go index 2ee483fab7e..4724847537a 100644 --- a/etcdctl/command/auth_commands.go +++ b/etcdctl/command/auth_commands.go @@ -67,7 +67,7 @@ func authEnableDisable(c *cli.Context, enable bool) { os.Exit(1) } s := mustNewAuthAPI(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) var err error if enable { err = s.Enable(ctx) diff --git a/etcdctl/command/member_commands.go b/etcdctl/command/member_commands.go index be59886bd7a..f1d5fe2d4ed 100644 --- a/etcdctl/command/member_commands.go +++ b/etcdctl/command/member_commands.go @@ -21,7 +21,6 @@ import ( "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" - "github.com/coreos/etcd/client" ) func NewMemberCommand() cli.Command { @@ -59,7 +58,7 @@ func actionMemberList(c *cli.Context) { os.Exit(1) } mAPI := mustNewMembersAPI(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) members, err := mAPI.List(ctx) cancel() if err != nil { @@ -86,7 +85,7 @@ func actionMemberAdd(c *cli.Context) { mAPI := mustNewMembersAPI(c) url := args[1] - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) m, err := mAPI.Add(ctx, url) cancel() if err != nil { @@ -98,7 +97,7 @@ func actionMemberAdd(c *cli.Context) { newName := args[0] fmt.Printf("Added member named %s with ID %s to cluster\n", newName, newID) - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) members, err := mAPI.List(ctx) cancel() if err != nil { @@ -133,7 +132,7 @@ func actionMemberRemove(c *cli.Context) { mAPI := mustNewMembersAPI(c) // Get the list of members. - listctx, listCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + listctx, listCancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) members, err := mAPI.List(listctx) listCancel() if err != nil { @@ -158,7 +157,7 @@ func actionMemberRemove(c *cli.Context) { } // Actually attempt to remove the member. - ctx, removeCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, removeCancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err = mAPI.Remove(ctx, removalID) removeCancel() if err != nil { @@ -180,7 +179,7 @@ func actionMemberUpdate(c *cli.Context) { mid := args[0] urls := args[1] - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err := mAPI.Update(ctx, mid, strings.Split(urls, ",")) cancel() if err != nil { diff --git a/etcdctl/command/role_commands.go b/etcdctl/command/role_commands.go index 2b3ed240111..100b34254aa 100644 --- a/etcdctl/command/role_commands.go +++ b/etcdctl/command/role_commands.go @@ -93,7 +93,7 @@ func actionRoleList(c *cli.Context) { os.Exit(1) } r := mustNewAuthRoleAPI(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) roles, err := r.ListRoles(ctx) cancel() if err != nil { @@ -108,14 +108,14 @@ func actionRoleList(c *cli.Context) { func actionRoleAdd(c *cli.Context) { api, role := mustRoleAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) currentRole, err := api.GetRole(ctx, role) cancel() if currentRole != nil { fmt.Fprintf(os.Stderr, "Role %s already exists\n", role) os.Exit(1) } - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err = api.AddRole(ctx, role) cancel() if err != nil { @@ -128,7 +128,7 @@ func actionRoleAdd(c *cli.Context) { func actionRoleRemove(c *cli.Context) { api, role := mustRoleAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err := api.RemoveRole(ctx, role) cancel() if err != nil { @@ -182,14 +182,14 @@ func roleGrantRevoke(c *cli.Context, grant bool) { } api, role := mustRoleAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) currentRole, err := api.GetRole(ctx, role) cancel() if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) var newRole *client.Role if grant { newRole, err = api.GrantRoleKV(ctx, role, []string{path}, permType) @@ -215,7 +215,7 @@ func roleGrantRevoke(c *cli.Context, grant bool) { func actionRoleGet(c *cli.Context) { api, rolename := mustRoleAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) role, err := api.GetRole(ctx, rolename) cancel() if err != nil { diff --git a/etcdctl/command/user_commands.go b/etcdctl/command/user_commands.go index 5596fe78b6b..6cb1d3ef7ad 100644 --- a/etcdctl/command/user_commands.go +++ b/etcdctl/command/user_commands.go @@ -89,7 +89,7 @@ func actionUserList(c *cli.Context) { os.Exit(1) } u := mustNewAuthUserAPI(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) users, err := u.ListUsers(ctx) cancel() if err != nil { @@ -104,7 +104,7 @@ func actionUserList(c *cli.Context) { func actionUserAdd(c *cli.Context) { api, user := mustUserAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) currentUser, err := api.GetUser(ctx, user) cancel() if currentUser != nil { @@ -116,7 +116,7 @@ func actionUserAdd(c *cli.Context) { fmt.Fprintln(os.Stderr, "Error reading password:", err) os.Exit(1) } - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err = api.AddUser(ctx, user, pass) cancel() if err != nil { @@ -129,7 +129,7 @@ func actionUserAdd(c *cli.Context) { func actionUserRemove(c *cli.Context) { api, user := mustUserAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err := api.RemoveUser(ctx, user) cancel() if err != nil { @@ -142,7 +142,7 @@ func actionUserRemove(c *cli.Context) { func actionUserPasswd(c *cli.Context) { api, user := mustUserAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) currentUser, err := api.GetUser(ctx, user) cancel() if currentUser == nil { @@ -155,7 +155,7 @@ func actionUserPasswd(c *cli.Context) { os.Exit(1) } - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) _, err = api.ChangePassword(ctx, user, pass) cancel() if err != nil { @@ -182,7 +182,7 @@ func userGrantRevoke(c *cli.Context, grant bool) { } api, user := mustUserAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) currentUser, err := api.GetUser(ctx, user) cancel() if currentUser == nil { @@ -190,7 +190,7 @@ func userGrantRevoke(c *cli.Context, grant bool) { os.Exit(1) } - ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel = context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) var newUser *client.User if grant { newUser, err = api.GrantUser(ctx, user, roles) @@ -217,7 +217,7 @@ func userGrantRevoke(c *cli.Context, grant bool) { func actionUserGet(c *cli.Context) { api, username := mustUserAPIAndName(c) - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) user, err := api.GetUser(ctx, username) cancel() if err != nil { diff --git a/etcdctl/command/util.go b/etcdctl/command/util.go index 0e9b5155838..ed615e70e0e 100644 --- a/etcdctl/command/util.go +++ b/etcdctl/command/util.go @@ -196,7 +196,7 @@ func mustNewClient(c *cli.Context) client.Client { if debug { fmt.Fprintf(os.Stderr, "start to sync cluster using endpoints(%s)\n", strings.Join(hc.Endpoints(), ",")) } - ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) err := hc.Sync(ctx) cancel() if err != nil { diff --git a/etcdctl/main.go b/etcdctl/main.go index da30af69c21..5854bdbfab6 100644 --- a/etcdctl/main.go +++ b/etcdctl/main.go @@ -40,6 +40,7 @@ func main() { cli.StringFlag{Name: "ca-file", Value: "", Usage: "verify certificates of HTTPS-enabled servers using this CA bundle"}, cli.StringFlag{Name: "username, u", Value: "", Usage: "provide username[:password] and prompt if password is not supplied."}, cli.DurationFlag{Name: "timeout", Value: time.Second, Usage: "connection timeout per request"}, + cli.DurationFlag{Name: "total-timeout", Value: time.Second * 5, Usage: "timeout of entire RPC execution"}, } app.Commands = []cli.Command{ command.NewBackupCommand(),