2019-09-04 06:56:09 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package reverseproxy
|
|
|
|
|
|
|
|
import (
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
"context"
|
2019-09-04 06:56:09 +08:00
|
|
|
"fmt"
|
2019-11-12 06:33:38 +08:00
|
|
|
"strconv"
|
2019-09-04 06:56:09 +08:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-09-04 06:56:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Host represents a remote host which can be proxied to.
|
|
|
|
// Its methods must be safe for concurrent use.
|
|
|
|
type Host interface {
|
|
|
|
// NumRequests returns the numnber of requests
|
|
|
|
// currently in process with the host.
|
|
|
|
NumRequests() int
|
|
|
|
|
|
|
|
// Fails returns the count of recent failures.
|
|
|
|
Fails() int
|
|
|
|
|
|
|
|
// Unhealthy returns true if the backend is unhealthy.
|
|
|
|
Unhealthy() bool
|
|
|
|
|
2019-09-10 11:44:58 +08:00
|
|
|
// CountRequest atomically counts the given number of
|
|
|
|
// requests as currently in process with the host. The
|
|
|
|
// count should not go below 0.
|
2019-09-04 06:56:09 +08:00
|
|
|
CountRequest(int) error
|
|
|
|
|
2019-09-10 11:44:58 +08:00
|
|
|
// CountFail atomically counts the given number of
|
|
|
|
// failures with the host. The count should not go
|
|
|
|
// below 0.
|
2019-09-04 06:56:09 +08:00
|
|
|
CountFail(int) error
|
|
|
|
|
2019-09-10 11:44:58 +08:00
|
|
|
// SetHealthy atomically marks the host as either
|
|
|
|
// healthy (true) or unhealthy (false). If the given
|
|
|
|
// status is the same, this should be a no-op and
|
|
|
|
// return false. It returns true if the status was
|
|
|
|
// changed; i.e. if it is now different from before.
|
2019-09-04 06:56:09 +08:00
|
|
|
SetHealthy(bool) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpstreamPool is a collection of upstreams.
|
|
|
|
type UpstreamPool []*Upstream
|
|
|
|
|
|
|
|
// Upstream bridges this proxy's configuration to the
|
|
|
|
// state of the backend host it is correlated with.
|
|
|
|
type Upstream struct {
|
|
|
|
Host `json:"-"`
|
|
|
|
|
2019-12-24 03:45:35 +08:00
|
|
|
// The [network address](/docs/json/apps/http/#servers/listen)
|
|
|
|
// to dial to connect to the upstream. Must represent precisely
|
|
|
|
// one socket (i.e. no port ranges). A valid network address
|
|
|
|
// either has a host and port, or is a unix socket address.
|
|
|
|
//
|
|
|
|
// Placeholders may be used to make the upstream dynamic, but be
|
|
|
|
// aware of the health check implications of this: a single
|
|
|
|
// upstream that represents numerous (perhaps arbitrary) backends
|
|
|
|
// can be considered down if one or enough of the arbitrary
|
|
|
|
// backends is down. Also be aware of open proxy vulnerabilities.
|
|
|
|
Dial string `json:"dial,omitempty"`
|
|
|
|
|
|
|
|
// The maximum number of simultaneous requests to allow to
|
|
|
|
// this upstream. If set, overrides the global passive health
|
|
|
|
// check UnhealthyRequestCount value.
|
|
|
|
MaxRequests int `json:"max_requests,omitempty"`
|
2019-09-04 06:56:09 +08:00
|
|
|
|
|
|
|
// TODO: This could be really useful, to bind requests
|
|
|
|
// with certain properties to specific backends
|
|
|
|
// HeaderAffinity string
|
|
|
|
// IPAffinity string
|
|
|
|
|
|
|
|
healthCheckPolicy *PassiveHealthChecks
|
2019-09-04 09:06:54 +08:00
|
|
|
cb CircuitBreaker
|
2019-09-04 06:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Available returns true if the remote host
|
2019-09-04 09:06:54 +08:00
|
|
|
// is available to receive requests. This is
|
|
|
|
// the method that should be used by selection
|
|
|
|
// policies, etc. to determine if a backend
|
|
|
|
// should be able to be sent a request.
|
2019-09-04 06:56:09 +08:00
|
|
|
func (u *Upstream) Available() bool {
|
|
|
|
return u.Healthy() && !u.Full()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Healthy returns true if the remote host
|
|
|
|
// is currently known to be healthy or "up".
|
2019-09-04 09:06:54 +08:00
|
|
|
// It consults the circuit breaker, if any.
|
2019-09-04 06:56:09 +08:00
|
|
|
func (u *Upstream) Healthy() bool {
|
|
|
|
healthy := !u.Host.Unhealthy()
|
|
|
|
if healthy && u.healthCheckPolicy != nil {
|
|
|
|
healthy = u.Host.Fails() < u.healthCheckPolicy.MaxFails
|
|
|
|
}
|
2019-09-04 09:06:54 +08:00
|
|
|
if healthy && u.cb != nil {
|
|
|
|
healthy = u.cb.OK()
|
|
|
|
}
|
2019-09-04 06:56:09 +08:00
|
|
|
return healthy
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full returns true if the remote host
|
|
|
|
// cannot receive more requests at this time.
|
|
|
|
func (u *Upstream) Full() bool {
|
|
|
|
return u.MaxRequests > 0 && u.Host.NumRequests() >= u.MaxRequests
|
|
|
|
}
|
|
|
|
|
|
|
|
// upstreamHost is the basic, in-memory representation
|
|
|
|
// of the state of a remote host. It implements the
|
|
|
|
// Host interface.
|
|
|
|
type upstreamHost struct {
|
|
|
|
numRequests int64 // must be first field to be 64-bit aligned on 32-bit systems (see https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
|
|
|
|
fails int64
|
|
|
|
unhealthy int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumRequests returns the number of active requests to the upstream.
|
|
|
|
func (uh *upstreamHost) NumRequests() int {
|
|
|
|
return int(atomic.LoadInt64(&uh.numRequests))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fails returns the number of recent failures with the upstream.
|
|
|
|
func (uh *upstreamHost) Fails() int {
|
|
|
|
return int(atomic.LoadInt64(&uh.fails))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unhealthy returns whether the upstream is healthy.
|
|
|
|
func (uh *upstreamHost) Unhealthy() bool {
|
|
|
|
return atomic.LoadInt32(&uh.unhealthy) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountRequest mutates the active request count by
|
|
|
|
// delta. It returns an error if the adjustment fails.
|
|
|
|
func (uh *upstreamHost) CountRequest(delta int) error {
|
|
|
|
result := atomic.AddInt64(&uh.numRequests, int64(delta))
|
|
|
|
if result < 0 {
|
|
|
|
return fmt.Errorf("count below 0: %d", result)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountFail mutates the recent failures count by
|
|
|
|
// delta. It returns an error if the adjustment fails.
|
|
|
|
func (uh *upstreamHost) CountFail(delta int) error {
|
|
|
|
result := atomic.AddInt64(&uh.fails, int64(delta))
|
|
|
|
if result < 0 {
|
|
|
|
return fmt.Errorf("count below 0: %d", result)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHealthy sets the upstream has healthy or unhealthy
|
2019-10-12 04:25:39 +08:00
|
|
|
// and returns true if the new value is different.
|
2019-09-04 06:56:09 +08:00
|
|
|
func (uh *upstreamHost) SetHealthy(healthy bool) (bool, error) {
|
|
|
|
var unhealthy, compare int32 = 1, 0
|
|
|
|
if healthy {
|
|
|
|
unhealthy, compare = 0, 1
|
|
|
|
}
|
|
|
|
swapped := atomic.CompareAndSwapInt32(&uh.unhealthy, compare, unhealthy)
|
|
|
|
return swapped, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:14:39 +08:00
|
|
|
// DialInfo contains information needed to dial a
|
|
|
|
// connection to an upstream host. This information
|
|
|
|
// may be different than that which is represented
|
|
|
|
// in a URL (for example, unix sockets don't have
|
|
|
|
// a host that can be represented in a URL, but
|
|
|
|
// they certainly have a network name and address).
|
|
|
|
type DialInfo struct {
|
2019-10-12 04:25:39 +08:00
|
|
|
// Upstream is the Upstream associated with
|
|
|
|
// this DialInfo. It may be nil.
|
|
|
|
Upstream *Upstream
|
|
|
|
|
|
|
|
// The network to use. This should be one of
|
|
|
|
// the values that is accepted by net.Dial:
|
2019-09-06 03:14:39 +08:00
|
|
|
// https://golang.org/pkg/net/#Dial
|
|
|
|
Network string
|
|
|
|
|
|
|
|
// The address to dial. Follows the same
|
|
|
|
// semantics and rules as net.Dial.
|
|
|
|
Address string
|
2019-09-15 03:25:26 +08:00
|
|
|
|
2019-10-12 04:25:39 +08:00
|
|
|
// Host and Port are components of Address.
|
2019-09-15 03:25:26 +08:00
|
|
|
Host, Port string
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:14:39 +08:00
|
|
|
// String returns the Caddy network address form
|
|
|
|
// by joining the network and address with a
|
|
|
|
// forward slash.
|
|
|
|
func (di DialInfo) String() string {
|
2019-10-12 04:25:39 +08:00
|
|
|
return caddy.JoinNetworkAddress(di.Network, di.Host, di.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillDialInfo returns a filled DialInfo for the given upstream, using
|
|
|
|
// the given Replacer. Note that the returned value is not a pointer.
|
2019-12-30 04:12:52 +08:00
|
|
|
func fillDialInfo(upstream *Upstream, repl *caddy.Replacer) (DialInfo, error) {
|
2019-10-12 04:25:39 +08:00
|
|
|
dial := repl.ReplaceAll(upstream.Dial, "")
|
2019-11-12 06:33:38 +08:00
|
|
|
addr, err := caddy.ParseNetworkAddress(dial)
|
2019-10-12 04:25:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return DialInfo{}, fmt.Errorf("upstream %s: invalid dial address %s: %v", upstream.Dial, dial, err)
|
|
|
|
}
|
2019-11-12 06:33:38 +08:00
|
|
|
if numPorts := addr.PortRangeSize(); numPorts != 1 {
|
2019-10-12 04:25:39 +08:00
|
|
|
return DialInfo{}, fmt.Errorf("upstream %s: dial address must represent precisely one socket: %s represents %d",
|
2019-11-12 06:33:38 +08:00
|
|
|
upstream.Dial, dial, numPorts)
|
2019-10-12 04:25:39 +08:00
|
|
|
}
|
|
|
|
return DialInfo{
|
|
|
|
Upstream: upstream,
|
2019-11-12 06:33:38 +08:00
|
|
|
Network: addr.Network,
|
|
|
|
Address: addr.JoinHostPort(0),
|
|
|
|
Host: addr.Host,
|
|
|
|
Port: strconv.Itoa(int(addr.StartPort)),
|
2019-10-12 04:25:39 +08:00
|
|
|
}, nil
|
2019-09-06 03:14:39 +08:00
|
|
|
}
|
|
|
|
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
// GetDialInfo gets the upstream dialing info out of the context,
|
|
|
|
// and returns true if there was a valid value; false otherwise.
|
|
|
|
func GetDialInfo(ctx context.Context) (DialInfo, bool) {
|
|
|
|
dialInfo, ok := caddyhttp.GetVar(ctx, dialInfoVarKey).(DialInfo)
|
|
|
|
return dialInfo, ok
|
|
|
|
}
|
2019-09-06 03:14:39 +08:00
|
|
|
|
2019-09-04 06:56:09 +08:00
|
|
|
// hosts is the global repository for hosts that are
|
|
|
|
// currently in use by active configuration(s). This
|
|
|
|
// allows the state of remote hosts to be preserved
|
|
|
|
// through config reloads.
|
|
|
|
var hosts = caddy.NewUsagePool()
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
|
|
|
|
// dialInfoVarKey is the key used for the variable that holds
|
|
|
|
// the dial info for the upstream connection.
|
|
|
|
const dialInfoVarKey = "reverse_proxy.dial_info"
|