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.

178 lines
3.6 KiB

limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
  1. package core
  2. import (
  3. "time"
  4. cfg "github.com/tendermint/tendermint/config"
  5. "github.com/tendermint/tendermint/consensus"
  6. "github.com/tendermint/tendermint/crypto"
  7. dbm "github.com/tendermint/tendermint/libs/db"
  8. "github.com/tendermint/tendermint/libs/log"
  9. mempl "github.com/tendermint/tendermint/mempool"
  10. "github.com/tendermint/tendermint/p2p"
  11. "github.com/tendermint/tendermint/proxy"
  12. sm "github.com/tendermint/tendermint/state"
  13. "github.com/tendermint/tendermint/state/txindex"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. const (
  17. // see README
  18. defaultPerPage = 30
  19. maxPerPage = 100
  20. // SubscribeTimeout is the maximum time we wait to subscribe for an event.
  21. // must be less than the server's write timeout (see rpcserver.DefaultConfig)
  22. SubscribeTimeout = 5 * time.Second
  23. )
  24. //----------------------------------------------
  25. // These interfaces are used by RPC and must be thread safe
  26. type Consensus interface {
  27. GetState() sm.State
  28. GetValidators() (int64, []*types.Validator)
  29. GetLastHeight() int64
  30. GetRoundStateJSON() ([]byte, error)
  31. GetRoundStateSimpleJSON() ([]byte, error)
  32. }
  33. type transport interface {
  34. Listeners() []string
  35. IsListening() bool
  36. NodeInfo() p2p.NodeInfo
  37. }
  38. type peers interface {
  39. DialPeersAsync(p2p.AddrBook, []string, bool) error
  40. NumPeers() (outbound, inbound, dialig int)
  41. Peers() p2p.IPeerSet
  42. }
  43. //----------------------------------------------
  44. // These package level globals come with setters
  45. // that are expected to be called only once, on startup
  46. var (
  47. // external, thread safe interfaces
  48. proxyAppQuery proxy.AppConnQuery
  49. // interfaces defined in types and above
  50. stateDB dbm.DB
  51. blockStore sm.BlockStore
  52. evidencePool sm.EvidencePool
  53. consensusState Consensus
  54. p2pPeers peers
  55. p2pTransport transport
  56. // objects
  57. pubKey crypto.PubKey
  58. genDoc *types.GenesisDoc // cache the genesis structure
  59. addrBook p2p.AddrBook
  60. txIndexer txindex.TxIndexer
  61. consensusReactor *consensus.ConsensusReactor
  62. eventBus *types.EventBus // thread safe
  63. mempool *mempl.Mempool
  64. logger log.Logger
  65. config cfg.RPCConfig
  66. )
  67. func SetStateDB(db dbm.DB) {
  68. stateDB = db
  69. }
  70. func SetBlockStore(bs sm.BlockStore) {
  71. blockStore = bs
  72. }
  73. func SetMempool(mem *mempl.Mempool) {
  74. mempool = mem
  75. }
  76. func SetEvidencePool(evpool sm.EvidencePool) {
  77. evidencePool = evpool
  78. }
  79. func SetConsensusState(cs Consensus) {
  80. consensusState = cs
  81. }
  82. func SetP2PPeers(p peers) {
  83. p2pPeers = p
  84. }
  85. func SetP2PTransport(t transport) {
  86. p2pTransport = t
  87. }
  88. func SetPubKey(pk crypto.PubKey) {
  89. pubKey = pk
  90. }
  91. func SetGenesisDoc(doc *types.GenesisDoc) {
  92. genDoc = doc
  93. }
  94. func SetAddrBook(book p2p.AddrBook) {
  95. addrBook = book
  96. }
  97. func SetProxyAppQuery(appConn proxy.AppConnQuery) {
  98. proxyAppQuery = appConn
  99. }
  100. func SetTxIndexer(indexer txindex.TxIndexer) {
  101. txIndexer = indexer
  102. }
  103. func SetConsensusReactor(conR *consensus.ConsensusReactor) {
  104. consensusReactor = conR
  105. }
  106. func SetLogger(l log.Logger) {
  107. logger = l
  108. }
  109. func SetEventBus(b *types.EventBus) {
  110. eventBus = b
  111. }
  112. // SetConfig sets an RPCConfig.
  113. func SetConfig(c cfg.RPCConfig) {
  114. config = c
  115. }
  116. func validatePage(page, perPage, totalCount int) int {
  117. if perPage < 1 {
  118. return 1
  119. }
  120. pages := ((totalCount - 1) / perPage) + 1
  121. if page < 1 {
  122. page = 1
  123. } else if page > pages {
  124. page = pages
  125. }
  126. return page
  127. }
  128. func validatePerPage(perPage int) int {
  129. if perPage < 1 {
  130. return defaultPerPage
  131. } else if perPage > maxPerPage {
  132. return maxPerPage
  133. }
  134. return perPage
  135. }
  136. func validateSkipCount(page, perPage int) int {
  137. skipCount := (page - 1) * perPage
  138. if skipCount < 0 {
  139. return 0
  140. }
  141. return skipCount
  142. }