Skip to content

Commit d6a7e4b

Browse files
authored
refactor: use a shutdown channel instead of dockerRunner ctx (#40)
1 parent ac06b4b commit d6a7e4b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

internal/engine/clients/runner/docker/runner.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type dockerRunner struct {
2525
images map[string]bool
2626
tasks map[string]string
2727
mtx sync.RWMutex
28-
ctx context.Context
29-
cancel context.CancelFunc
28+
exit chan struct{}
29+
once sync.Once
3030
wg sync.WaitGroup
3131
}
3232

@@ -163,16 +163,14 @@ func (r *dockerRunner) DeleteVolume(ctx context.Context, opts ...runner.DeleteVo
163163
}
164164

165165
func (r *dockerRunner) Close() {
166-
r.cancel()
167-
r.wg.Wait()
168-
close(r.sem)
166+
r.once.Do(func() {
167+
close(r.exit)
168+
r.wg.Wait()
169+
close(r.sem)
170+
})
169171
}
170172

171173
func (r *dockerRunner) pullImage(ctx context.Context, tag string) error {
172-
if r.ctx.Err() != nil {
173-
return r.ctx.Err()
174-
}
175-
176174
r.mtx.RLock()
177175
if _, ok := r.images[tag]; ok {
178176
r.mtx.RUnlock()
@@ -183,8 +181,8 @@ func (r *dockerRunner) pullImage(ctx context.Context, tag string) error {
183181
select {
184182
case <-ctx.Done():
185183
return ctx.Err()
186-
case <-r.ctx.Done():
187-
return r.ctx.Err()
184+
case <-r.exit:
185+
return runner.ErrRunnerClosing
188186
case r.sem <- struct{}{}:
189187
r.wg.Add(1)
190188
defer func() {
@@ -266,17 +264,15 @@ func NewRunner(opts ...runner.Option) runner.Runner {
266264
panic(err)
267265
}
268266

269-
ctx, cancel := context.WithCancel(context.Background())
270-
271267
dr := &dockerRunner{
272268
options: options,
273269
client: c,
274270
sem: make(chan struct{}, 1),
275271
images: map[string]bool{},
276272
tasks: map[string]string{},
277273
mtx: sync.RWMutex{},
278-
ctx: ctx,
279-
cancel: cancel,
274+
exit: make(chan struct{}),
275+
once: sync.Once{},
280276
wg: sync.WaitGroup{},
281277
}
282278

internal/engine/clients/runner/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ var (
77
ErrVolumeNotFound = errors.New("volume not found")
88
ErrInvalidVolumeName = errors.New("invalid volume name")
99
ErrBadExitCode = errors.New("bad exit code")
10+
ErrRunnerClosing = errors.New("runner is closing")
1011
)

0 commit comments

Comments
 (0)