Skip to content

Commit b2d37bc

Browse files
committed
add timeouts to contexts
1 parent 90ea3b3 commit b2d37bc

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

docker/core/core.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ const (
1919
networkDriverName = vxrouter.NetworkDriver
2020
ipamDriverName = vxrouter.IpamDriver
2121
envPrefix = vxrouter.EnvPrefix
22+
dockerTimeout = 5 * time.Second
2223
)
2324

25+
func toCtx() context.Context {
26+
c, _ := context.WithTimeout(context.Background(), dockerTimeout)
27+
return c
28+
}
29+
2430
// Core is a wrapper for docker client type things
2531
type Core struct {
2632
dc *client.Client
@@ -94,7 +100,7 @@ func (c *Core) getNrFromCache(s string) *types.NetworkResource {
94100

95101
// GetContainers gets a list of docker containers
96102
func (c *Core) GetContainers() ([]types.Container, error) {
97-
return c.dc.ContainerList(context.Background(), types.ContainerListOptions{})
103+
return c.dc.ContainerList(toCtx(), types.ContainerListOptions{})
98104
}
99105

100106
// GetNetworkResourceByID gets a network resource by ID (checks cache first)
@@ -108,7 +114,7 @@ func (c *Core) GetNetworkResourceByID(id string) (*types.NetworkResource, error)
108114
}
109115

110116
//netid wasn't in cache, fetch from docker inspect
111-
nnr, err := c.dc.NetworkInspect(context.Background(), id)
117+
nnr, err := c.dc.NetworkInspect(toCtx(), id)
112118
if err != nil {
113119
log.WithError(err).Error("failed to inspect network")
114120
return nil, err
@@ -132,7 +138,7 @@ func (c *Core) GetNetworkResourceByPool(pool string) (*types.NetworkResource, er
132138

133139
flts := filters.NewArgs()
134140
flts.Add("driver", networkDriverName)
135-
nl, err := c.dc.NetworkList(context.Background(), types.NetworkListOptions{Filters: flts})
141+
nl, err := c.dc.NetworkList(toCtx(), types.NetworkListOptions{Filters: flts})
136142
if err != nil {
137143
log.WithError(err).Error("failed to list networks")
138144
return nil, err

docker/vxrnet/main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ import (
2121
)
2222

2323
const (
24-
version = vxrouter.Version
25-
envPrefix = vxrouter.EnvPrefix
24+
version = vxrouter.Version
25+
envPrefix = vxrouter.EnvPrefix
26+
shutdownTimeout = 10 * time.Second
2627
)
2728

29+
func shutdownContext() context.Context {
30+
c, _ := context.WithTimeout(context.Background(), shutdownTimeout)
31+
return c
32+
}
33+
2834
func main() {
2935
app := cli.NewApp()
3036
app.Name = "docker-" + network.DriverName
@@ -115,12 +121,12 @@ func Run(ctx *cli.Context) {
115121
case <-c:
116122
}
117123

118-
err = nh.Shutdown(context.Background())
124+
err = nh.Shutdown(shutdownContext())
119125
if err != nil {
120126
log.WithField("driver", network.DriverName).WithError(err).Error("error shutting down driver")
121127
}
122128

123-
err = ih.Shutdown(context.Background())
129+
err = ih.Shutdown(shutdownContext())
124130
if err != nil {
125131
log.WithField("driver", ipam.DriverName).WithError(err).Error("error shutting down driver")
126132
}

0 commit comments

Comments
 (0)