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.

89 lines
2.5 KiB

pubsub 2.0 (#3227) * green pubsub tests :OK: * get rid of clientToQueryMap * Subscribe and SubscribeUnbuffered * start adapting other pkgs to new pubsub * nope * rename MsgAndTags to Message * remove TagMap it does not bring any additional benefits * bring back EventSubscriber * fix test * fix data race in TestStartNextHeightCorrectly ``` Write at 0x00c0001c7418 by goroutine 796: github.com/tendermint/tendermint/consensus.TestStartNextHeightCorrectly() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:1296 +0xad testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Previous read at 0x00c0001c7418 by goroutine 858: github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1631 +0x1366 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1476 +0x8f github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /go/src/github.com/tendermint/tendermint/consensus/state.go:667 +0xa1e github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /go/src/github.com/tendermint/tendermint/consensus/state.go:628 +0x794 Goroutine 796 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1119 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 testing.runTests() /usr/local/go/src/testing/testing.go:1117 +0x4ee testing.(*M).Run() /usr/local/go/src/testing/testing.go:1034 +0x2ee main.main() _testmain.go:214 +0x332 Goroutine 858 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).startRoutines() /go/src/github.com/tendermint/tendermint/consensus/state.go:334 +0x221 github.com/tendermint/tendermint/consensus.startTestRound() /go/src/github.com/tendermint/tendermint/consensus/common_test.go:122 +0x63 github.com/tendermint/tendermint/consensus.TestStateFullRound1() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:255 +0x397 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ``` * fixes after my own review * fix formatting * wait 100ms before kicking a subscriber out + a test for indexer_service * fixes after my second review * no timeout * add changelog entries * fix merge conflicts * fix typos after Thane's review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * reformat code * rewrite indexer service in the attempt to fix failing test https://github.com/tendermint/tendermint/pull/3227/#issuecomment-462316527 * Revert "rewrite indexer service in the attempt to fix failing test" This reverts commit 0d9107a098230de7138abb1c201877c246e89ed1. * another attempt to fix indexer * fixes after Ethan's review * use unbuffered channel when indexing transactions Refs https://github.com/tendermint/tendermint/pull/3227#discussion_r258786716 * add a comment for EventBus#SubscribeUnbuffered * format code
6 years ago
  1. package pubsub
  2. import (
  3. "errors"
  4. "sync"
  5. )
  6. var (
  7. // ErrUnsubscribed is returned by Err when a client unsubscribes.
  8. ErrUnsubscribed = errors.New("client unsubscribed")
  9. // ErrOutOfCapacity is returned by Err when a client is not pulling messages
  10. // fast enough. Note the client's subscription will be terminated.
  11. ErrOutOfCapacity = errors.New("client is not pulling messages fast enough")
  12. )
  13. // A Subscription represents a client subscription for a particular query and
  14. // consists of three things:
  15. // 1) channel onto which messages and tags are published
  16. // 2) channel which is closed if a client is too slow or choose to unsubscribe
  17. // 3) err indicating the reason for (2)
  18. type Subscription struct {
  19. out chan Message
  20. cancelled chan struct{}
  21. mtx sync.RWMutex
  22. err error
  23. }
  24. // NewSubscription returns a new subscription with the given outCapacity.
  25. func NewSubscription(outCapacity int) *Subscription {
  26. return &Subscription{
  27. out: make(chan Message, outCapacity),
  28. cancelled: make(chan struct{}),
  29. }
  30. }
  31. // Out returns a channel onto which messages and tags are published.
  32. // Unsubscribe/UnsubscribeAll does not close the channel to avoid clients from
  33. // receiving a nil message.
  34. func (s *Subscription) Out() <-chan Message {
  35. return s.out
  36. }
  37. // Cancelled returns a channel that's closed when the subscription is
  38. // terminated and supposed to be used in a select statement.
  39. func (s *Subscription) Cancelled() <-chan struct{} {
  40. return s.cancelled
  41. }
  42. // Err returns nil if the channel returned by Cancelled is not yet closed.
  43. // If the channel is closed, Err returns a non-nil error explaining why:
  44. // - ErrUnsubscribed if the subscriber choose to unsubscribe,
  45. // - ErrOutOfCapacity if the subscriber is not pulling messages fast enough
  46. // and the channel returned by Out became full,
  47. // After Err returns a non-nil error, successive calls to Err return the same
  48. // error.
  49. func (s *Subscription) Err() error {
  50. s.mtx.RLock()
  51. defer s.mtx.RUnlock()
  52. return s.err
  53. }
  54. func (s *Subscription) cancel(err error) {
  55. s.mtx.Lock()
  56. s.err = err
  57. s.mtx.Unlock()
  58. close(s.cancelled)
  59. }
  60. // Message glues data and tags together.
  61. type Message struct {
  62. data interface{}
  63. tags map[string]string
  64. }
  65. func NewMessage(data interface{}, tags map[string]string) Message {
  66. return Message{data, tags}
  67. }
  68. // Data returns an original data published.
  69. func (msg Message) Data() interface{} {
  70. return msg.data
  71. }
  72. // Tags returns tags, which matched the client's query.
  73. func (msg Message) Tags() map[string]string {
  74. return msg.tags
  75. }