mirror of https://github.com/docker/docs.git
only close LogDriver after LogCopier is done
this prevents the copier from sending messages in the buffer to the closed driver. If the copied took longer than the timeout to drain the buffer, this aborts the copier read loop and return back so we can cleanup resources properly. Signed-off-by: Daniel Dao <dqminh@cloudflare.com>
This commit is contained in:
parent
012a3b6e74
commit
84e14754e1
|
@ -369,6 +369,9 @@ func (m *containerMonitor) resetContainer(lock bool) {
|
|||
select {
|
||||
case <-time.After(loggerCloseTimeout):
|
||||
logrus.Warnf("Logger didn't exit in time: logs may be truncated")
|
||||
container.LogCopier.Close()
|
||||
// always waits for the LogCopier to finished before closing
|
||||
<-exit
|
||||
case <-exit:
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ type Copier struct {
|
|||
srcs map[string]io.Reader
|
||||
dst Logger
|
||||
copyJobs sync.WaitGroup
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
// NewCopier creates a new Copier
|
||||
|
@ -28,6 +29,7 @@ func NewCopier(cid string, srcs map[string]io.Reader, dst Logger) *Copier {
|
|||
cid: cid,
|
||||
srcs: srcs,
|
||||
dst: dst,
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,6 +46,10 @@ func (c *Copier) copySrc(name string, src io.Reader) {
|
|||
reader := bufio.NewReader(src)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-c.closed:
|
||||
return
|
||||
default:
|
||||
line, err := reader.ReadBytes('\n')
|
||||
line = bytes.TrimSuffix(line, []byte{'\n'})
|
||||
|
||||
|
@ -61,7 +67,7 @@ func (c *Copier) copySrc(name string, src io.Reader) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,3 +75,12 @@ func (c *Copier) copySrc(name string, src io.Reader) {
|
|||
func (c *Copier) Wait() {
|
||||
c.copyJobs.Wait()
|
||||
}
|
||||
|
||||
// Close closes the copier
|
||||
func (c *Copier) Close() {
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
close(c.closed)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,9 +10,15 @@ import (
|
|||
|
||||
type TestLoggerJSON struct {
|
||||
*json.Encoder
|
||||
delay time.Duration
|
||||
}
|
||||
|
||||
func (l *TestLoggerJSON) Log(m *Message) error { return l.Encode(m) }
|
||||
func (l *TestLoggerJSON) Log(m *Message) error {
|
||||
if l.delay > 0 {
|
||||
time.Sleep(l.delay)
|
||||
}
|
||||
return l.Encode(m)
|
||||
}
|
||||
|
||||
func (l *TestLoggerJSON) Close() error { return nil }
|
||||
|
||||
|
@ -94,3 +100,33 @@ func TestCopier(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopierSlow(t *testing.T) {
|
||||
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
||||
var stdout bytes.Buffer
|
||||
for i := 0; i < 30; i++ {
|
||||
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var jsonBuf bytes.Buffer
|
||||
//encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
|
||||
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
|
||||
|
||||
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
||||
c := NewCopier(cid, map[string]io.Reader{"stdout": &stdout}, jsonLog)
|
||||
c.Run()
|
||||
wait := make(chan struct{})
|
||||
go func() {
|
||||
c.Wait()
|
||||
close(wait)
|
||||
}()
|
||||
<-time.After(150 * time.Millisecond)
|
||||
c.Close()
|
||||
select {
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
t.Fatalf("failed to exit in time after the copier is closed")
|
||||
case <-wait:
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue