You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4 KiB

  1. # ADR 033: pubsub 2.0
  2. Author: Anton Kaliaev (@melekes)
  3. ## Changelog
  4. 02-10-2018: Initial draft
  5. ## Context
  6. Since the initial version of the pubsub, there's been a number of issues
  7. raised: #951, #1879, #1880. Some of them are high-level issues questioning the
  8. core design choices made. Others are minor and mostly about the interface of
  9. `Subscribe()` / `Publish()` functions.
  10. ### Sync vs Async
  11. Now, when publishing a message to subscribers, we can do it in a goroutine:
  12. _using channels for data transmission_
  13. ```go
  14. for each subscriber {
  15. out := subscriber.outc
  16. go func() {
  17. out <- msg
  18. }
  19. }
  20. ```
  21. _by invoking callback functions_
  22. ```go
  23. for each subscriber {
  24. go subscriber.callbackFn()
  25. }
  26. ```
  27. This gives us greater performance and allows us to avoid "slow client problem"
  28. (when other subscribers have to wait for a slow subscriber). A pool of
  29. goroutines can be used to avoid uncontrolled memory growth.
  30. In certain cases, this is what you want. But in our case, because we need
  31. strict ordering of events (if event A was published before B, the guaranteed
  32. delivery order will be A -> B), we can't use goroutines.
  33. There is also a question whenever we should have a non-blocking send:
  34. ```go
  35. for each subscriber {
  36. out := subscriber.outc
  37. select {
  38. case out <- msg:
  39. default:
  40. log("subscriber %v buffer is full, skipping...")
  41. }
  42. }
  43. ```
  44. This fixes the "slow client problem", but there is no way for a slow client to
  45. know if it had missed a message. On the other hand, if we're going to stick
  46. with blocking send, **devs must always ensure subscriber's handling code does not
  47. block**. As you can see, there is an implicit choice between ordering guarantees
  48. and using goroutines.
  49. The interim option is to run goroutines pool for a single message, wait for all
  50. goroutines to finish. This will solve "slow client problem", but we'd still
  51. have to wait `max(goroutine_X_time)` before we can publish the next message.
  52. My opinion: not worth doing.
  53. ### Channels vs Callbacks
  54. Yet another question is whether we should use channels for message transmission or
  55. call subscriber-defined callback functions. Callback functions give subscribers
  56. more flexibility - you can use mutexes in there, channels, spawn goroutines,
  57. anything you really want. But they also carry local scope, which can result in
  58. memory leaks and/or memory usage increase.
  59. Go channels are de-facto standard for carrying data between goroutines.
  60. **Question: Is it worth switching to callback functions?**
  61. ### Why `Subscribe()` accepts an `out` channel?
  62. Because in our tests, we create buffered channels (cap: 1). Alternatively, we
  63. can make capacity an argument.
  64. ## Decision
  65. Change Subscribe() function to return out channel:
  66. ```go
  67. // outCap can be used to set capacity of out channel (unbuffered by default).
  68. Subscribe(ctx context.Context, clientID string, query Query, outCap... int) (out <-chan interface{}, err error) {
  69. ```
  70. It's more idiomatic since we're closing it during Unsubscribe/UnsubscribeAll calls.
  71. Also, we should make tags available to subscribers:
  72. ```go
  73. type MsgAndTags struct {
  74. Msg interface{}
  75. Tags TagMap
  76. }
  77. // outCap can be used to set capacity of out channel (unbuffered by default).
  78. Subscribe(ctx context.Context, clientID string, query Query, outCap... int) (out <-chan MsgAndTags, err error) {
  79. ```
  80. ## Status
  81. In review
  82. ## Consequences
  83. ### Positive
  84. - more idiomatic interface
  85. - subscribers know what tags msg was published with
  86. ### Negative
  87. ### Neutral