endpoints.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package node
  17. import (
  18. "net"
  19. "net/http"
  20. "time"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. // StartHTTPEndpoint starts the HTTP RPC endpoint.
  25. func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.Handler) (*http.Server, net.Addr, error) {
  26. // start the HTTP listener
  27. var (
  28. listener net.Listener
  29. err error
  30. )
  31. if listener, err = net.Listen("tcp", endpoint); err != nil {
  32. return nil, nil, err
  33. }
  34. // make sure timeout values are meaningful
  35. CheckTimeouts(&timeouts)
  36. // Bundle and start the HTTP server
  37. httpSrv := &http.Server{
  38. Handler: handler,
  39. ReadTimeout: timeouts.ReadTimeout,
  40. ReadHeaderTimeout: timeouts.ReadHeaderTimeout,
  41. WriteTimeout: timeouts.WriteTimeout,
  42. IdleTimeout: timeouts.IdleTimeout,
  43. }
  44. go httpSrv.Serve(listener)
  45. return httpSrv, listener.Addr(), err
  46. }
  47. // checkModuleAvailability checks that all names given in modules are actually
  48. // available API services. It assumes that the MetadataApi module ("rpc") is always available;
  49. // the registration of this "rpc" module happens in NewServer() and is thus common to all endpoints.
  50. func checkModuleAvailability(modules []string, apis []rpc.API) (bad, available []string) {
  51. availableSet := make(map[string]struct{})
  52. for _, api := range apis {
  53. if _, ok := availableSet[api.Namespace]; !ok {
  54. availableSet[api.Namespace] = struct{}{}
  55. available = append(available, api.Namespace)
  56. }
  57. }
  58. for _, name := range modules {
  59. if _, ok := availableSet[name]; !ok {
  60. if name != rpc.MetadataApi && name != rpc.EngineApi {
  61. bad = append(bad, name)
  62. }
  63. }
  64. }
  65. return bad, available
  66. }
  67. // CheckTimeouts ensures that timeout values are meaningful
  68. func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
  69. if timeouts.ReadTimeout < time.Second {
  70. log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
  71. timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
  72. }
  73. if timeouts.ReadHeaderTimeout < time.Second {
  74. log.Warn("Sanitizing invalid HTTP read header timeout", "provided", timeouts.ReadHeaderTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadHeaderTimeout)
  75. timeouts.ReadHeaderTimeout = rpc.DefaultHTTPTimeouts.ReadHeaderTimeout
  76. }
  77. if timeouts.WriteTimeout < time.Second {
  78. log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
  79. timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout
  80. }
  81. if timeouts.IdleTimeout < time.Second {
  82. log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", rpc.DefaultHTTPTimeouts.IdleTimeout)
  83. timeouts.IdleTimeout = rpc.DefaultHTTPTimeouts.IdleTimeout
  84. }
  85. }