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 <kolyshkin@gmail.com>
(cherry picked from commit b2b169f13f681cd0d591ccb06d6cfff97933db77)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Upstream-commit: fe85c72a2eac4cbf249d2c4c754684bb447eefdd
Component: engine
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user