...

Source file src/go.etcd.io/etcd/server/v3/proxy/httpproxy/proxy.go

Documentation: go.etcd.io/etcd/server/v3/proxy/httpproxy

     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  
    15  package httpproxy
    16  
    17  import (
    18  	"encoding/json"
    19  	"net/http"
    20  	"strings"
    21  	"time"
    22  
    23  	"go.uber.org/zap"
    24  	"golang.org/x/net/http2"
    25  )
    26  
    27  const (
    28  	// DefaultMaxIdleConnsPerHost indicates the default maximum idle connection
    29  	// count maintained between proxy and each member. We set it to 128 to
    30  	// let proxy handle 128 concurrent requests in long term smoothly.
    31  	// If the number of concurrent requests is bigger than this value,
    32  	// proxy needs to create one new connection when handling each request in
    33  	// the delta, which is bad because the creation consumes resource and
    34  	// may eat up ephemeral ports.
    35  	DefaultMaxIdleConnsPerHost = 128
    36  )
    37  
    38  // GetProxyURLs is a function which should return the current set of URLs to
    39  // which client requests should be proxied. This function will be queried
    40  // periodically by the proxy Handler to refresh the set of available
    41  // backends.
    42  type GetProxyURLs func() []string
    43  
    44  // NewHandler creates a new HTTP handler, listening on the given transport,
    45  // which will proxy requests to an etcd cluster.
    46  // The handler will periodically update its view of the cluster.
    47  func NewHandler(lg *zap.Logger, t *http.Transport, urlsFunc GetProxyURLs, failureWait time.Duration, refreshInterval time.Duration) http.Handler {
    48  	if lg == nil {
    49  		lg = zap.NewNop()
    50  	}
    51  	if t.TLSClientConfig != nil {
    52  		// Enable http2, see Issue 5033.
    53  		err := http2.ConfigureTransport(t)
    54  		if err != nil {
    55  			lg.Info("Error enabling Transport HTTP/2 support", zap.Error(err))
    56  		}
    57  	}
    58  
    59  	p := &reverseProxy{
    60  		lg:        lg,
    61  		director:  newDirector(lg, urlsFunc, failureWait, refreshInterval),
    62  		transport: t,
    63  	}
    64  
    65  	mux := http.NewServeMux()
    66  	mux.Handle("/", p)
    67  	mux.HandleFunc("/v2/config/local/proxy", p.configHandler)
    68  
    69  	return mux
    70  }
    71  
    72  // NewReadonlyHandler wraps the given HTTP handler to allow only GET requests
    73  func NewReadonlyHandler(hdlr http.Handler) http.Handler {
    74  	readonly := readonlyHandlerFunc(hdlr)
    75  	return http.HandlerFunc(readonly)
    76  }
    77  
    78  func readonlyHandlerFunc(next http.Handler) func(http.ResponseWriter, *http.Request) {
    79  	return func(w http.ResponseWriter, req *http.Request) {
    80  		if req.Method != "GET" {
    81  			w.WriteHeader(http.StatusNotImplemented)
    82  			return
    83  		}
    84  
    85  		next.ServeHTTP(w, req)
    86  	}
    87  }
    88  
    89  func (p *reverseProxy) configHandler(w http.ResponseWriter, r *http.Request) {
    90  	if !allowMethod(w, r.Method, "GET") {
    91  		return
    92  	}
    93  
    94  	eps := p.director.endpoints()
    95  	epstr := make([]string, len(eps))
    96  	for i, e := range eps {
    97  		epstr[i] = e.URL.String()
    98  	}
    99  
   100  	proxyConfig := struct {
   101  		Endpoints []string `json:"endpoints"`
   102  	}{
   103  		Endpoints: epstr,
   104  	}
   105  
   106  	json.NewEncoder(w).Encode(proxyConfig)
   107  }
   108  
   109  // allowMethod verifies that the given method is one of the allowed methods,
   110  // and if not, it writes an error to w.  A boolean is returned indicating
   111  // whether or not the method is allowed.
   112  func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
   113  	for _, meth := range ms {
   114  		if m == meth {
   115  			return true
   116  		}
   117  	}
   118  	w.Header().Set("Allow", strings.Join(ms, ","))
   119  	http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
   120  	return false
   121  }
   122  

View as plain text