1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-06-29 05:41:02 +02:00

refactoring to use atomic bool

fix compilation
This commit is contained in:
chrislu 2023-08-20 11:22:44 -07:00
parent 1e72579ac7
commit 3e9c32a3f0

View file

@ -3,6 +3,7 @@ package log_buffer
import ( import (
"bytes" "bytes"
"sync" "sync"
"sync/atomic"
"time" "time"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -34,7 +35,7 @@ type LogBuffer struct {
flushInterval time.Duration flushInterval time.Duration
flushFn func(startTime, stopTime time.Time, buf []byte) flushFn func(startTime, stopTime time.Time, buf []byte)
notifyFn func() notifyFn func()
isStopping bool isStopping *atomic.Bool
flushChan chan *dataToFlush flushChan chan *dataToFlush
lastTsNs int64 lastTsNs int64
sync.RWMutex sync.RWMutex
@ -50,6 +51,7 @@ func NewLogBuffer(name string, flushInterval time.Duration, flushFn func(startTi
flushFn: flushFn, flushFn: flushFn,
notifyFn: notifyFn, notifyFn: notifyFn,
flushChan: make(chan *dataToFlush, 256), flushChan: make(chan *dataToFlush, 256),
isStopping: new(atomic.Bool),
} }
go lb.loopFlush() go lb.loopFlush()
go lb.loopInterval() go lb.loopInterval()
@ -119,20 +121,14 @@ func (m *LogBuffer) AddToBuffer(partitionKey, data []byte, processingTsNs int64)
} }
func (m *LogBuffer) IsStopping() bool { func (m *LogBuffer) IsStopping() bool {
m.RLock() return m.isStopping.Load()
defer m.RUnlock()
return m.isStopping
} }
func (m *LogBuffer) Shutdown() { func (m *LogBuffer) Shutdown() {
m.Lock() isAlreadyStopped := m.isStopping.Swap(true)
defer m.Unlock() if isAlreadyStopped {
if m.isStopping {
return return
} }
m.isStopping = true
toFlush := m.copyToFlush() toFlush := m.copyToFlush()
m.flushChan <- toFlush m.flushChan <- toFlush
close(m.flushChan) close(m.flushChan)