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.

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