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.

91 lines
2.2 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
  1. package rpcserver
  2. import (
  3. "encoding/hex"
  4. "net/http"
  5. "regexp"
  6. "strconv"
  7. "github.com/pkg/errors"
  8. )
  9. var (
  10. // Parts of regular expressions
  11. atom = "[A-Z0-9!#$%&'*+\\-/=?^_`{|}~]+"
  12. dotAtom = atom + `(?:\.` + atom + `)*`
  13. domain = `[A-Z0-9.-]+\.[A-Z]{2,4}`
  14. RE_INT = regexp.MustCompile(`^-?[0-9]+$`)
  15. RE_HEX = regexp.MustCompile(`^(?i)[a-f0-9]+$`)
  16. RE_EMAIL = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`)
  17. RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`)
  18. RE_HOST = regexp.MustCompile(`^(?i)(` + domain + `)$`)
  19. //RE_ID12 = regexp.MustCompile(`^[a-zA-Z0-9]{12}$`)
  20. )
  21. func GetParam(r *http.Request, param string) string {
  22. s := r.URL.Query().Get(param)
  23. if s == "" {
  24. s = r.FormValue(param)
  25. }
  26. return s
  27. }
  28. func GetParamByteSlice(r *http.Request, param string) ([]byte, error) {
  29. s := GetParam(r, param)
  30. return hex.DecodeString(s)
  31. }
  32. func GetParamInt64(r *http.Request, param string) (int64, error) {
  33. s := GetParam(r, param)
  34. i, err := strconv.ParseInt(s, 10, 64)
  35. if err != nil {
  36. return 0, errors.Errorf(param, err.Error())
  37. }
  38. return i, nil
  39. }
  40. func GetParamInt32(r *http.Request, param string) (int32, error) {
  41. s := GetParam(r, param)
  42. i, err := strconv.ParseInt(s, 10, 32)
  43. if err != nil {
  44. return 0, errors.Errorf(param, err.Error())
  45. }
  46. return int32(i), nil
  47. }
  48. func GetParamUint64(r *http.Request, param string) (uint64, error) {
  49. s := GetParam(r, param)
  50. i, err := strconv.ParseUint(s, 10, 64)
  51. if err != nil {
  52. return 0, errors.Errorf(param, err.Error())
  53. }
  54. return i, nil
  55. }
  56. func GetParamUint(r *http.Request, param string) (uint, error) {
  57. s := GetParam(r, param)
  58. i, err := strconv.ParseUint(s, 10, 64)
  59. if err != nil {
  60. return 0, errors.Errorf(param, err.Error())
  61. }
  62. return uint(i), nil
  63. }
  64. func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
  65. s := GetParam(r, param)
  66. if !re.MatchString(s) {
  67. return "", errors.Errorf(param, "did not match regular expression %v", re.String())
  68. }
  69. return s, nil
  70. }
  71. func GetParamFloat64(r *http.Request, param string) (float64, error) {
  72. s := GetParam(r, param)
  73. f, err := strconv.ParseFloat(s, 64)
  74. if err != nil {
  75. return 0, errors.Errorf(param, err.Error())
  76. }
  77. return f, nil
  78. }