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.

281 lines
8.0 KiB

rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
8 years ago
8 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
  1. // Commons for HTTP handling
  2. package server
  3. import (
  4. "bufio"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "net"
  9. "net/http"
  10. "os"
  11. "runtime/debug"
  12. "strings"
  13. "time"
  14. "golang.org/x/net/netutil"
  15. "github.com/tendermint/tendermint/libs/log"
  16. types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  17. )
  18. // Config is a RPC server configuration.
  19. type Config struct {
  20. // see netutil.LimitListener
  21. MaxOpenConnections int
  22. // mirrors http.Server#ReadTimeout
  23. ReadTimeout time.Duration
  24. // mirrors http.Server#WriteTimeout
  25. WriteTimeout time.Duration
  26. // MaxBodyBytes controls the maximum number of bytes the
  27. // server will read parsing the request body.
  28. MaxBodyBytes int64
  29. // mirrors http.Server#MaxHeaderBytes
  30. MaxHeaderBytes int
  31. }
  32. // DefaultConfig returns a default configuration.
  33. func DefaultConfig() *Config {
  34. return &Config{
  35. MaxOpenConnections: 0, // unlimited
  36. ReadTimeout: 10 * time.Second,
  37. WriteTimeout: 10 * time.Second,
  38. MaxBodyBytes: int64(1000000), // 1MB
  39. MaxHeaderBytes: 1 << 20, // same as the net/http default
  40. }
  41. }
  42. // Serve creates a http.Server and calls Serve with the given listener. It
  43. // wraps handler with RecoverAndLogHandler and a handler, which limits the max
  44. // body size to config.MaxBodyBytes.
  45. //
  46. // NOTE: This function blocks - you may want to call it in a go-routine.
  47. func Serve(listener net.Listener, handler http.Handler, logger log.Logger, config *Config) error {
  48. logger.Info(fmt.Sprintf("Starting RPC HTTP server on %s", listener.Addr()))
  49. s := &http.Server{
  50. Handler: RecoverAndLogHandler(maxBytesHandler{h: handler, n: config.MaxBodyBytes}, logger),
  51. ReadTimeout: config.ReadTimeout,
  52. WriteTimeout: config.WriteTimeout,
  53. MaxHeaderBytes: config.MaxHeaderBytes,
  54. }
  55. err := s.Serve(listener)
  56. logger.Info("RPC HTTP server stopped", "err", err)
  57. return err
  58. }
  59. // Serve creates a http.Server and calls ServeTLS with the given listener,
  60. // certFile and keyFile. It wraps handler with RecoverAndLogHandler and a
  61. // handler, which limits the max body size to config.MaxBodyBytes.
  62. //
  63. // NOTE: This function blocks - you may want to call it in a go-routine.
  64. func ServeTLS(
  65. listener net.Listener,
  66. handler http.Handler,
  67. certFile, keyFile string,
  68. logger log.Logger,
  69. config *Config,
  70. ) error {
  71. logger.Info(fmt.Sprintf("Starting RPC HTTPS server on %s (cert: %q, key: %q)",
  72. listener.Addr(), certFile, keyFile))
  73. s := &http.Server{
  74. Handler: RecoverAndLogHandler(maxBytesHandler{h: handler, n: config.MaxBodyBytes}, logger),
  75. ReadTimeout: config.ReadTimeout,
  76. WriteTimeout: config.WriteTimeout,
  77. MaxHeaderBytes: config.MaxHeaderBytes,
  78. }
  79. err := s.ServeTLS(listener, certFile, keyFile)
  80. logger.Error("RPC HTTPS server stopped", "err", err)
  81. return err
  82. }
  83. // WriteRPCResponseHTTPError marshals res as JSON and writes it to w.
  84. //
  85. // Maps JSON RPC error codes to HTTP Status codes as follows:
  86. //
  87. // HTTP Status code message
  88. // 500 -32700 Parse error.
  89. // 400 -32600 Invalid Request.
  90. // 404 -32601 Method not found.
  91. // 500 -32602 Invalid params.
  92. // 500 -32603 Internal error.
  93. // 500 -32099..-32000 Server error.
  94. //
  95. // source: https://www.jsonrpc.org/historical/json-rpc-over-http.html
  96. //
  97. // Panics if it can't Marshal res or write to w.
  98. func WriteRPCResponseHTTPError(
  99. w http.ResponseWriter,
  100. res types.RPCResponse,
  101. ) {
  102. if res.Error == nil {
  103. panic("tried to write http error response without RPC error")
  104. }
  105. jsonBytes, err := json.MarshalIndent(res, "", " ")
  106. if err != nil {
  107. panic(err)
  108. }
  109. var httpCode int
  110. switch res.Error.Code {
  111. case -32600:
  112. httpCode = http.StatusBadRequest
  113. case -32601:
  114. httpCode = http.StatusNotFound
  115. default:
  116. httpCode = http.StatusInternalServerError
  117. }
  118. w.Header().Set("Content-Type", "application/json")
  119. w.WriteHeader(httpCode)
  120. if _, err := w.Write(jsonBytes); err != nil {
  121. panic(err)
  122. }
  123. }
  124. // WriteRPCResponseHTTP marshals res as JSON and writes it to w.
  125. //
  126. // Panics if it can't Marshal res or write to w.
  127. func WriteRPCResponseHTTP(w http.ResponseWriter, res ...types.RPCResponse) {
  128. var v interface{}
  129. if len(res) == 1 {
  130. v = res[0]
  131. } else {
  132. v = res
  133. }
  134. jsonBytes, err := json.MarshalIndent(v, "", " ")
  135. if err != nil {
  136. panic(err)
  137. }
  138. w.Header().Set("Content-Type", "application/json")
  139. w.WriteHeader(200)
  140. if _, err := w.Write(jsonBytes); err != nil {
  141. panic(err)
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. // RecoverAndLogHandler wraps an HTTP handler, adding error logging.
  146. // If the inner function panics, the outer function recovers, logs, sends an
  147. // HTTP 500 error response.
  148. func RecoverAndLogHandler(handler http.Handler, logger log.Logger) http.Handler {
  149. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  150. // Wrap the ResponseWriter to remember the status
  151. rww := &responseWriterWrapper{-1, w}
  152. begin := time.Now()
  153. rww.Header().Set("X-Server-Time", fmt.Sprintf("%v", begin.Unix()))
  154. defer func() {
  155. // Handle any panics in the panic handler below. Does not use the logger, since we want
  156. // to avoid any further panics. However, we try to return a 500, since it otherwise
  157. // defaults to 200 and there is no other way to terminate the connection. If that
  158. // should panic for whatever reason then the Go HTTP server will handle it and
  159. // terminate the connection - panicing is the de-facto and only way to get the Go HTTP
  160. // server to terminate the request and close the connection/stream:
  161. // https://github.com/golang/go/issues/17790#issuecomment-258481416
  162. if e := recover(); e != nil {
  163. fmt.Fprintf(os.Stderr, "Panic during RPC panic recovery: %v\n%v\n", e, string(debug.Stack()))
  164. w.WriteHeader(500)
  165. }
  166. }()
  167. defer func() {
  168. // Send a 500 error if a panic happens during a handler.
  169. // Without this, Chrome & Firefox were retrying aborted ajax requests,
  170. // at least to my localhost.
  171. if e := recover(); e != nil {
  172. // If RPCResponse
  173. if res, ok := e.(types.RPCResponse); ok {
  174. WriteRPCResponseHTTP(rww, res)
  175. } else {
  176. // Panics can contain anything, attempt to normalize it as an error.
  177. var err error
  178. switch e := e.(type) {
  179. case error:
  180. err = e
  181. case string:
  182. err = errors.New(e)
  183. case fmt.Stringer:
  184. err = errors.New(e.String())
  185. default:
  186. }
  187. logger.Error(
  188. "Panic in RPC HTTP handler", "err", e, "stack",
  189. string(debug.Stack()),
  190. )
  191. WriteRPCResponseHTTPError(
  192. rww,
  193. types.RPCInternalError(types.JSONRPCIntID(-1), err),
  194. )
  195. }
  196. }
  197. // Finally, log.
  198. durationMS := time.Since(begin).Nanoseconds() / 1000000
  199. if rww.Status == -1 {
  200. rww.Status = 200
  201. }
  202. logger.Info("Served RPC HTTP response",
  203. "method", r.Method, "url", r.URL,
  204. "status", rww.Status, "duration", durationMS,
  205. "remoteAddr", r.RemoteAddr,
  206. )
  207. }()
  208. handler.ServeHTTP(rww, r)
  209. })
  210. }
  211. // Remember the status for logging
  212. type responseWriterWrapper struct {
  213. Status int
  214. http.ResponseWriter
  215. }
  216. func (w *responseWriterWrapper) WriteHeader(status int) {
  217. w.Status = status
  218. w.ResponseWriter.WriteHeader(status)
  219. }
  220. // implements http.Hijacker
  221. func (w *responseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  222. return w.ResponseWriter.(http.Hijacker).Hijack()
  223. }
  224. type maxBytesHandler struct {
  225. h http.Handler
  226. n int64
  227. }
  228. func (h maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  229. r.Body = http.MaxBytesReader(w, r.Body, h.n)
  230. h.h.ServeHTTP(w, r)
  231. }
  232. // Listen starts a new net.Listener on the given address.
  233. // It returns an error if the address is invalid or the call to Listen() fails.
  234. func Listen(addr string, config *Config) (listener net.Listener, err error) {
  235. parts := strings.SplitN(addr, "://", 2)
  236. if len(parts) != 2 {
  237. return nil, fmt.Errorf(
  238. "invalid listening address %s (use fully formed addresses, including the tcp:// or unix:// prefix)",
  239. addr,
  240. )
  241. }
  242. proto, addr := parts[0], parts[1]
  243. listener, err = net.Listen(proto, addr)
  244. if err != nil {
  245. return nil, fmt.Errorf("failed to listen on %v: %v", addr, err)
  246. }
  247. if config.MaxOpenConnections > 0 {
  248. listener = netutil.LimitListener(listener, config.MaxOpenConnections)
  249. }
  250. return listener, nil
  251. }