net

package
v0.21.236 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: Apache-2.0, MIT Imports: 28 Imported by: 8

Documentation

Overview

Package net provides generic network related functions used across Skipper, which might be useful also in other contexts than Skipper.

Index

Examples

Constants

View Source
const (
	// DefaultReadTimeout is the default socket read timeout
	DefaultReadTimeout = 25 * time.Millisecond
	// DefaultWriteTimeout is the default socket write timeout
	DefaultWriteTimeout = 25 * time.Millisecond
	// DefaultPoolTimeout is the default timeout to access the connection pool
	DefaultPoolTimeout = 25 * time.Millisecond
	// DefaultDialTimeout is the default dial timeout
	DefaultDialTimeout = 25 * time.Millisecond
	// DefaultMinConns is the default minimum of connections
	DefaultMinConns = 100
	// DefaultMaxConns is the default maximum of connections
	DefaultMaxConns = 100
)

Variables

This section is empty.

Functions

func NewJumpHash added in v0.13.76

func NewJumpHash(shards []string) redis.ConsistentHash

func NewMultiprobe added in v0.13.76

func NewMultiprobe(shards []string) redis.ConsistentHash

func NewRendezvous added in v0.13.76

func NewRendezvous(shards []string) redis.ConsistentHash

func NewRendezvousVnodes added in v0.13.76

func NewRendezvousVnodes(shards []string) redis.ConsistentHash

func ParseIPCIDRs added in v0.15.14

func ParseIPCIDRs(cidrs []string) (*netipx.IPSet, error)

ParseIPCIDRs returns a valid IPSet in case there is no parsing error.

func RemoteAddr added in v0.15.14

func RemoteAddr(r *http.Request) netip.Addr

RemoteAddr returns the remote address of the client. When the 'X-Forwarded-For' header is set, then it is used instead. This is how most often proxies behave. Wikipedia shows the format https://en.wikipedia.org/wiki/X-Forwarded-For#Format

Example:

X-Forwarded-For: client, proxy1, proxy2

func RemoteAddrFromLast added in v0.15.14

func RemoteAddrFromLast(r *http.Request) netip.Addr

RemoteAddrFromLast returns the remote address of the client. When the 'X-Forwarded-For' header is set, then it is used instead. This is known to be true for AWS Application LoadBalancer. AWS docs https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html

Example:

X-Forwarded-For: ip-address-1, ip-address-2, client-ip-address

func RemoteHost

func RemoteHost(r *http.Request) net.IP

RemoteHost is *deprecated* use RemoteAddr

func RemoteHostFromLast added in v0.9.164

func RemoteHostFromLast(r *http.Request) net.IP

RemoteHostFromLast is *deprecated* use RemoteAddrFromLast instead

func SchemeHost added in v0.18.97

func SchemeHost(input string) (string, string, error)

SchemeHost parses URI string (without #fragment part) and returns schema used in this URI as first return value and host[:port] part as second return value. Port is never omitted for HTTP(S): if no port is specified in URI, default port for given schema is used. If URI is invalid, error is returned.

Types

type Client added in v0.11.42

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

Client adds additional features like Bearer token injection, and opentracing to the wrapped http.Client with the same interface as http.Client from the stdlib.

Example
tracer := lightstep.NewTracer(lightstep.Options{})

cli := net.NewClient(net.Options{
	Tracer:                     tracer,
	OpentracingComponentTag:    "testclient",
	OpentracingSpanName:        "clientSpan",
	BearerTokenRefreshInterval: 10 * time.Second,
	BearerTokenFile:            "/tmp/foo.token",
	IdleConnTimeout:            2 * time.Second,
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

Example (CustomTracer)
mockTracer := tracingtest.NewTracer()
cli := net.NewClient(net.Options{
	Tracer:              &customTracer{mockTracer},
	OpentracingSpanName: "clientSpan",
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
defer srv.Close()

cli.Get("http://" + srv.Listener.Addr().String() + "/")
fmt.Printf("customtag: %s", mockTracer.FinishedSpans()[0].Tags()["customtag"])
Output:

customtag: test
Example (FileSecretsReader)
tracer := lightstep.NewTracer(lightstep.Options{})

sp := secrets.NewSecretPaths(10 * time.Second)
if err := sp.Add("/tmp/bar.token"); err != nil {
	log.Fatalf("failed to read secret: %v", err)
}

cli := net.NewClient(net.Options{
	Tracer:                  tracer,
	OpentracingComponentTag: "testclient",
	OpentracingSpanName:     "clientSpan",
	SecretsReader:           sp,
	IdleConnTimeout:         2 * time.Second,
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

Example (HostSecret)
tracer := lightstep.NewTracer(lightstep.Options{})
sec := []byte("mysecret")

cli := net.NewClient(net.Options{
	Tracer:                  tracer,
	OpentracingComponentTag: "testclient",
	OpentracingSpanName:     "clientSpan",
	SecretsReader: secrets.NewHostSecret(
		newTestSecretsReader(
			map[string][]byte{
				"key": sec,
			},
		),
		map[string]string{
			"127.0.0.1": "key",
		},
	),
	IdleConnTimeout: 2 * time.Second,
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

Example (StaticDelegateSecret)
tracer := lightstep.NewTracer(lightstep.Options{})
sec := []byte("mysecret")

cli := net.NewClient(net.Options{
	Tracer:                  tracer,
	OpentracingComponentTag: "testclient",
	OpentracingSpanName:     "clientSpan",
	SecretsReader: secrets.NewStaticDelegateSecret(
		newTestSecretsReader(
			map[string][]byte{
				"key": sec,
			},
		),
		"key",
	),
	IdleConnTimeout: 2 * time.Second,
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

Example (StaticSecret)
tracer := lightstep.NewTracer(lightstep.Options{})
sec := []byte("mysecret")
cli := net.NewClient(net.Options{
	Tracer:                  tracer,
	OpentracingComponentTag: "testclient",
	OpentracingSpanName:     "clientSpan",
	SecretsReader:           secrets.StaticSecret(sec),
	IdleConnTimeout:         2 * time.Second,
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

Example (WithAfterResponseHook)
mockTracer := tracingtest.NewTracer()
cli := net.NewClient(net.Options{
	Tracer:                     &customTracer{mockTracer},
	OpentracingComponentTag:    "testclient",
	OpentracingSpanName:        "clientSpan",
	BearerTokenRefreshInterval: 10 * time.Second,
	BearerTokenFile:            "/tmp/foo.token",
	IdleConnTimeout:            2 * time.Second,
	AfterResponse: func(rsp *http.Response, err error) {
		if span := opentracing.SpanFromContext(rsp.Request.Context()); span != nil {
			span.SetTag("status.code", rsp.StatusCode)
			if err != nil {
				span.SetTag("error", err.Error())
			}
		}
		rsp.StatusCode = 255
	},
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

rsp, err := cli.Get("http://" + srv.Listener.Addr().String() + "/")
if err != nil {
	log.Fatalf("Failed to get: %v", err)
}

fmt.Printf("response code: %d\n", rsp.StatusCode)
fmt.Printf("span status.code: %d", mockTracer.FinishedSpans()[0].Tags()["status.code"])
Output:

response code: 255
span status.code: 200
Example (WithBeforeSendHook)
mockTracer := tracingtest.NewTracer()
peerService := "my-peer-service"
cli := net.NewClient(net.Options{
	Tracer:                  &customTracer{mockTracer},
	OpentracingComponentTag: "testclient",
	OpentracingSpanName:     "clientSpan",
	IdleConnTimeout:         2 * time.Second,
	BeforeSend: func(req *http.Request) {
		req.Header.Set("X-Foo", "qux")
		if span := opentracing.SpanFromContext(req.Context()); span != nil {
			logrus.Println("BeforeSend: found span")
			span.SetTag(string(ext.PeerService), peerService)
		} else {
			logrus.Println("BeforeSend: no span found")
		}
	},
})
defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("X-Foo: %s\n", r.Header.Get("X-Foo"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

cli.Get("http://" + srv.Listener.Addr().String() + "/")
fmt.Printf("request tag %q set to %q", string(ext.PeerService), mockTracer.FinishedSpans()[0].Tags()[string(ext.PeerService)])
Output:

X-Foo: qux
request tag "peer.service" set to "my-peer-service"
Example (WithTransport)
tracer := lightstep.NewTracer(lightstep.Options{})

d := stdlibnet.Dialer{
	Timeout:   3 * time.Second,
	KeepAlive: 30 * time.Second,
	DualStack: true,
}
f := d.DialContext

cli := net.NewClient(net.Options{
	Transport: &http.Transport{
		IdleConnTimeout: 10 * time.Second,
		DialContext:     f,
	},
	Tracer:                     tracer,
	OpentracingComponentTag:    "testclient",
	OpentracingSpanName:        "clientSpan",
	BearerTokenRefreshInterval: 10 * time.Second,
	BearerTokenFile:            "/tmp/foo.token",
})

defer cli.Close()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"

for i := 0; i < 15; i++ {
	rsp, err := cli.Get(u)
	if err != nil {
		log.Fatalf("Failed to do request: %v", err)
	}
	log.Printf("rsp code: %v", rsp.StatusCode)
	time.Sleep(1 * time.Second)
}
Output:

func NewClient added in v0.11.42

func NewClient(o Options) *Client

NewClient creates a wrapped http.Client and uses Transport to support OpenTracing. On teardown you have to use Close() to not leak a goroutine.

If secrets.SecretsReader is nil, but BearerTokenFile is not empty string, it creates StaticDelegateSecret with a wrapped secrets.SecretPaths, which can be used with Kubernetes secrets to read from the secret an automatically updated Bearer token.

func (*Client) Close added in v0.11.42

func (c *Client) Close()

func (*Client) CloseIdleConnections added in v0.11.42

func (c *Client) CloseIdleConnections()

CloseIdleConnections delegates the call to the underlying http.Client.

func (*Client) Do added in v0.11.42

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do delegates the given http.Request to the underlying http.Client and adds a Bearer token to the authorization header, if Client has a secrets.SecretsReader and the request does not contain an Authorization header.

func (*Client) Get added in v0.11.42

func (c *Client) Get(url string) (*http.Response, error)

func (*Client) Head added in v0.11.42

func (c *Client) Head(url string) (*http.Response, error)

func (*Client) Post added in v0.11.42

func (c *Client) Post(url, contentType string, body io.Reader) (*http.Response, error)

func (*Client) PostForm added in v0.11.42

func (c *Client) PostForm(url string, data url.Values) (*http.Response, error)

type ConnManager added in v0.21.205

type ConnManager struct {
	// Metrics is an optional metrics registry to count connection events.
	Metrics metrics.Metrics

	// Keepalive is the duration after which server connection is closed.
	Keepalive time.Duration

	// KeepaliveRequests is the number of requests after which server connection is closed.
	KeepaliveRequests int
	// contains filtered or unexported fields
}

ConnManager tracks creation of HTTP server connections and closes connections when their age or number of requests served reaches configured limits. Use ConnManager.Configure method to setup ConnManager for an http.Server.

func (*ConnManager) Configure added in v0.21.205

func (cm *ConnManager) Configure(server *http.Server)

type ForwardedHeaders added in v0.13.99

type ForwardedHeaders struct {
	// For sets or appends request remote IP to the X-Forwarded-For header
	For bool
	// PrependFor sets or prepends request remote IP to the X-Forwarded-For header, overrides For
	PrependFor bool
	// Host sets X-Forwarded-Host to the request host
	Host bool
	// Method sets the http method as X-Forwarded-Method to the request header
	Method bool
	// Uri sets the path and query as X-Forwarded-Uri header to the request header
	Uri bool
	// Sets X-Forwarded-Port value
	Port string
	// Sets X-Forwarded-Proto value
	Proto string
}

ForwardedHeaders sets non-standard X-Forwarded-* Headers See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#proxies and https://github.com/authelia/authelia

func (*ForwardedHeaders) Set added in v0.13.99

func (h *ForwardedHeaders) Set(req *http.Request)

type ForwardedHeadersHandler added in v0.13.99

type ForwardedHeadersHandler struct {
	Headers ForwardedHeaders
	Exclude IPNets
	Handler http.Handler
}

func (*ForwardedHeadersHandler) ServeHTTP added in v0.13.99

type HostPatch added in v0.13.147

type HostPatch struct {
	// Remove port if present
	RemovePort bool

	// Remove trailing dot if present
	RemoteTrailingDot bool

	// Convert to lowercase
	ToLower bool
}

HostPatch is used to modify host[:port] string

func (*HostPatch) Apply added in v0.13.147

func (h *HostPatch) Apply(original string) string

type HostPatchHandler added in v0.13.147

type HostPatchHandler struct {
	Patch   HostPatch
	Handler http.Handler
}

func (*HostPatchHandler) ServeHTTP added in v0.13.147

func (h *HostPatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type IPNets added in v0.13.99

type IPNets []*net.IPNet

IPNets is *deprecated* use netipx.IPSet instead

func ParseCIDRs added in v0.13.99

func ParseCIDRs(cidrs []string) (nets IPNets, err error)

ParseCIDRs is *deprecated* use ParseIPCIDRs.

func (IPNets) Contain added in v0.13.99

func (nets IPNets) Contain(ip net.IP) bool

Contain is *deprecated* use netipx.IPSet.Contains() instead

type Options added in v0.11.19

type Options struct {
	// Transport see https://golang.org/pkg/net/http/#Transport
	// In case Transport is not nil, the Transport arguments are used below.
	Transport *http.Transport
	// CheckRedirect see https://golang.org/pkg/net/http/#Client
	CheckRedirect func(req *http.Request, via []*http.Request) error
	// Proxy see https://golang.org/pkg/net/http/#Transport.Proxy
	Proxy func(req *http.Request) (*url.URL, error)
	// DisableKeepAlives see https://golang.org/pkg/net/http/#Transport.DisableKeepAlives
	DisableKeepAlives bool
	// DisableCompression see https://golang.org/pkg/net/http/#Transport.DisableCompression
	DisableCompression bool
	// ForceAttemptHTTP2 see https://golang.org/pkg/net/http/#Transport.ForceAttemptHTTP2
	ForceAttemptHTTP2 bool
	// MaxIdleConns see https://golang.org/pkg/net/http/#Transport.MaxIdleConns
	MaxIdleConns int
	// MaxIdleConnsPerHost see https://golang.org/pkg/net/http/#Transport.MaxIdleConnsPerHost
	MaxIdleConnsPerHost int
	// MaxConnsPerHost see https://golang.org/pkg/net/http/#Transport.MaxConnsPerHost
	MaxConnsPerHost int
	// WriteBufferSize see https://golang.org/pkg/net/http/#Transport.WriteBufferSize
	WriteBufferSize int
	// ReadBufferSize see https://golang.org/pkg/net/http/#Transport.ReadBufferSize
	ReadBufferSize int
	// MaxResponseHeaderBytes see
	// https://golang.org/pkg/net/http/#Transport.MaxResponseHeaderBytes
	MaxResponseHeaderBytes int64
	// Timeout sets all Timeouts, that are set to 0 to the given
	// value. Basically it's the default timeout value.
	Timeout time.Duration
	// TLSHandshakeTimeout see
	// https://golang.org/pkg/net/http/#Transport.TLSHandshakeTimeout,
	// if not set or set to 0, its using Options.Timeout.
	TLSHandshakeTimeout time.Duration
	// IdleConnTimeout see
	// https://golang.org/pkg/net/http/#Transport.IdleConnTimeout,
	// if not set or set to 0, its using Options.Timeout.
	IdleConnTimeout time.Duration
	// ResponseHeaderTimeout see
	// https://golang.org/pkg/net/http/#Transport.ResponseHeaderTimeout,
	// if not set or set to 0, its using Options.Timeout.
	ResponseHeaderTimeout time.Duration
	// ExpectContinueTimeout see
	// https://golang.org/pkg/net/http/#Transport.ExpectContinueTimeout,
	// if not set or set to 0, its using Options.Timeout.
	ExpectContinueTimeout time.Duration

	// Tracer instance, can be nil to not enable tracing
	Tracer opentracing.Tracer
	// OpentracingComponentTag sets component tag for all requests
	OpentracingComponentTag string
	// OpentracingSpanName sets span name for all requests
	OpentracingSpanName string

	// BearerTokenFile injects bearer token read from file, which
	// file path is the given string. In case SecretsReader is
	// provided, BearerTokenFile will be ignored.
	BearerTokenFile string
	// BearerTokenRefreshInterval refresh bearer from
	// BearerTokenFile. In case SecretsReader is provided,
	// BearerTokenFile will be ignored.
	BearerTokenRefreshInterval time.Duration
	// SecretsReader is used to read and refresh bearer tokens
	SecretsReader secrets.SecretsReader

	// Log is used for error logging
	Log logging.Logger

	// BeforeSend is a hook function that runs just before executing RoundTrip(*http.Request)
	BeforeSend func(*http.Request)
	// AfterResponse is a hook function that runs just after executing RoundTrip(*http.Request)
	AfterResponse func(*http.Response, error)
}

Options are mostly passed to the http.Transport of the same name. Options.Timeout can be used as default for all timeouts, that are not set. You can pass an opentracing.Tracer https://godoc.org/github.com/opentracing/opentracing-go#Tracer, which can be nil to get the https://godoc.org/github.com/opentracing/opentracing-go#NoopTracer.

type RedisOptions added in v0.13.38

type RedisOptions struct {
	// Addrs are the list of redis shards
	Addrs []string

	// AddrUpdater is a func that is regularly called to update
	// redis address list. This func should return a list of redis
	// shards.
	AddrUpdater func() ([]string, error)

	// UpdateInterval is the time.Duration that AddrUpdater is
	// triggered and SetAddrs be used to update the redis shards
	UpdateInterval time.Duration

	// Password is the password needed to connect to Redis server
	Password string

	// ReadTimeout for redis socket reads
	ReadTimeout time.Duration
	// WriteTimeout for redis socket writes
	WriteTimeout time.Duration
	// DialTimeout is the max time.Duration to dial a new connection
	DialTimeout time.Duration

	// PoolTimeout is the max time.Duration to get a connection from pool
	PoolTimeout time.Duration
	// IdleTimeout requires a non 0 IdleCheckFrequency
	IdleTimeout time.Duration
	// IdleCheckFrequency - reaper frequency, only used if IdleTimeout > 0
	IdleCheckFrequency time.Duration
	// MaxConnAge
	MaxConnAge time.Duration
	// MinIdleConns is the minimum number of socket connections to redis
	MinIdleConns int
	// MaxIdleConns is the maximum number of socket connections to redis
	MaxIdleConns int

	// HeartbeatFrequency frequency of PING commands sent to check
	// shards availability.
	HeartbeatFrequency time.Duration

	// ConnMetricsInterval defines the frequency of updating the redis
	// connection related metrics. Defaults to 60 seconds.
	ConnMetricsInterval time.Duration
	// MetricsPrefix is the prefix for redis ring client metrics,
	// defaults to "swarm.redis." if not set
	MetricsPrefix string
	// Tracer provides OpenTracing for Redis queries.
	Tracer opentracing.Tracer
	// Log is the logger that is used
	Log logging.Logger

	// HashAlgorithm is one of rendezvous, rendezvousVnodes, jump, mpchash, defaults to github.com/go-redis/redis default
	HashAlgorithm string
}

RedisOptions is used to configure the redis.Ring

type RedisRingClient added in v0.13.38

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

RedisRingClient is a redis client that does access redis by computing a ring hash. It logs to the logging.Logger interface, that you can pass. It adds metrics and operations are traced with opentracing. You can set timeouts and the defaults are set to be ok to be in the hot path of low latency production requests.

func NewRedisRingClient added in v0.13.38

func NewRedisRingClient(ro *RedisOptions) *RedisRingClient

func (*RedisRingClient) Close added in v0.13.38

func (r *RedisRingClient) Close()

func (*RedisRingClient) Expire added in v0.13.38

func (r *RedisRingClient) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)

func (*RedisRingClient) Get added in v0.13.38

func (r *RedisRingClient) Get(ctx context.Context, key string) (string, error)

func (*RedisRingClient) NewScript added in v0.13.208

func (r *RedisRingClient) NewScript(source string) *RedisScript

func (*RedisRingClient) RingAvailable added in v0.13.38

func (r *RedisRingClient) RingAvailable() bool

func (*RedisRingClient) RunScript added in v0.13.208

func (r *RedisRingClient) RunScript(ctx context.Context, s *RedisScript, keys []string, args ...interface{}) (interface{}, error)

func (*RedisRingClient) Set added in v0.13.38

func (r *RedisRingClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) (string, error)

func (*RedisRingClient) SetAddrs added in v0.13.232

func (r *RedisRingClient) SetAddrs(ctx context.Context, addrs []string)

func (*RedisRingClient) StartMetricsCollection added in v0.13.38

func (r *RedisRingClient) StartMetricsCollection()

func (*RedisRingClient) StartSpan added in v0.13.38

func (r *RedisRingClient) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span

func (*RedisRingClient) ZAdd added in v0.13.38

func (r *RedisRingClient) ZAdd(ctx context.Context, key string, val int64, score float64) (int64, error)

func (*RedisRingClient) ZCard added in v0.13.38

func (r *RedisRingClient) ZCard(ctx context.Context, key string) (int64, error)

func (*RedisRingClient) ZRangeByScoreWithScoresFirst added in v0.13.38

func (r *RedisRingClient) ZRangeByScoreWithScoresFirst(ctx context.Context, key string, min, max float64, offset, count int64) (interface{}, error)

func (*RedisRingClient) ZRem added in v0.13.38

func (r *RedisRingClient) ZRem(ctx context.Context, key string, members ...interface{}) (int64, error)

func (*RedisRingClient) ZRemRangeByScore added in v0.13.38

func (r *RedisRingClient) ZRemRangeByScore(ctx context.Context, key string, min, max float64) (int64, error)

type RedisScript added in v0.13.208

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

type RequestMatchHandler added in v0.13.160

type RequestMatchHandler struct {
	Match   []string
	Handler http.Handler
}

func (*RequestMatchHandler) ServeHTTP added in v0.13.160

func (h *RequestMatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Transport added in v0.11.19

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

Transport wraps an http.Transport and adds support for tracing and bearerToken injection.

Example
tracer := lightstep.NewTracer(lightstep.Options{})

cli := net.NewTransport(net.Options{
	Tracer: tracer,
})
defer cli.Close()
cli = net.WithSpanName(cli, "myspan")
cli = net.WithBearerToken(cli, "mytoken")

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	log.Printf("Authorization: %s", r.Header.Get("Authorization"))
	log.Printf("Ot-Tracer-Sampled: %s", r.Header.Get("Ot-Tracer-Sampled"))
	log.Printf("Ot-Tracer-Traceid: %s", r.Header.Get("Ot-Tracer-Traceid"))
	log.Printf("Ot-Tracer-Spanid: %s", r.Header.Get("Ot-Tracer-Spanid"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

u := "http://" + srv.Listener.Addr().String() + "/"
req, err := http.NewRequest("GET", u, nil)
if err != nil {
	log.Fatalf("Failed to create request: %v", err)
}

rsp, err := cli.RoundTrip(req)
if err != nil {
	log.Fatalf("Failed to do request: %v", err)
}
log.Printf("rsp code: %v", rsp.StatusCode)
Output:

func NewTransport added in v0.11.19

func NewTransport(options Options) *Transport

NewTransport creates a wrapped http.Transport, with regular DNS lookups using CloseIdleConnections on every IdleConnTimeout. You can optionally add tracing. On teardown you have to use Close() to not leak a goroutine.

func WithBearerToken added in v0.11.19

func WithBearerToken(t *Transport, bearerToken string) *Transport

WithBearerToken adds an Authorization header with "Bearer " prefix and add the given bearerToken as value to all requests. To regular update your token you need to call this method and use the returned Transport.

func WithComponentTag added in v0.11.19

func WithComponentTag(t *Transport, componentName string) *Transport

WithComponentTag sets the component name, if you have an enabled tracing Transport.

func WithSpanName added in v0.11.19

func WithSpanName(t *Transport, spanName string) *Transport

WithSpanName sets the name of the span, if you have an enabled tracing Transport.

func (*Transport) Close added in v0.11.19

func (t *Transport) Close()

func (*Transport) CloseIdleConnections added in v0.11.42

func (t *Transport) CloseIdleConnections()

func (*Transport) RoundTrip added in v0.11.19

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip the request with tracing, bearer token injection and add client tracing: DNS, TCP/IP, TLS handshake, connection pool access. Client traces are added as logs into the created span.

type ValidateQueryHandler added in v0.13.218

type ValidateQueryHandler struct {
	Handler http.Handler
}

func (*ValidateQueryHandler) ServeHTTP added in v0.13.218

func (q *ValidateQueryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ValidateQueryLogHandler added in v0.13.223

type ValidateQueryLogHandler struct {
	Handler http.Handler
}

func (*ValidateQueryLogHandler) ServeHTTP added in v0.13.223

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL