-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloser.go
50 lines (43 loc) · 1.26 KB
/
closer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package runnable
import (
"context"
)
// Closer returns a runnable intended to call a Close method on shutdown.
func Closer(c interface{ Close() }) Runnable {
return &closer{baseWrapper{"closer", c}, c, func(ctx context.Context) error {
c.Close()
return nil
}}
}
// Closer returns a runnable intended to call a Close method on shutdown.
func CloserErr(c interface{ Close() error }) Runnable {
return &closer{baseWrapper{"closer", c}, c, func(ctx context.Context) error {
return c.Close()
}}
}
// Closer returns a runnable intended to call a Close method on shutdown.
func CloserCtx(c interface{ Close(context.Context) }) Runnable {
return &closer{baseWrapper{"closer", c}, c, func(ctx context.Context) error {
c.Close(ctx)
return nil
}}
}
// Closer returns a runnable intended to call a Close method on shutdown.
func CloserCtxErr(c interface{ Close(context.Context) error }) Runnable {
return &closer{baseWrapper{"closer", c}, c, func(ctx context.Context) error {
return c.Close(ctx)
}}
}
type closer struct {
baseWrapper
c any
closeFn func(context.Context) error
}
func (c *closer) Run(ctx context.Context) error {
<-ctx.Done()
err := c.closeFn(ctx)
if err != nil {
return &RunnableError{"closer: Close() returned an error", err}
}
return nil
}