mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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>
This commit is contained in:
parent
77faf158f5
commit
b2b169f13f
2 changed files with 8 additions and 10 deletions
|
@ -20,14 +20,10 @@ const name = "journald"
|
||||||
type journald struct {
|
type journald struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
vars map[string]string // additional variables and values to send to the journal along with the log message
|
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
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type readerList struct {
|
|
||||||
readers map[*logger.LogWatcher]*logger.LogWatcher
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if err := logger.RegisterLogDriver(name, New); err != nil {
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
|
@ -84,7 +80,7 @@ func New(info logger.Info) (logger.Logger, error) {
|
||||||
for k, v := range extraAttrs {
|
for k, v := range extraAttrs {
|
||||||
vars[k] = v
|
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
|
// 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 {
|
func (s *journald) Close() error {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.closed = true
|
s.closed = true
|
||||||
for reader := range s.readers.readers {
|
for r := range s.readers {
|
||||||
reader.ProducerGone()
|
r.ProducerGone()
|
||||||
|
delete(s.readers, r)
|
||||||
|
|
||||||
}
|
}
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return nil
|
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 {
|
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.mu.Lock()
|
||||||
s.readers.readers[logWatcher] = logWatcher
|
s.readers[logWatcher] = struct{}{}
|
||||||
if s.closed {
|
if s.closed {
|
||||||
// the journald Logger is closed, presumably because the container has been
|
// 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
|
// 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.
|
// Clean up.
|
||||||
C.close(pfd[0])
|
C.close(pfd[0])
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
delete(s.readers.readers, logWatcher)
|
delete(s.readers, logWatcher)
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
close(logWatcher.Msg)
|
close(logWatcher.Msg)
|
||||||
newCursor <- cursor
|
newCursor <- cursor
|
||||||
|
|
Loading…
Add table
Reference in a new issue