This commit is contained in:
Chris Lu
2020-05-12 22:55:55 -07:00
parent 25257acd51
commit ca4017dd87

View File

@@ -25,7 +25,7 @@ func (tp *TopicPartition) String() string {
return fmt.Sprintf(TopicPartitionFmt, tp.Namespace, tp.Topic, tp.Partition) return fmt.Sprintf(TopicPartitionFmt, tp.Namespace, tp.Topic, tp.Partition)
} }
type TopicCursor struct { type TopicControl struct {
sync.Mutex sync.Mutex
cond *sync.Cond cond *sync.Cond
subscriberCount int subscriberCount int
@@ -36,18 +36,18 @@ type TopicCursor struct {
type TopicManager struct { type TopicManager struct {
sync.Mutex sync.Mutex
topicCursors map[TopicPartition]*TopicCursor topicCursors map[TopicPartition]*TopicControl
broker *MessageBroker broker *MessageBroker
} }
func NewTopicManager(messageBroker *MessageBroker) *TopicManager { func NewTopicManager(messageBroker *MessageBroker) *TopicManager {
return &TopicManager{ return &TopicManager{
topicCursors: make(map[TopicPartition]*TopicCursor), topicCursors: make(map[TopicPartition]*TopicControl),
broker: messageBroker, broker: messageBroker,
} }
} }
func (tm *TopicManager) buildLogBuffer(tl *TopicCursor, tp TopicPartition, topicConfig *messaging_pb.TopicConfiguration) *log_buffer.LogBuffer { func (tm *TopicManager) buildLogBuffer(tc *TopicControl, tp TopicPartition, topicConfig *messaging_pb.TopicConfiguration) *log_buffer.LogBuffer {
flushFn := func(startTime, stopTime time.Time, buf []byte) { flushFn := func(startTime, stopTime time.Time, buf []byte) {
@@ -69,29 +69,29 @@ func (tm *TopicManager) buildLogBuffer(tl *TopicCursor, tp TopicPartition, topic
} }
} }
logBuffer := log_buffer.NewLogBuffer(time.Minute, flushFn, func() { logBuffer := log_buffer.NewLogBuffer(time.Minute, flushFn, func() {
tl.subscriptions.NotifyAll() tc.subscriptions.NotifyAll()
}) })
return logBuffer return logBuffer
} }
func (tm *TopicManager) RequestLock(partition TopicPartition, topicConfig *messaging_pb.TopicConfiguration, isPublisher bool) *TopicCursor { func (tm *TopicManager) RequestLock(partition TopicPartition, topicConfig *messaging_pb.TopicConfiguration, isPublisher bool) *TopicControl {
tm.Lock() tm.Lock()
defer tm.Unlock() defer tm.Unlock()
lock, found := tm.topicCursors[partition] tc, found := tm.topicCursors[partition]
if !found { if !found {
lock = &TopicCursor{} tc = &TopicControl{}
tm.topicCursors[partition] = lock tm.topicCursors[partition] = tc
lock.subscriptions = NewTopicPartitionSubscriptions() tc.subscriptions = NewTopicPartitionSubscriptions()
lock.logBuffer = tm.buildLogBuffer(lock, partition, topicConfig) tc.logBuffer = tm.buildLogBuffer(tc, partition, topicConfig)
} }
if isPublisher { if isPublisher {
lock.publisherCount++ tc.publisherCount++
} else { } else {
lock.subscriberCount++ tc.subscriberCount++
} }
return lock return tc
} }
func (tm *TopicManager) ReleaseLock(partition TopicPartition, isPublisher bool) { func (tm *TopicManager) ReleaseLock(partition TopicPartition, isPublisher bool) {