Skip to content

Commit 5b6b03d

Browse files
committed
etcdctl: add timeout to snapshot save command
snapshot save command by default has no timeout, but respects user specified command timeout.
1 parent 7f450bf commit 5b6b03d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

etcdctl/ctlv3/command/snapshot_command.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
102102
sp := snapshot.NewV3(lg)
103103
cfg := mustClientCfgFromCmd(cmd)
104104

105+
// if user does not specify "--command-timeout" flag, there will be no timeout for snapshot save command
106+
ctx, cancel := context.WithCancel(context.Background())
107+
if isCommandTimeoutFlagSet(cmd) {
108+
ctx, cancel = commandCtx(cmd)
109+
}
110+
defer cancel()
111+
105112
path := args[0]
106-
if err := sp.Save(context.TODO(), *cfg, path); err != nil {
113+
if err := sp.Save(ctx, *cfg, path); err != nil {
107114
ExitWithError(ExitInterrupted, err)
108115
}
109116
fmt.Printf("Snapshot saved at %s\n", path)

etcdctl/ctlv3/command/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ func commandCtx(cmd *cobra.Command) (context.Context, context.CancelFunc) {
8282
return context.WithTimeout(context.Background(), timeOut)
8383
}
8484

85+
func isCommandTimeoutFlagSet(cmd *cobra.Command) bool {
86+
commandTimeoutFlag := cmd.Flags().Lookup("command-timeout")
87+
if commandTimeoutFlag == nil {
88+
panic("expect command-timeout flag to exist")
89+
}
90+
return commandTimeoutFlag.Changed
91+
}
92+
8593
// get the process_resident_memory_bytes from <server:2379>/metrics
8694
func endpointMemoryMetrics(host string) float64 {
8795
residentMemoryKey := "process_resident_memory_bytes"

0 commit comments

Comments
 (0)