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.

207 lines
4.9 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
  1. package core
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "time"
  6. cfg "github.com/tendermint/tendermint/config"
  7. "github.com/tendermint/tendermint/consensus"
  8. "github.com/tendermint/tendermint/crypto"
  9. tmjson "github.com/tendermint/tendermint/libs/json"
  10. "github.com/tendermint/tendermint/libs/log"
  11. mempl "github.com/tendermint/tendermint/mempool"
  12. "github.com/tendermint/tendermint/p2p"
  13. "github.com/tendermint/tendermint/proxy"
  14. sm "github.com/tendermint/tendermint/state"
  15. "github.com/tendermint/tendermint/state/indexer"
  16. "github.com/tendermint/tendermint/state/txindex"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. const (
  20. // see README
  21. defaultPerPage = 30
  22. maxPerPage = 100
  23. // SubscribeTimeout is the maximum time we wait to subscribe for an event.
  24. // must be less than the server's write timeout (see rpcserver.DefaultConfig)
  25. SubscribeTimeout = 5 * time.Second
  26. // genesisChunkSize is the maximum size, in bytes, of each
  27. // chunk in the genesis structure for the chunked API
  28. genesisChunkSize = 16 * 1024 * 1024 // 16
  29. )
  30. var (
  31. // set by Node
  32. env *Environment
  33. )
  34. // SetEnvironment sets up the given Environment.
  35. // It will race if multiple Node call SetEnvironment.
  36. func SetEnvironment(e *Environment) {
  37. env = e
  38. }
  39. //----------------------------------------------
  40. // These interfaces are used by RPC and must be thread safe
  41. type Consensus interface {
  42. GetState() sm.State
  43. GetValidators() (int64, []*types.Validator)
  44. GetLastHeight() int64
  45. GetRoundStateJSON() ([]byte, error)
  46. GetRoundStateSimpleJSON() ([]byte, error)
  47. }
  48. type transport interface {
  49. Listeners() []string
  50. IsListening() bool
  51. NodeInfo() p2p.NodeInfo
  52. }
  53. type peers interface {
  54. AddPersistentPeers([]string) error
  55. AddUnconditionalPeerIDs([]string) error
  56. AddPrivatePeerIDs([]string) error
  57. DialPeersAsync([]string) error
  58. Peers() p2p.IPeerSet
  59. }
  60. //----------------------------------------------
  61. // Environment contains objects and interfaces used by the RPC. It is expected
  62. // to be setup once during startup.
  63. type Environment struct {
  64. // external, thread safe interfaces
  65. ProxyAppQuery proxy.AppConnQuery
  66. ProxyAppMempool proxy.AppConnMempool
  67. // interfaces defined in types and above
  68. StateStore sm.Store
  69. BlockStore sm.BlockStore
  70. EvidencePool sm.EvidencePool
  71. ConsensusState Consensus
  72. P2PPeers peers
  73. P2PTransport transport
  74. // objects
  75. PubKey crypto.PubKey
  76. GenDoc *types.GenesisDoc // cache the genesis structure
  77. TxIndexer txindex.TxIndexer
  78. BlockIndexer indexer.BlockIndexer
  79. ConsensusReactor *consensus.Reactor
  80. EventBus *types.EventBus // thread safe
  81. Mempool mempl.Mempool
  82. Logger log.Logger
  83. Config cfg.RPCConfig
  84. // cache of chunked genesis data.
  85. genChunks []string
  86. }
  87. //----------------------------------------------
  88. func validatePage(pagePtr *int, perPage, totalCount int) (int, error) {
  89. if perPage < 1 {
  90. panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
  91. }
  92. if pagePtr == nil { // no page parameter
  93. return 1, nil
  94. }
  95. pages := ((totalCount - 1) / perPage) + 1
  96. if pages == 0 {
  97. pages = 1 // one page (even if it's empty)
  98. }
  99. page := *pagePtr
  100. if page <= 0 || page > pages {
  101. return 1, fmt.Errorf("page should be within [1, %d] range, given %d", pages, page)
  102. }
  103. return page, nil
  104. }
  105. func validatePerPage(perPagePtr *int) int {
  106. if perPagePtr == nil { // no per_page parameter
  107. return defaultPerPage
  108. }
  109. perPage := *perPagePtr
  110. if perPage < 1 {
  111. return defaultPerPage
  112. } else if perPage > maxPerPage {
  113. return maxPerPage
  114. }
  115. return perPage
  116. }
  117. // InitGenesisChunks configures the environment and should be called on service
  118. // startup.
  119. func InitGenesisChunks() error {
  120. if env.genChunks != nil {
  121. return nil
  122. }
  123. if env.GenDoc == nil {
  124. return nil
  125. }
  126. data, err := tmjson.Marshal(env.GenDoc)
  127. if err != nil {
  128. return err
  129. }
  130. for i := 0; i < len(data); i += genesisChunkSize {
  131. end := i + genesisChunkSize
  132. if end > len(data) {
  133. end = len(data)
  134. }
  135. env.genChunks = append(env.genChunks, base64.StdEncoding.EncodeToString(data[i:end]))
  136. }
  137. return nil
  138. }
  139. func validateSkipCount(page, perPage int) int {
  140. skipCount := (page - 1) * perPage
  141. if skipCount < 0 {
  142. return 0
  143. }
  144. return skipCount
  145. }
  146. // latestHeight can be either latest committed or uncommitted (+1) height.
  147. func getHeight(latestHeight int64, heightPtr *int64) (int64, error) {
  148. if heightPtr != nil {
  149. height := *heightPtr
  150. if height <= 0 {
  151. return 0, fmt.Errorf("height must be greater than 0, but got %d", height)
  152. }
  153. if height > latestHeight {
  154. return 0, fmt.Errorf("height %d must be less than or equal to the current blockchain height %d",
  155. height, latestHeight)
  156. }
  157. base := env.BlockStore.Base()
  158. if height < base {
  159. return 0, fmt.Errorf("height %d is not available, lowest height is %d",
  160. height, base)
  161. }
  162. return height, nil
  163. }
  164. return latestHeight, nil
  165. }
  166. func latestUncommittedHeight() int64 {
  167. nodeIsSyncing := env.ConsensusReactor.WaitSync()
  168. if nodeIsSyncing {
  169. return env.BlockStore.Height()
  170. }
  171. return env.BlockStore.Height() + 1
  172. }