Stop closes the buffer, cleans up background goroutines, and flushes remaining unwritten data.
()
| 188 | // Stop closes the buffer, cleans up background goroutines, and flushes |
| 189 | // remaining unwritten data. |
| 190 | func (s *BufferedWriteSyncer) Stop() (err error) { |
| 191 | // Critical section. |
| 192 | stopped := func() bool { |
| 193 | s.mu.Lock() |
| 194 | defer s.mu.Unlock() |
| 195 | |
| 196 | if !s.initialized { |
| 197 | return false |
| 198 | } |
| 199 | |
| 200 | if s.stopped { |
| 201 | return false |
| 202 | } |
| 203 | s.stopped = true |
| 204 | |
| 205 | s.ticker.Stop() |
| 206 | close(s.stop) // tell flushLoop to stop |
| 207 | return true |
| 208 | }() |
| 209 | |
| 210 | // Not initialized, or already stopped, no need for any cleanup. |
| 211 | if !stopped { |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | // Wait for flushLoop to end outside of the lock, as it may need the lock to complete. |
| 216 | // See https://github.com/uber-go/zap/issues/1428 for details. |
| 217 | <-s.done |
| 218 | |
| 219 | return s.Sync() |
| 220 | } |