Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 044b1dd

Browse files
committed
use different timeouts for all vcs commands
both `runFromRepoDir` and `runFromCwd` accept now a timeout to control the timeouts of the underlying `monitoredCmd`, instead of having a hardcoded timeout of 2 minutes for all commands. This will allow better tuning of the timeouts on a per-command basis. Signed-off-by: Miguel Molina <[email protected]>
1 parent bcfa7ee commit 044b1dd

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

internal/gps/cmd.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,22 @@ func (e killCmdError) Error() string {
136136
return fmt.Sprintf("error killing command: %s", e.err)
137137
}
138138

139-
func runFromCwd(ctx context.Context, cmd string, args ...string) ([]byte, error) {
140-
c := newMonitoredCmd(exec.Command(cmd, args...), 2*time.Minute)
139+
func runFromCwd(ctx context.Context, timeout time.Duration, cmd string, args ...string) ([]byte, error) {
140+
c := newMonitoredCmd(exec.Command(cmd, args...), timeout)
141141
return c.combinedOutput(ctx)
142142
}
143143

144-
func runFromRepoDir(ctx context.Context, repo vcs.Repo, cmd string, args ...string) ([]byte, error) {
145-
c := newMonitoredCmd(repo.CmdFromDir(cmd, args...), 2*time.Minute)
144+
func runFromRepoDir(ctx context.Context, repo vcs.Repo, timeout time.Duration, cmd string, args ...string) ([]byte, error) {
145+
c := newMonitoredCmd(repo.CmdFromDir(cmd, args...), timeout)
146146
return c.combinedOutput(ctx)
147147
}
148+
149+
const (
150+
// expensiveCmdTimeout is meant to be used in a command that is expensive
151+
// in terms of computation and we know it will take long or one that uses
152+
// the network, such as clones, updates, ....
153+
expensiveCmdTimeout = 2 * time.Minute
154+
// defaultCmdTimeout is just an umbrella value for all other commands that
155+
// should not take much.
156+
defaultCmdTimeout = 10 * time.Second
157+
)

internal/gps/vcs_repo.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func newVcsLocalErrorOr(msg string, err error, out string) error {
8080
}
8181

8282
func (r *gitRepo) get(ctx context.Context) error {
83-
out, err := runFromCwd(ctx, "git", "clone", "--recursive", "-v", "--progress", r.Remote(), r.LocalPath())
83+
out, err := runFromCwd(ctx, expensiveCmdTimeout, "git", "clone", "--recursive", "-v", "--progress", r.Remote(), r.LocalPath())
8484
if err != nil {
8585
return newVcsRemoteErrorOr("unable to get repository", err, string(out))
8686
}
@@ -90,15 +90,15 @@ func (r *gitRepo) get(ctx context.Context) error {
9090

9191
func (r *gitRepo) fetch(ctx context.Context) error {
9292
// Perform a fetch to make sure everything is up to date.
93-
out, err := runFromRepoDir(ctx, r, "git", "fetch", "--tags", "--prune", r.RemoteLocation)
93+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "git", "fetch", "--tags", "--prune", r.RemoteLocation)
9494
if err != nil {
9595
return newVcsRemoteErrorOr("unable to update repository", err, string(out))
9696
}
9797
return nil
9898
}
9999

100100
func (r *gitRepo) updateVersion(ctx context.Context, v string) error {
101-
out, err := runFromRepoDir(ctx, r, "git", "checkout", v)
101+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "checkout", v)
102102
if err != nil {
103103
return newVcsLocalErrorOr("Unable to update checked out version", err, string(out))
104104
}
@@ -110,20 +110,20 @@ func (r *gitRepo) updateVersion(ctx context.Context, v string) error {
110110
// submodules. Or nested submodules. What a great idea, submodules.
111111
func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {
112112
// First, update them to whatever they should be, if there should happen to be any.
113-
out, err := runFromRepoDir(ctx, r, "git", "submodule", "update", "--init", "--recursive")
113+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "git", "submodule", "update", "--init", "--recursive")
114114
if err != nil {
115115
return newVcsLocalErrorOr("unexpected error while defensively updating submodules", err, string(out))
116116
}
117117

118118
// Now, do a special extra-aggressive clean in case changing versions caused
119119
// one or more submodules to go away.
120-
out, err = runFromRepoDir(ctx, r, "git", "clean", "-x", "-d", "-f", "-f")
120+
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "clean", "-x", "-d", "-f", "-f")
121121
if err != nil {
122122
return newVcsLocalErrorOr("unexpected error while defensively cleaning up after possible derelict submodule directories", err, string(out))
123123
}
124124

125125
// Then, repeat just in case there are any nested submodules that went away.
126-
out, err = runFromRepoDir(ctx, r, "git", "submodule", "foreach", "--recursive", "git", "clean", "-x", "-d", "-f", "-f")
126+
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "submodule", "foreach", "--recursive", "git", "clean", "-x", "-d", "-f", "-f")
127127
if err != nil {
128128
return newVcsLocalErrorOr("unexpected error while defensively cleaning up after possible derelict nested submodule directories", err, string(out))
129129
}
@@ -144,7 +144,7 @@ func (r *bzrRepo) get(ctx context.Context) error {
144144
}
145145
}
146146

147-
out, err := runFromCwd(ctx, "bzr", "branch", r.Remote(), r.LocalPath())
147+
out, err := runFromCwd(ctx, expensiveCmdTimeout, "bzr", "branch", r.Remote(), r.LocalPath())
148148
if err != nil {
149149
return newVcsRemoteErrorOr("unable to get repository", err, string(out))
150150
}
@@ -153,15 +153,15 @@ func (r *bzrRepo) get(ctx context.Context) error {
153153
}
154154

155155
func (r *bzrRepo) fetch(ctx context.Context) error {
156-
out, err := runFromRepoDir(ctx, r, "bzr", "pull")
156+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "bzr", "pull")
157157
if err != nil {
158158
return newVcsRemoteErrorOr("unable to update repository", err, string(out))
159159
}
160160
return nil
161161
}
162162

163163
func (r *bzrRepo) updateVersion(ctx context.Context, version string) error {
164-
out, err := runFromRepoDir(ctx, r, "bzr", "update", "-r", version)
164+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "bzr", "update", "-r", version)
165165
if err != nil {
166166
return newVcsLocalErrorOr("unable to update checked out version", err, string(out))
167167
}
@@ -173,7 +173,7 @@ type hgRepo struct {
173173
}
174174

175175
func (r *hgRepo) get(ctx context.Context) error {
176-
out, err := runFromCwd(ctx, "hg", "clone", r.Remote(), r.LocalPath())
176+
out, err := runFromCwd(ctx, expensiveCmdTimeout, "hg", "clone", r.Remote(), r.LocalPath())
177177
if err != nil {
178178
return newVcsRemoteErrorOr("unable to get repository", err, string(out))
179179
}
@@ -182,15 +182,15 @@ func (r *hgRepo) get(ctx context.Context) error {
182182
}
183183

184184
func (r *hgRepo) fetch(ctx context.Context) error {
185-
out, err := runFromRepoDir(ctx, r, "hg", "pull")
185+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "hg", "pull")
186186
if err != nil {
187187
return newVcsRemoteErrorOr("unable to fetch latest changes", err, string(out))
188188
}
189189
return nil
190190
}
191191

192192
func (r *hgRepo) updateVersion(ctx context.Context, version string) error {
193-
out, err := runFromRepoDir(ctx, r, "hg", "update", version)
193+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "hg", "update", version)
194194
if err != nil {
195195
return newVcsRemoteErrorOr("unable to update checked out version", err, string(out))
196196
}
@@ -210,7 +210,7 @@ func (r *svnRepo) get(ctx context.Context) error {
210210
remote = "file:///" + remote
211211
}
212212

213-
out, err := runFromCwd(ctx, "svn", "checkout", remote, r.LocalPath())
213+
out, err := runFromCwd(ctx, expensiveCmdTimeout, "svn", "checkout", remote, r.LocalPath())
214214
if err != nil {
215215
return newVcsRemoteErrorOr("unable to get repository", err, string(out))
216216
}
@@ -219,7 +219,7 @@ func (r *svnRepo) get(ctx context.Context) error {
219219
}
220220

221221
func (r *svnRepo) update(ctx context.Context) error {
222-
out, err := runFromRepoDir(ctx, r, "svn", "update")
222+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "svn", "update")
223223
if err != nil {
224224
return newVcsRemoteErrorOr("unable to update repository", err, string(out))
225225
}
@@ -228,7 +228,7 @@ func (r *svnRepo) update(ctx context.Context) error {
228228
}
229229

230230
func (r *svnRepo) updateVersion(ctx context.Context, version string) error {
231-
out, err := runFromRepoDir(ctx, r, "svn", "update", "-r", version)
231+
out, err := runFromRepoDir(ctx, r, expensiveCmdTimeout, "svn", "update", "-r", version)
232232
if err != nil {
233233
return newVcsRemoteErrorOr("unable to update checked out version", err, string(out))
234234
}
@@ -250,7 +250,7 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
250250
Commit commit `xml:"entry>commit"`
251251
}
252252

253-
out, err := runFromRepoDir(ctx, r, "svn", "info", "-r", id, "--xml")
253+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "svn", "info", "-r", id, "--xml")
254254
if err != nil {
255255
return nil, newVcsLocalErrorOr("unable to retrieve commit information", err, string(out))
256256
}
@@ -267,7 +267,7 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
267267
}
268268
}
269269

270-
out, err := runFromRepoDir(ctx, r, "svn", "log", "-r", id, "--xml")
270+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "svn", "log", "-r", id, "--xml")
271271
if err != nil {
272272
return nil, newVcsRemoteErrorOr("unable to retrieve commit information", err, string(out))
273273
}

internal/gps/vcs_source.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
136136
// could have an err here...but it's hard to imagine how?
137137
defer fs.RenameWithFallback(bak, idx)
138138

139-
out, err := runFromRepoDir(ctx, r, "git", "read-tree", rev.String())
139+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "read-tree", rev.String())
140140
if err != nil {
141141
return fmt.Errorf("%s: %s", out, err)
142142
}
@@ -152,7 +152,7 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
152152
// though we have a bunch of housekeeping to do to set up, then tear
153153
// down, the sparse checkout controls, as well as restore the original
154154
// index and HEAD.
155-
out, err = runFromRepoDir(ctx, r, "git", "checkout-index", "-a", "--prefix="+to)
155+
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "git", "checkout-index", "-a", "--prefix="+to)
156156
if err != nil {
157157
return fmt.Errorf("%s: %s", out, err)
158158
}
@@ -365,15 +365,15 @@ func (s *bzrSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
365365
}
366366

367367
// Now, list all the tags
368-
out, err := runFromRepoDir(ctx, r, "bzr", "tags", "--show-ids", "-v")
368+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "bzr", "tags", "--show-ids", "-v")
369369
if err != nil {
370370
return nil, fmt.Errorf("%s: %s", err, string(out))
371371
}
372372

373373
all := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
374374

375375
var branchrev []byte
376-
branchrev, err = runFromRepoDir(ctx, r, "bzr", "version-info", "--custom", "--template={revision_id}", "--revision=branch:.")
376+
branchrev, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "bzr", "version-info", "--custom", "--template={revision_id}", "--revision=branch:.")
377377
br := string(branchrev)
378378
if err != nil {
379379
return nil, fmt.Errorf("%s: %s", err, br)
@@ -416,7 +416,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
416416
}
417417

418418
// Now, list all the tags
419-
out, err := runFromRepoDir(ctx, r, "hg", "tags", "--debug", "--verbose")
419+
out, err := runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "tags", "--debug", "--verbose")
420420
if err != nil {
421421
return nil, fmt.Errorf("%s: %s", err, string(out))
422422
}
@@ -450,7 +450,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
450450
// bookmarks next, because the presence of the magic @ bookmark has to
451451
// determine how we handle the branches
452452
var magicAt bool
453-
out, err = runFromRepoDir(ctx, r, "hg", "bookmarks", "--debug")
453+
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "bookmarks", "--debug")
454454
if err != nil {
455455
// better nothing than partial and misleading
456456
return nil, fmt.Errorf("%s: %s", err, string(out))
@@ -483,7 +483,7 @@ func (s *hgSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
483483
}
484484
}
485485

486-
out, err = runFromRepoDir(ctx, r, "hg", "branches", "-c", "--debug")
486+
out, err = runFromRepoDir(ctx, r, defaultCmdTimeout, "hg", "branches", "-c", "--debug")
487487
if err != nil {
488488
// better nothing than partial and misleading
489489
return nil, fmt.Errorf("%s: %s", err, string(out))

0 commit comments

Comments
 (0)