local_partition_publishers.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package topic
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "time"
  6. )
  7. type LocalPartitionPublishers struct {
  8. publishers map[string]*LocalPublisher
  9. publishersLock sync.RWMutex
  10. }
  11. type LocalPublisher struct {
  12. connectTimeNs int64 // accessed atomically
  13. lastSeenTimeNs int64 // accessed atomically
  14. lastPublishedOffset int64 // accessed atomically - offset of last message published
  15. lastAckedOffset int64 // accessed atomically - offset of last message acknowledged by broker
  16. }
  17. func NewLocalPublisher() *LocalPublisher {
  18. now := time.Now().UnixNano()
  19. publisher := &LocalPublisher{}
  20. atomic.StoreInt64(&publisher.connectTimeNs, now)
  21. atomic.StoreInt64(&publisher.lastSeenTimeNs, now)
  22. atomic.StoreInt64(&publisher.lastPublishedOffset, 0)
  23. atomic.StoreInt64(&publisher.lastAckedOffset, 0)
  24. return publisher
  25. }
  26. func (p *LocalPublisher) SignalShutdown() {
  27. }
  28. // UpdateLastSeen updates the last activity time for this publisher
  29. func (p *LocalPublisher) UpdateLastSeen() {
  30. atomic.StoreInt64(&p.lastSeenTimeNs, time.Now().UnixNano())
  31. }
  32. // UpdatePublishedOffset updates the offset of the last message published by this publisher
  33. func (p *LocalPublisher) UpdatePublishedOffset(offset int64) {
  34. atomic.StoreInt64(&p.lastPublishedOffset, offset)
  35. atomic.StoreInt64(&p.lastSeenTimeNs, time.Now().UnixNano())
  36. }
  37. // UpdateAckedOffset updates the offset of the last message acknowledged by the broker for this publisher
  38. func (p *LocalPublisher) UpdateAckedOffset(offset int64) {
  39. atomic.StoreInt64(&p.lastAckedOffset, offset)
  40. atomic.StoreInt64(&p.lastSeenTimeNs, time.Now().UnixNano())
  41. }
  42. // GetTimestamps returns the connect and last seen timestamps safely
  43. func (p *LocalPublisher) GetTimestamps() (connectTimeNs, lastSeenTimeNs int64) {
  44. return atomic.LoadInt64(&p.connectTimeNs), atomic.LoadInt64(&p.lastSeenTimeNs)
  45. }
  46. // GetOffsets returns the published and acknowledged offsets safely
  47. func (p *LocalPublisher) GetOffsets() (lastPublishedOffset, lastAckedOffset int64) {
  48. return atomic.LoadInt64(&p.lastPublishedOffset), atomic.LoadInt64(&p.lastAckedOffset)
  49. }
  50. func NewLocalPartitionPublishers() *LocalPartitionPublishers {
  51. return &LocalPartitionPublishers{
  52. publishers: make(map[string]*LocalPublisher),
  53. }
  54. }
  55. func (p *LocalPartitionPublishers) AddPublisher(clientName string, publisher *LocalPublisher) {
  56. p.publishersLock.Lock()
  57. defer p.publishersLock.Unlock()
  58. p.publishers[clientName] = publisher
  59. }
  60. func (p *LocalPartitionPublishers) RemovePublisher(clientName string) {
  61. p.publishersLock.Lock()
  62. defer p.publishersLock.Unlock()
  63. delete(p.publishers, clientName)
  64. }
  65. func (p *LocalPartitionPublishers) SignalShutdown() {
  66. p.publishersLock.RLock()
  67. defer p.publishersLock.RUnlock()
  68. for _, publisher := range p.publishers {
  69. publisher.SignalShutdown()
  70. }
  71. }
  72. func (p *LocalPartitionPublishers) Size() int {
  73. p.publishersLock.RLock()
  74. defer p.publishersLock.RUnlock()
  75. return len(p.publishers)
  76. }
  77. // GetPublisherNames returns the names of all publishers
  78. func (p *LocalPartitionPublishers) GetPublisherNames() []string {
  79. p.publishersLock.RLock()
  80. defer p.publishersLock.RUnlock()
  81. names := make([]string, 0, len(p.publishers))
  82. for name := range p.publishers {
  83. names = append(names, name)
  84. }
  85. return names
  86. }
  87. // ForEachPublisher iterates over all publishers
  88. func (p *LocalPartitionPublishers) ForEachPublisher(fn func(name string, publisher *LocalPublisher)) {
  89. p.publishersLock.RLock()
  90. defer p.publishersLock.RUnlock()
  91. for name, publisher := range p.publishers {
  92. fn(name, publisher)
  93. }
  94. }