{"modulePath":"github.com/codedance/caddy","version":"v0.11.2","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package proxy\n\nPackage proxy is middleware that proxies HTTP requests.\n\n## Constants\n\n```go\nconst CustomStatusContextCancelled = 499\n```\n\n## Functions\n\n```go\nfunc RegisterPolicy(name string, policy func(string) Policy)\n```\nRegisterPolicy adds a custom policy to the proxy.\n\n## Types\n\n```go\ntype First struct{}\n```\nFirst is a policy that selects the first available host\n\n```go\nfunc (r *First) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects the first available host from the pool\n\n```go\ntype Header struct {\n\t// The name of the request header, the value of which will determine\n\t// how the request is routed\n\tName string\n}\n```\nHeader is a policy that selects based on a hash of the given header\n\n```go\nfunc (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects the host based on hashing the header value\n\n```go\ntype HostPool []*UpstreamHost\n```\nHostPool is a collection of UpstreamHosts.\n\n```go\ntype IPHash struct{}\n```\nIPHash is a policy that selects hosts based on hashing the request IP\n\n```go\nfunc (r *IPHash) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects an up host from the pool based on hashing the request IP\n\n```go\ntype LeastConn struct{}\n```\nLeastConn is a policy that selects the host with the least connections.\n\n```go\nfunc (r *LeastConn) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects the up host with the least number of connections in the pool. If more than one host has the same least number of connections, one of the hosts is chosen at random.\n\n```go\ntype Policy interface {\n\tSelect(pool HostPool, r *http.Request) *UpstreamHost\n}\n```\nPolicy decides how a host will be selected from a pool.\n\n```go\ntype Proxy struct {\n\tNext      httpserver.Handler\n\tUpstreams []Upstream\n}\n```\nProxy represents a middleware instance that can proxy requests.\n\n```go\nfunc (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)\n```\nServeHTTP satisfies the httpserver.Handler interface.\n\n```go\ntype Random struct{}\n```\nRandom is a policy that selects up hosts from a pool at random.\n\n```go\nfunc (r *Random) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects an up host at random from the specified pool.\n\n```go\ntype ReverseProxy struct {\n\t// Director must be a function which modifies\n\t// the request into a new request to be sent\n\t// using Transport. Its response is then copied\n\t// back to the original client unmodified.\n\tDirector func(*http.Request)\n\n\t// The transport used to perform proxy requests.\n\tTransport http.RoundTripper\n\n\t// FlushInterval specifies the flush interval\n\t// to flush to the client while copying the\n\t// response body.\n\t// If zero, no periodic flushing is done.\n\tFlushInterval time.Duration\n\t// contains filtered or unexported fields\n}\n```\nReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client.\n\n```go\nfunc NewSingleHostReverseProxy(target *url.URL, without string, keepalive int, timeout, fallbackDelay time.Duration) *ReverseProxy\n```\nNewSingleHostReverseProxy returns a new ReverseProxy that rewrites URLs to the scheme, host, and base path provided in target. If the target's path is \"/base\" and the incoming request was for \"/dir\", the target request will be for /base/dir. Without logic: target's path is \"/\", incoming is \"/api/messages\", without is \"/api\", then the target request will be for /messages.\n\n```go\nfunc (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, respUpdateFn respUpdateFn) error\n```\nServeHTTP serves the proxied request to the upstream by performing a roundtrip. It is designed to handle websocket connection upgrades as well.\n\n```go\nfunc (rp *ReverseProxy) UseInsecureTransport()\n```\nUseInsecureTransport is used to facilitate HTTPS proxying when it is OK for upstream to be using a bad certificate, since this transport skips verification.\n\n```go\ntype RoundRobin struct {\n\t// contains filtered or unexported fields\n}\n```\nRoundRobin is a policy that selects hosts based on round-robin ordering.\n\n```go\nfunc (r *RoundRobin) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects an up host from the pool using a round-robin ordering scheme.\n\n```go\ntype URIHash struct{}\n```\nURIHash is a policy that selects the host based on hashing the request URI\n\n```go\nfunc (r *URIHash) Select(pool HostPool, request *http.Request) *UpstreamHost\n```\nSelect selects the host based on hashing the URI\n\n```go\ntype Upstream interface {\n\t// The path this upstream host should be routed on\n\tFrom() string\n\n\t// Selects an upstream host to be routed to. It\n\t// should return a suitable upstream host, or nil\n\t// if no such hosts are available.\n\tSelect(*http.Request) *UpstreamHost\n\n\t// Checks if subpath is not an ignored path\n\tAllowedPath(string) bool\n\n\t// Gets the duration of the headstart the first\n\t// connection is given in the Go standard library's\n\t// implementation of \"Happy Eyeballs\" when DualStack\n\t// is enabled in net.Dialer.\n\tGetFallbackDelay() time.Duration\n\n\t// Gets how long to try selecting upstream hosts\n\t// in the case of cascading failures.\n\tGetTryDuration() time.Duration\n\n\t// Gets how long to wait between selecting upstream\n\t// hosts in the case of cascading failures.\n\tGetTryInterval() time.Duration\n\n\t// Gets the number of upstream hosts.\n\tGetHostCount() int\n\n\t// Gets how long to wait before timing out\n\t// the request\n\tGetTimeout() time.Duration\n\n\t// Stops the upstream from proxying requests to shutdown goroutines cleanly.\n\tStop() error\n}\n```\nUpstream manages a pool of proxy upstream hosts.\n\n```go\nfunc NewStaticUpstreams(c caddyfile.Dispenser, host string) ([]Upstream, error)\n```\nNewStaticUpstreams parses the configuration input and sets up static upstreams for the proxy middleware. The host string parameter, if not empty, is used for setting the upstream Host header for the health checks if the upstream header config requires it.\n\n```go\ntype UpstreamHost struct {\n\t// This field is read \u0026 written to concurrently, so all access must use\n\t// atomic operations.\n\tConns             int64 // must be first field to be 64-bit aligned on 32-bit systems\n\tMaxConns          int64\n\tName              string // hostname of this upstream host\n\tUpstreamHeaders   http.Header\n\tDownstreamHeaders http.Header\n\tFailTimeout       time.Duration\n\tCheckDown         UpstreamHostDownFunc\n\tWithoutPathPrefix string\n\tReverseProxy      *ReverseProxy\n\tFails             int32\n\t// This is an int32 so that we can use atomic operations to do concurrent\n\t// reads \u0026 writes to this value.  The default value of 0 indicates that it\n\t// is healthy and any non-zero value indicates unhealthy.\n\tUnhealthy         int32\n\tHealthCheckResult atomic.Value\n}\n```\nUpstreamHost represents a single proxy upstream\n\n```go\nfunc (uh *UpstreamHost) Available() bool\n```\nAvailable checks whether the upstream host is available for proxying to\n\n```go\nfunc (uh *UpstreamHost) Down() bool\n```\nDown checks whether the upstream host is down or not. Down will try to use uh.CheckDown first, and will fall back to some default criteria if necessary.\n\n```go\nfunc (uh *UpstreamHost) Full() bool\n```\nFull checks whether the upstream host has reached its maximum connections\n\n```go\ntype UpstreamHostDownFunc func(*UpstreamHost) bool\n```\nUpstreamHostDownFunc can be used to customize how Down behaves.\n\n","path":"github.com/codedance/caddy/caddyhttp/proxy","name":"proxy","synopsis":"Package proxy is middleware that proxies HTTP requests.","isRedistributable":true}
