From b2b169f13f681cd0d591ccb06d6cfff97933db77 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 10 Sep 2018 16:14:37 -0700 Subject: [PATCH] daemon/logger/journald: simplify readers field As in other similar drivers (jsonlog, local), use a set (i.e. `map[whatever]struct{}`), making the code simpler. While at it, make sure we remove the reader from the set after calling `ProducerGone()` on it. Signed-off-by: Kir Kolyshkin --- daemon/logger/journald/journald.go | 8 ++------ daemon/logger/journald/read.go | 10 ++++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/daemon/logger/journald/journald.go b/daemon/logger/journald/journald.go index 342e18f57f..113ed585c9 100644 --- a/daemon/logger/journald/journald.go +++ b/daemon/logger/journald/journald.go @@ -20,14 +20,10 @@ const name = "journald" type journald struct { mu sync.Mutex vars map[string]string // additional variables and values to send to the journal along with the log message - readers readerList + readers map[*logger.LogWatcher]struct{} closed bool } -type readerList struct { - readers map[*logger.LogWatcher]*logger.LogWatcher -} - func init() { if err := logger.RegisterLogDriver(name, New); err != nil { logrus.Fatal(err) @@ -84,7 +80,7 @@ func New(info logger.Info) (logger.Logger, error) { for k, v := range extraAttrs { vars[k] = v } - return &journald{vars: vars, readers: readerList{readers: make(map[*logger.LogWatcher]*logger.LogWatcher)}}, nil + return &journald{vars: vars, readers: make(map[*logger.LogWatcher]struct{})}, nil } // We don't actually accept any options, but we have to supply a callback for diff --git a/daemon/logger/journald/read.go b/daemon/logger/journald/read.go index cadb97f4ca..4bddfd5781 100644 --- a/daemon/logger/journald/read.go +++ b/daemon/logger/journald/read.go @@ -164,8 +164,10 @@ import ( func (s *journald) Close() error { s.mu.Lock() s.closed = true - for reader := range s.readers.readers { - reader.ProducerGone() + for r := range s.readers { + r.ProducerGone() + delete(s.readers, r) + } s.mu.Unlock() return nil @@ -253,7 +255,7 @@ drain: func (s *journald) followJournal(logWatcher *logger.LogWatcher, j *C.sd_journal, pfd [2]C.int, cursor *C.char, untilUnixMicro uint64) *C.char { s.mu.Lock() - s.readers.readers[logWatcher] = logWatcher + s.readers[logWatcher] = struct{}{} if s.closed { // the journald Logger is closed, presumably because the container has been // reset. So we shouldn't follow, because we'll never be woken up. But we @@ -290,7 +292,7 @@ func (s *journald) followJournal(logWatcher *logger.LogWatcher, j *C.sd_journal, // Clean up. C.close(pfd[0]) s.mu.Lock() - delete(s.readers.readers, logWatcher) + delete(s.readers, logWatcher) s.mu.Unlock() close(logWatcher.Msg) newCursor <- cursor