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.

182 lines
6.1 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>
5 years ago
5 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>
5 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>
5 years ago
  1. package proxy
  2. import (
  3. "context"
  4. "net/http"
  5. amino "github.com/tendermint/go-amino"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/rpc/client"
  9. rpcclient "github.com/tendermint/tendermint/rpc/client"
  10. "github.com/tendermint/tendermint/rpc/core"
  11. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  12. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  13. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. const (
  17. wsEndpoint = "/websocket"
  18. )
  19. // StartProxy will start the websocket manager on the client,
  20. // set up the rpc routes to proxy via the given client,
  21. // and start up an http/rpc server on the location given by bind (eg. :1234)
  22. // NOTE: This function blocks - you may want to call it in a go-routine.
  23. func StartProxy(c rpcclient.Client, listenAddr string, logger log.Logger, maxOpenConnections int) error {
  24. err := c.Start()
  25. if err != nil {
  26. return err
  27. }
  28. cdc := amino.NewCodec()
  29. ctypes.RegisterAmino(cdc)
  30. r := RPCRoutes(c)
  31. // build the handler...
  32. mux := http.NewServeMux()
  33. rpcserver.RegisterRPCFuncs(mux, r, cdc, logger)
  34. unsubscribeFromAllEvents := func(remoteAddr string) {
  35. if err := c.UnsubscribeAll(context.Background(), remoteAddr); err != nil {
  36. logger.Error("Failed to unsubscribe from events", "err", err)
  37. }
  38. }
  39. wm := rpcserver.NewWebsocketManager(r, cdc, rpcserver.OnDisconnect(unsubscribeFromAllEvents))
  40. wm.SetLogger(logger)
  41. core.SetLogger(logger)
  42. mux.HandleFunc(wsEndpoint, wm.WebsocketHandler)
  43. config := rpcserver.DefaultConfig()
  44. config.MaxOpenConnections = maxOpenConnections
  45. l, err := rpcserver.Listen(listenAddr, config)
  46. if err != nil {
  47. return err
  48. }
  49. return rpcserver.StartHTTPServer(l, mux, logger, config)
  50. }
  51. // RPCRoutes just routes everything to the given client, as if it were
  52. // a tendermint fullnode.
  53. //
  54. // if we want security, the client must implement it as a secure client
  55. func RPCRoutes(c rpcclient.Client) map[string]*rpcserver.RPCFunc {
  56. return map[string]*rpcserver.RPCFunc{
  57. // Subscribe/unsubscribe are reserved for websocket events.
  58. "subscribe": rpcserver.NewWSRPCFunc(c.(Wrapper).SubscribeWS, "query"),
  59. "unsubscribe": rpcserver.NewWSRPCFunc(c.(Wrapper).UnsubscribeWS, "query"),
  60. "unsubscribe_all": rpcserver.NewWSRPCFunc(c.(Wrapper).UnsubscribeAllWS, ""),
  61. // info API
  62. "status": rpcserver.NewRPCFunc(makeStatusFunc(c), ""),
  63. "blockchain": rpcserver.NewRPCFunc(makeBlockchainInfoFunc(c), "minHeight,maxHeight"),
  64. "genesis": rpcserver.NewRPCFunc(makeGenesisFunc(c), ""),
  65. "block": rpcserver.NewRPCFunc(makeBlockFunc(c), "height"),
  66. "commit": rpcserver.NewRPCFunc(makeCommitFunc(c), "height"),
  67. "tx": rpcserver.NewRPCFunc(makeTxFunc(c), "hash,prove"),
  68. "validators": rpcserver.NewRPCFunc(makeValidatorsFunc(c), "height"),
  69. // broadcast API
  70. "broadcast_tx_commit": rpcserver.NewRPCFunc(makeBroadcastTxCommitFunc(c), "tx"),
  71. "broadcast_tx_sync": rpcserver.NewRPCFunc(makeBroadcastTxSyncFunc(c), "tx"),
  72. "broadcast_tx_async": rpcserver.NewRPCFunc(makeBroadcastTxAsyncFunc(c), "tx"),
  73. // abci API
  74. "abci_query": rpcserver.NewRPCFunc(makeABCIQueryFunc(c), "path,data"),
  75. "abci_info": rpcserver.NewRPCFunc(makeABCIInfoFunc(c), ""),
  76. }
  77. }
  78. func makeStatusFunc(c client.StatusClient) func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
  79. return func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
  80. return c.Status()
  81. }
  82. }
  83. func makeBlockchainInfoFunc(c rpcclient.Client) func(
  84. ctx *rpctypes.Context,
  85. minHeight,
  86. maxHeight int64,
  87. ) (*ctypes.ResultBlockchainInfo, error) {
  88. return func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  89. return c.BlockchainInfo(minHeight, maxHeight)
  90. }
  91. }
  92. func makeGenesisFunc(c rpcclient.Client) func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
  93. return func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
  94. return c.Genesis()
  95. }
  96. }
  97. func makeBlockFunc(c rpcclient.Client) func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error) {
  98. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error) {
  99. return c.Block(height)
  100. }
  101. }
  102. func makeCommitFunc(c rpcclient.Client) func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error) {
  103. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error) {
  104. return c.Commit(height)
  105. }
  106. }
  107. func makeTxFunc(c rpcclient.Client) func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
  108. return func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
  109. return c.Tx(hash, prove)
  110. }
  111. }
  112. func makeValidatorsFunc(c rpcclient.Client) func(
  113. ctx *rpctypes.Context,
  114. height *int64,
  115. ) (*ctypes.ResultValidators, error) {
  116. return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultValidators, error) {
  117. return c.Validators(height)
  118. }
  119. }
  120. func makeBroadcastTxCommitFunc(c rpcclient.Client) func(
  121. ctx *rpctypes.Context,
  122. tx types.Tx,
  123. ) (*ctypes.ResultBroadcastTxCommit, error) {
  124. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  125. return c.BroadcastTxCommit(tx)
  126. }
  127. }
  128. func makeBroadcastTxSyncFunc(c rpcclient.Client) func(
  129. ctx *rpctypes.Context,
  130. tx types.Tx,
  131. ) (*ctypes.ResultBroadcastTx, error) {
  132. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  133. return c.BroadcastTxSync(tx)
  134. }
  135. }
  136. func makeBroadcastTxAsyncFunc(c rpcclient.Client) func(
  137. ctx *rpctypes.Context,
  138. tx types.Tx,
  139. ) (*ctypes.ResultBroadcastTx, error) {
  140. return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  141. return c.BroadcastTxAsync(tx)
  142. }
  143. }
  144. func makeABCIQueryFunc(c rpcclient.Client) func(
  145. ctx *rpctypes.Context,
  146. path string,
  147. data cmn.HexBytes,
  148. ) (*ctypes.ResultABCIQuery, error) {
  149. return func(ctx *rpctypes.Context, path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error) {
  150. return c.ABCIQuery(path, data)
  151. }
  152. }
  153. func makeABCIInfoFunc(c rpcclient.Client) func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error) {
  154. return func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error) {
  155. return c.ABCIInfo()
  156. }
  157. }