Skip to content

Commit 3e467aa

Browse files
committed
feat: add read timeout config
1 parent 0a0d8ce commit 3e467aa

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

internal/command/cli.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,12 @@ func (cli *DebugCli) PullImage(image string) error {
200200
domain, remainder := splitDockerDomain(image)
201201
imageName := path.Join(domain, remainder)
202202

203-
ctx, cancel := cli.withContent(cli.config.Timeout * 30)
203+
ctx, cancel := context.WithCancel(cli.ctx)
204204
defer cancel()
205205
responseBody, err := cli.client.ImagePull(ctx, imageName, types.ImagePullOptions{})
206206
if err != nil {
207207
return errors.WithStack(err)
208208
}
209-
210209
defer func() {
211210
err = responseBody.Close()
212211
if err != nil {
@@ -424,7 +423,7 @@ func (cli *DebugCli) ExecStart(_ execOptions, execID string) error {
424423
Resp: response,
425424
TTY: true,
426425
}
427-
return streamer.Stream(cli.ctx)
426+
return streamer.Stream(cli.ctx, cli.config.ReadTimeout)
428427
}
429428

430429
// FindContainer find container

internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var configName = "config.toml"
2020
// PathSeparator path separator
2121
var PathSeparator = string(os.PathSeparator)
2222

23-
// ConfigFile 默认配置文件
23+
// File 默认配置文件
2424
var File = fmt.Sprintf(
2525
"~%s%s%s%s",
2626
PathSeparator,
@@ -65,6 +65,7 @@ type Config struct {
6565
Timeout time.Duration `toml:"timeout"`
6666
DockerConfigDefault string `toml:"config_default"`
6767
DockerConfig map[string]*DockerConfig `toml:"config"`
68+
ReadTimeout time.Duration `toml:"read_timeout"`
6869
}
6970

7071
// Save save to default file
@@ -144,6 +145,7 @@ func InitConfig() (*Config, error) {
144145
DockerConfig: map[string]*DockerConfig{
145146
"default": &dc,
146147
},
148+
ReadTimeout: time.Second * 3,
147149
}
148150
file, err := os.OpenFile(File, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
149151
if err != nil {

internal/config/version-0.7.6.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package config
2+
3+
import (
4+
"github.com/blang/semver"
5+
"time"
6+
)
7+
8+
// Up000706 update version 0.7.6
9+
func Up000706(conf *Config) error {
10+
if conf.ReadTimeout == 0 {
11+
conf.ReadTimeout = time.Second * 3
12+
}
13+
return nil
14+
}
15+
16+
func init() {
17+
v, err := semver.Parse("0.7.6")
18+
if err != nil {
19+
return
20+
}
21+
migrationArr = append(migrationArr, &migration{
22+
Up: Up000706,
23+
Version: v,
24+
})
25+
}

pkg/tty/hijack.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type HijackedIOStreamer struct {
3939
// to/from the hijacked connection, blocking until it is either done reading
4040
// output, the user inputs the detach key sequence when in TTY mode, or when
4141
// the given context is cancelled.
42-
func (h *HijackedIOStreamer) Stream(ctx context.Context) error {
42+
func (h *HijackedIOStreamer) Stream(ctx context.Context, readTimeout time.Duration) error {
4343
restoreInput, err := h.setupInput()
4444
if err != nil {
4545
return errors.Errorf("unable to setup input stream: %s", err)
@@ -48,7 +48,7 @@ func (h *HijackedIOStreamer) Stream(ctx context.Context) error {
4848
defer restoreInput()
4949
defer h.Resp.Close()
5050
outputDone := h.beginOutputStream(restoreInput)
51-
inputDone, detached := h.beginInputStream(ctx, restoreInput)
51+
inputDone, detached := h.beginInputStream(ctx, restoreInput, readTimeout)
5252

5353
select {
5454
case err = <-outputDone:
@@ -145,7 +145,7 @@ func (h *HijackedIOStreamer) beginOutputStream(restoreInput func()) <-chan error
145145

146146
var errInvalidWrite = errors.New("invalid write result")
147147

148-
func Copy(ctx context.Context, dst net.Conn, src io.Reader) (written int64, err error) {
148+
func Copy(ctx context.Context, dst net.Conn, src io.Reader, readTimeout time.Duration) (written int64, err error) {
149149
size := 32 * 1024
150150
buf := make([]byte, size)
151151
for {
@@ -158,7 +158,7 @@ func Copy(ctx context.Context, dst net.Conn, src io.Reader) (written int64, err
158158
nr, er := src.Read(buf)
159159
if nr > 0 {
160160
// docker container is stop check
161-
err = dst.SetReadDeadline(time.Now().Add(time.Second * 3))
161+
err = dst.SetReadDeadline(time.Now().Add(readTimeout))
162162
if err != nil {
163163
break
164164
}
@@ -189,13 +189,13 @@ func Copy(ctx context.Context, dst net.Conn, src io.Reader) (written int64, err
189189
return
190190
}
191191

192-
func (h *HijackedIOStreamer) beginInputStream(ctx context.Context, restoreInput func()) (doneC <-chan struct{}, detachedC <-chan error) {
192+
func (h *HijackedIOStreamer) beginInputStream(ctx context.Context, restoreInput func(), readTimeout time.Duration) (doneC <-chan struct{}, detachedC <-chan error) {
193193
inputDone := make(chan struct{})
194194
detached := make(chan error)
195195

196196
go func() {
197197
if h.InputStream != nil {
198-
_, err := Copy(ctx, h.Resp.Conn, h.InputStream)
198+
_, err := Copy(ctx, h.Resp.Conn, h.InputStream, readTimeout)
199199
restoreInput()
200200

201201
logrus.Debug("[hijack] End of stdin")

0 commit comments

Comments
 (0)