serve.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdmain
  15. import (
  16. "crypto/tls"
  17. "io/ioutil"
  18. defaultLog "log"
  19. "net"
  20. "net/http"
  21. "strings"
  22. "time"
  23. "github.com/cockroachdb/cmux"
  24. "github.com/coreos/etcd/etcdserver"
  25. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  26. "google.golang.org/grpc"
  27. )
  28. type serveCtx struct {
  29. l net.Listener
  30. host string
  31. secure bool
  32. insecure bool
  33. }
  34. // serve accepts incoming connections on the listener l,
  35. // creating a new service goroutine for each. The service goroutines
  36. // read requests and then call handler to reply to them.
  37. func serve(sctx *serveCtx, s *etcdserver.EtcdServer, tlscfg *tls.Config, handler http.Handler) error {
  38. logger := defaultLog.New(ioutil.Discard, "etcdhttp", 0)
  39. <-s.ReadyNotify()
  40. plog.Info("ready to serve client requests")
  41. m := cmux.New(sctx.l)
  42. if sctx.insecure {
  43. gs := v3rpc.Server(s, nil)
  44. grpcl := m.Match(cmux.HTTP2())
  45. go func() { plog.Fatal(gs.Serve(grpcl)) }()
  46. srvhttp := &http.Server{
  47. Handler: handler,
  48. ErrorLog: logger, // do not log user error
  49. }
  50. httpl := m.Match(cmux.HTTP1())
  51. go func() { plog.Fatal(srvhttp.Serve(httpl)) }()
  52. plog.Noticef("serving insecure client requests on %s, this is strongly discouraged!", sctx.host)
  53. }
  54. if sctx.secure {
  55. gs := v3rpc.Server(s, tlscfg)
  56. handler = grpcHandlerFunc(gs, handler)
  57. tlsl := tls.NewListener(m.Match(cmux.Any()), tlscfg)
  58. // TODO: add debug flag; enable logging when debug flag is set
  59. srv := &http.Server{
  60. Handler: handler,
  61. TLSConfig: tlscfg,
  62. ErrorLog: logger, // do not log user error
  63. }
  64. go func() { plog.Fatal(srv.Serve(tlsl)) }()
  65. plog.Infof("serving client requests on %s", sctx.host)
  66. }
  67. return m.Serve()
  68. }
  69. // grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
  70. // connections or otherHandler otherwise. Copied from cockroachdb.
  71. func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
  72. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  73. if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
  74. grpcServer.ServeHTTP(w, r)
  75. } else {
  76. otherHandler.ServeHTTP(w, r)
  77. }
  78. })
  79. }
  80. func servePeerHTTP(l net.Listener, handler http.Handler) error {
  81. logger := defaultLog.New(ioutil.Discard, "etcdhttp", 0)
  82. // TODO: add debug flag; enable logging when debug flag is set
  83. srv := &http.Server{
  84. Handler: handler,
  85. ReadTimeout: 5 * time.Minute,
  86. ErrorLog: logger, // do not log user error
  87. }
  88. return srv.Serve(l)
  89. }