serve.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  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. m := cmux.New(sctx.l)
  40. if sctx.insecure {
  41. gs := v3rpc.Server(s, nil)
  42. grpcl := m.Match(cmux.HTTP2())
  43. go func() { plog.Fatal(gs.Serve(grpcl)) }()
  44. srvhttp := &http.Server{
  45. Handler: handler,
  46. ErrorLog: logger, // do not log user error
  47. }
  48. httpl := m.Match(cmux.HTTP1())
  49. go func() { plog.Fatal(srvhttp.Serve(httpl)) }()
  50. plog.Noticef("serving insecure client requests on %s, this is strongly discouraged!", sctx.host)
  51. }
  52. if sctx.secure {
  53. gs := v3rpc.Server(s, tlscfg)
  54. handler = grpcHandlerFunc(gs, handler)
  55. tlsl := tls.NewListener(m.Match(cmux.Any()), tlscfg)
  56. // TODO: add debug flag; enable logging when debug flag is set
  57. srv := &http.Server{
  58. Handler: handler,
  59. TLSConfig: tlscfg,
  60. ErrorLog: logger, // do not log user error
  61. }
  62. go func() { plog.Fatal(srv.Serve(tlsl)) }()
  63. plog.Infof("serving client requests on %s", sctx.host)
  64. }
  65. return m.Serve()
  66. }
  67. // grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
  68. // connections or otherHandler otherwise. Copied from cockroachdb.
  69. func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
  70. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
  72. grpcServer.ServeHTTP(w, r)
  73. } else {
  74. otherHandler.ServeHTTP(w, r)
  75. }
  76. })
  77. }
  78. func servePeerHTTP(l net.Listener, handler http.Handler) error {
  79. logger := defaultLog.New(ioutil.Discard, "etcdhttp", 0)
  80. // TODO: add debug flag; enable logging when debug flag is set
  81. srv := &http.Server{
  82. Handler: handler,
  83. ReadTimeout: 5 * time.Minute,
  84. ErrorLog: logger, // do not log user error
  85. }
  86. return srv.Serve(l)
  87. }