瀏覽代碼

Merge pull request #37819 from kolyshkin/journald-cleanup

daemon/logger/journald: simplify readers field
Brian Goff 6 年之前
父節點
當前提交
6de755c838
共有 2 個文件被更改,包括 8 次插入10 次删除
  1. 2 6
      daemon/logger/journald/journald.go
  2. 6 4
      daemon/logger/journald/read.go

+ 2 - 6
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

+ 6 - 4
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