pubsub.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. *
  3. * Copyright 2023 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpcsync
  19. import (
  20. "context"
  21. "sync"
  22. )
  23. // Subscriber represents an entity that is subscribed to messages published on
  24. // a PubSub. It wraps the callback to be invoked by the PubSub when a new
  25. // message is published.
  26. type Subscriber interface {
  27. // OnMessage is invoked when a new message is published. Implementations
  28. // must not block in this method.
  29. OnMessage(msg any)
  30. }
  31. // PubSub is a simple one-to-many publish-subscribe system that supports
  32. // messages of arbitrary type. It guarantees that messages are delivered in
  33. // the same order in which they were published.
  34. //
  35. // Publisher invokes the Publish() method to publish new messages, while
  36. // subscribers interested in receiving these messages register a callback
  37. // via the Subscribe() method.
  38. //
  39. // Once a PubSub is stopped, no more messages can be published, but any pending
  40. // published messages will be delivered to the subscribers. Done may be used
  41. // to determine when all published messages have been delivered.
  42. type PubSub struct {
  43. cs *CallbackSerializer
  44. // Access to the below fields are guarded by this mutex.
  45. mu sync.Mutex
  46. msg any
  47. subscribers map[Subscriber]bool
  48. }
  49. // NewPubSub returns a new PubSub instance. Users should cancel the
  50. // provided context to shutdown the PubSub.
  51. func NewPubSub(ctx context.Context) *PubSub {
  52. return &PubSub{
  53. cs: NewCallbackSerializer(ctx),
  54. subscribers: map[Subscriber]bool{},
  55. }
  56. }
  57. // Subscribe registers the provided Subscriber to the PubSub.
  58. //
  59. // If the PubSub contains a previously published message, the Subscriber's
  60. // OnMessage() callback will be invoked asynchronously with the existing
  61. // message to begin with, and subsequently for every newly published message.
  62. //
  63. // The caller is responsible for invoking the returned cancel function to
  64. // unsubscribe itself from the PubSub.
  65. func (ps *PubSub) Subscribe(sub Subscriber) (cancel func()) {
  66. ps.mu.Lock()
  67. defer ps.mu.Unlock()
  68. ps.subscribers[sub] = true
  69. if ps.msg != nil {
  70. msg := ps.msg
  71. ps.cs.Schedule(func(context.Context) {
  72. ps.mu.Lock()
  73. defer ps.mu.Unlock()
  74. if !ps.subscribers[sub] {
  75. return
  76. }
  77. sub.OnMessage(msg)
  78. })
  79. }
  80. return func() {
  81. ps.mu.Lock()
  82. defer ps.mu.Unlock()
  83. delete(ps.subscribers, sub)
  84. }
  85. }
  86. // Publish publishes the provided message to the PubSub, and invokes
  87. // callbacks registered by subscribers asynchronously.
  88. func (ps *PubSub) Publish(msg any) {
  89. ps.mu.Lock()
  90. defer ps.mu.Unlock()
  91. ps.msg = msg
  92. for sub := range ps.subscribers {
  93. s := sub
  94. ps.cs.Schedule(func(context.Context) {
  95. ps.mu.Lock()
  96. defer ps.mu.Unlock()
  97. if !ps.subscribers[s] {
  98. return
  99. }
  100. s.OnMessage(msg)
  101. })
  102. }
  103. }
  104. // Done returns a channel that is closed after the context passed to NewPubSub
  105. // is canceled and all updates have been sent to subscribers.
  106. func (ps *PubSub) Done() <-chan struct{} {
  107. return ps.cs.Done()
  108. }