Skip to content

Commit fc9335d

Browse files
authored
Merge pull request #3459 from crosbymichael/timeout-config
Allow timeouts to be configured in config
2 parents 6cb56bb + 2e8ea9f commit fc9335d

6 files changed

Lines changed: 107 additions & 4 deletions

File tree

cmd/containerd/command/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/BurntSushi/toml"
25+
"github.com/containerd/containerd/pkg/timeout"
2526
"github.com/containerd/containerd/services/server"
2627
srvconfig "github.com/containerd/containerd/services/server/config"
2728
"github.com/urfave/cli"
@@ -68,6 +69,12 @@ var configCommand = cli.Command{
6869
config.Plugins[p.URI()] = p.Config
6970
}
7071
}
72+
timeouts := timeout.All()
73+
config.Timeouts = make(map[string]string)
74+
for k, v := range timeouts {
75+
config.Timeouts[k] = v.String()
76+
}
77+
7178
_, err = config.WriteTo(os.Stdout)
7279
return err
7380
},

pkg/timeout/timeout.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package timeout
18+
19+
import (
20+
"context"
21+
"sync"
22+
"time"
23+
)
24+
25+
var (
26+
mu sync.Mutex
27+
timeouts = make(map[string]time.Duration)
28+
29+
// DefaultTimeout of the timeout package
30+
DefaultTimeout = 1 * time.Second
31+
)
32+
33+
// Set the timeout for the key
34+
func Set(key string, t time.Duration) {
35+
mu.Lock()
36+
timeouts[key] = t
37+
mu.Unlock()
38+
}
39+
40+
// Get returns the timeout for the provided key
41+
func Get(key string) time.Duration {
42+
mu.Lock()
43+
t, ok := timeouts[key]
44+
mu.Unlock()
45+
if !ok {
46+
t = DefaultTimeout
47+
}
48+
return t
49+
}
50+
51+
// WithContext returns a context with the specified timeout for the provided key
52+
func WithContext(ctx context.Context, key string) (context.Context, func()) {
53+
t := Get(key)
54+
return context.WithTimeout(ctx, t)
55+
}
56+
57+
// All returns all keys and their timeouts
58+
func All() map[string]time.Duration {
59+
out := make(map[string]time.Duration)
60+
mu.Lock()
61+
defer mu.Unlock()
62+
for k, v := range timeouts {
63+
out[k] = v
64+
}
65+
return out
66+
}

runtime/v2/shim.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/containerd/containerd/identifiers"
3333
"github.com/containerd/containerd/log"
3434
"github.com/containerd/containerd/namespaces"
35+
"github.com/containerd/containerd/pkg/timeout"
3536
"github.com/containerd/containerd/runtime"
3637
client "github.com/containerd/containerd/runtime/v2/shim"
3738
"github.com/containerd/containerd/runtime/v2/task"
@@ -41,6 +42,18 @@ import (
4142
"github.com/sirupsen/logrus"
4243
)
4344

45+
const (
46+
loadTimeout = "io.containerd.timeout.shim.load"
47+
cleanupTimeout = "io.containerd.timeout.shim.cleanup"
48+
shutdownTimeout = "io.containerd.timeout.shim.shutdown"
49+
)
50+
51+
func init() {
52+
timeout.Set(loadTimeout, 5*time.Second)
53+
timeout.Set(cleanupTimeout, 5*time.Second)
54+
timeout.Set(shutdownTimeout, 3*time.Second)
55+
}
56+
4457
func loadAddress(path string) (string, error) {
4558
data, err := ioutil.ReadFile(path)
4659
if err != nil {
@@ -100,7 +113,7 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
100113
events: events,
101114
rtTasks: rt,
102115
}
103-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
116+
ctx, cancel := timeout.WithContext(ctx, loadTimeout)
104117
defer cancel()
105118
if err := s.Connect(ctx); err != nil {
106119
return nil, err
@@ -110,7 +123,7 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
110123

111124
func cleanupAfterDeadShim(ctx context.Context, id, ns string, events *exchange.Exchange, binaryCall *binary) {
112125
ctx = namespaces.WithNamespace(ctx, ns)
113-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
126+
ctx, cancel := timeout.WithContext(ctx, cleanupTimeout)
114127
defer cancel()
115128

116129
log.G(ctx).WithFields(logrus.Fields{
@@ -185,7 +198,7 @@ func (s *shim) Shutdown(ctx context.Context) error {
185198
}
186199

187200
func (s *shim) waitShutdown(ctx context.Context) error {
188-
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
201+
ctx, cancel := timeout.WithContext(ctx, shutdownTimeout)
189202
defer cancel()
190203
return s.Shutdown(ctx)
191204
}

services/server/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Config struct {
5555
Cgroup CgroupConfig `toml:"cgroup"`
5656
// ProxyPlugins configures plugins which are communicated to over GRPC
5757
ProxyPlugins map[string]ProxyPlugin `toml:"proxy_plugins"`
58+
// Timeouts specified as a duration
59+
Timeouts map[string]string `toml:"timeouts"`
5860

5961
StreamProcessors []StreamProcessor `toml:"stream_processors"`
6062

services/server/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/containerd/containerd/log"
4141
"github.com/containerd/containerd/metadata"
4242
"github.com/containerd/containerd/pkg/dialer"
43+
"github.com/containerd/containerd/pkg/timeout"
4344
"github.com/containerd/containerd/plugin"
4445
srvconfig "github.com/containerd/containerd/services/server/config"
4546
"github.com/containerd/containerd/snapshots"
@@ -77,6 +78,13 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
7778
if err := apply(ctx, config); err != nil {
7879
return nil, err
7980
}
81+
for key, sec := range config.Timeouts {
82+
d, err := time.ParseDuration(sec)
83+
if err != nil {
84+
return nil, errors.Errorf("unable to parse %s into a time duration", sec)
85+
}
86+
timeout.Set(key, d)
87+
}
8088
plugins, err := LoadPlugins(ctx, config)
8189
if err != nil {
8290
return nil, err

services/tasks/local.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/containerd/containerd/log"
4141
"github.com/containerd/containerd/metadata"
4242
"github.com/containerd/containerd/mount"
43+
"github.com/containerd/containerd/pkg/timeout"
4344
"github.com/containerd/containerd/plugin"
4445
"github.com/containerd/containerd/runtime"
4546
"github.com/containerd/containerd/runtime/linux/runctypes"
@@ -61,13 +62,19 @@ var (
6162
empty = &ptypes.Empty{}
6263
)
6364

65+
const (
66+
stateTimeout = "io.containerd.timeout.task.state"
67+
)
68+
6469
func init() {
6570
plugin.Register(&plugin.Registration{
6671
Type: plugin.ServicePlugin,
6772
ID: services.TasksService,
6873
Requires: tasksServiceRequires,
6974
InitFn: initFunc,
7075
})
76+
77+
timeout.Set(stateTimeout, 2*time.Second)
7178
}
7279

7380
func initFunc(ic *plugin.InitContext) (interface{}, error) {
@@ -266,7 +273,7 @@ func (l *local) DeleteProcess(ctx context.Context, r *api.DeleteProcessRequest,
266273
}
267274

268275
func getProcessState(ctx context.Context, p runtime.Process) (*task.Process, error) {
269-
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
276+
ctx, cancel := timeout.WithContext(ctx, stateTimeout)
270277
defer cancel()
271278

272279
state, err := p.State(ctx)

0 commit comments

Comments
 (0)