pkgsite: golang.org/x/pkgsite/internal/teeproxy Index | Files

package teeproxy

import "golang.org/x/pkgsite/internal/teeproxy"

Package teeproxy provides functionality for running a service which tees traffic to pkg.go.dev.

Index

Package Files

teeproxy.go

Variables

var (

    // TeeproxyGddoRequestLatencyDistribution aggregates the latency of
    // teeproxy requests from godoc.org by status code and host.
    TeeproxyGddoRequestLatencyDistribution = &view.View{
        Name:        "go-discovery/teeproxy/gddo-latency",
        Measure:     teeproxyGddoLatency,
        Aggregation: ochttp.DefaultLatencyDistribution,
        Description: "Teeproxy latency from godoc.org, by response status code",
        TagKeys:     []tag.Key{keyTeeproxyStatus, keyTeeproxyHost},
    }
    // TeeproxyPkgGoDevRequestLatencyDistribution aggregates the latency of
    // teeproxy requests to pkg.go.dev by status code and host.
    TeeproxyPkgGoDevRequestLatencyDistribution = &view.View{
        Name:        "go-discovery/teeproxy/pkgGoDev-latency",
        Measure:     teeproxyPkgGoDevLatency,
        Aggregation: ochttp.DefaultLatencyDistribution,
        Description: "Teeproxy latency to pkg.go.dev, by response status code",
        TagKeys:     []tag.Key{keyTeeproxyStatus, keyTeeproxyHost},
    }
    // TeeproxyGddoRequestCount counts teeproxy requests from godoc.org.
    TeeproxyGddoRequestCount = &view.View{
        Name:        "go-discovery/teeproxy/gddo-count",
        Measure:     teeproxyGddoLatency,
        Aggregation: view.Count(),
        Description: "Count of teeproxy requests from godoc.org",
        TagKeys:     []tag.Key{keyTeeproxyStatus, keyTeeproxyHost},
    }
    // TeeproxyPkgGoDevRequestCount counts teeproxy requests to pkg.go.dev.
    TeeproxyPkgGoDevRequestCount = &view.View{
        Name:        "go-discovery/teeproxy/pkgGoDev-count",
        Measure:     teeproxyPkgGoDevLatency,
        Aggregation: view.Count(),
        Description: "Count of teeproxy requests to pkg.go.dev",
        TagKeys:     []tag.Key{keyTeeproxyStatus, keyTeeproxyHost},
    }
    // TeeproxyPkgGoDevBrokenPathCount counts teeproxy requests to pkg.go.dev
    // that return 4xx or 5xx but return 2xx or 3xx on godoc.org.
    TeeproxyPkgGoDevBrokenPathCount = &view.View{
        Name:        "go-discovery/teeproxy/pkgGoDev-brokenPath",
        Measure:     teeproxyPkgGoDevBrokenPaths,
        Aggregation: view.Count(),
        Description: "Count of broken paths in pkg.go.dev",
        TagKeys:     []tag.Key{keyTeeproxyStatus, keyTeeproxyHost, keyTeeproxyPath},
    }
)

type Config Uses

type Config struct {
    // AuthKey is the name of the header that is used by pkg.go.dev to
    // determine if a request is coming from a trusted source.
    AuthKey string
    // AuthValue is the value of the header that is used by pkg.go.dev to
    // determine that the request is coming from the teeproxy.
    AuthValue string
    // Hosts is the list of hosts that the teeproxy forwards requests to.
    Hosts []string
    // Client is the HTTP client used by the teeproxy to forward requests
    // to the hosts.
    Client *http.Client
    // Rate is the rate at which requests are rate limited.
    Rate float64
    // Burst is the maximum burst of requests permitted.
    Burst         int
    BreakerConfig breaker.Config
}

Config contains configuration values for Server.

type RequestEvent Uses

type RequestEvent struct {
    Host    string
    Path    string
    URL     string
    Header  http.Header
    Latency time.Duration
    Status  int
    Error   error
    // IsRobot reports whether this request came from a robot.
    // https://github.com/golang/gddo/blob/a4ebd2f/gddo-server/main.go#L152
    IsRobot bool
}

RequestEvent stores information about a godoc.org or pkg.go.dev request.

type Server Uses

type Server struct {
    // contains filtered or unexported fields
}

Server receives requests from godoc.org and tees them to specified hosts.

func NewServer Uses

func NewServer(config Config) (_ *Server, err error)

NewServer returns a new Server struct with preconfigured settings.

The server is rate limited and allows events up to a rate of "Rate" and a burst of "Burst".

The server also implements the circuit breaker pattern and maintains a breaker for each host. Each breaker can be in one of three states: green, yellow, or red.

In the green state, the breaker remains green until it encounters a time window of length "GreenInterval" where there are more than of "FailsToRed" failures and a failureRatio of more than "FailureThreshold", in which case the state becomes red.

In the red state, the breaker halts all requests and waits for a timeout period before shifting to the yellow state.

In the yellow state, the breaker allows the first "SuccsToGreen" requests. If any of these fail, the state reverts to red. Otherwise, the state becomes green again.

The timeout period is initially set to "MinTimeout" when the breaker shifts from green to yellow. By default, the timeout period is doubled each time the breaker fails to shift from the yellow state to the green state and is capped at "MaxTimeout".

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP receives requests from godoc.org and forwards them to the specified hosts. These requests are validated and rate limited before being forwarded. Too many error responses returned by pkg.go.dev will cause the server to back off temporarily before trying to forward requests to the hosts again. ServeHTTP will always reply with StatusOK as long as the request is a valid godoc.org request, even if the request could not be processed by the hosts. Instead, problems with processing the request by the hosts will logged.

Package teeproxy imports 18 packages (graph) and is imported by 1 packages. Updated 2020-10-28. Refresh now. Tools for package owners.