endpoints.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2018 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. WriteTimeout: timeouts.WriteTimeout,
  41. IdleTimeout: timeouts.IdleTimeout,
  42. }
  43. go httpSrv.Serve(listener)
  44. return httpSrv, listener.Addr(), err
  45. }
  46. // checkModuleAvailability checks that all names given in modules are actually
  47. // available API services. It assumes that the MetadataApi module ("rpc") is always available;
  48. // the registration of this "rpc" module happens in NewServer() and is thus common to all endpoints.
  49. func checkModuleAvailability(modules []string, apis []rpc.API) (bad, available []string) {
  50. availableSet := make(map[string]struct{})
  51. for _, api := range apis {
  52. if _, ok := availableSet[api.Namespace]; !ok {
  53. availableSet[api.Namespace] = struct{}{}
  54. available = append(available, api.Namespace)
  55. }
  56. }
  57. for _, name := range modules {
  58. if _, ok := availableSet[name]; !ok && name != rpc.MetadataApi {
  59. bad = append(bad, name)
  60. }
  61. }
  62. return bad, available
  63. }
  64. // CheckTimeouts ensures that timeout values are meaningful
  65. func CheckTimeouts(timeouts *rpc.HTTPTimeouts) {
  66. if timeouts.ReadTimeout < time.Second {
  67. log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", rpc.DefaultHTTPTimeouts.ReadTimeout)
  68. timeouts.ReadTimeout = rpc.DefaultHTTPTimeouts.ReadTimeout
  69. }
  70. if timeouts.WriteTimeout < time.Second {
  71. log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", rpc.DefaultHTTPTimeouts.WriteTimeout)
  72. timeouts.WriteTimeout = rpc.DefaultHTTPTimeouts.WriteTimeout
  73. }
  74. if timeouts.IdleTimeout < time.Second {
  75. log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", rpc.DefaultHTTPTimeouts.IdleTimeout)
  76. timeouts.IdleTimeout = rpc.DefaultHTTPTimeouts.IdleTimeout
  77. }
  78. }