Browse code

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>

Kir Kolyshkin authored on 2018/09/11 08:14:37
Showing 2 changed files
... ...
@@ -20,14 +20,10 @@ const name = "journald"
20 20
 type journald struct {
21 21
 	mu      sync.Mutex
22 22
 	vars    map[string]string // additional variables and values to send to the journal along with the log message
23
-	readers readerList
23
+	readers map[*logger.LogWatcher]struct{}
24 24
 	closed  bool
25 25
 }
26 26
 
27
-type readerList struct {
28
-	readers map[*logger.LogWatcher]*logger.LogWatcher
29
-}
30
-
31 27
 func init() {
32 28
 	if err := logger.RegisterLogDriver(name, New); err != nil {
33 29
 		logrus.Fatal(err)
... ...
@@ -84,7 +80,7 @@ func New(info logger.Info) (logger.Logger, error) {
84 84
 	for k, v := range extraAttrs {
85 85
 		vars[k] = v
86 86
 	}
87
-	return &journald{vars: vars, readers: readerList{readers: make(map[*logger.LogWatcher]*logger.LogWatcher)}}, nil
87
+	return &journald{vars: vars, readers: make(map[*logger.LogWatcher]struct{})}, nil
88 88
 }
89 89
 
90 90
 // We don't actually accept any options, but we have to supply a callback for
... ...
@@ -164,8 +164,10 @@ import (
164 164
 func (s *journald) Close() error {
165 165
 	s.mu.Lock()
166 166
 	s.closed = true
167
-	for reader := range s.readers.readers {
168
-		reader.ProducerGone()
167
+	for r := range s.readers {
168
+		r.ProducerGone()
169
+		delete(s.readers, r)
170
+
169 171
 	}
170 172
 	s.mu.Unlock()
171 173
 	return nil
... ...
@@ -253,7 +255,7 @@ drain:
253 253
 
254 254
 func (s *journald) followJournal(logWatcher *logger.LogWatcher, j *C.sd_journal, pfd [2]C.int, cursor *C.char, untilUnixMicro uint64) *C.char {
255 255
 	s.mu.Lock()
256
-	s.readers.readers[logWatcher] = logWatcher
256
+	s.readers[logWatcher] = struct{}{}
257 257
 	if s.closed {
258 258
 		// the journald Logger is closed, presumably because the container has been
259 259
 		// 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,
290 290
 		// Clean up.
291 291
 		C.close(pfd[0])
292 292
 		s.mu.Lock()
293
-		delete(s.readers.readers, logWatcher)
293
+		delete(s.readers, logWatcher)
294 294
 		s.mu.Unlock()
295 295
 		close(logWatcher.Msg)
296 296
 		newCursor <- cursor