net

package
v0.11.20 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: Apache-2.0, MIT Imports: 8 Imported by: 7

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

This section is empty.

Variables

This section is empty.

Functions

func RemoteHost

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

RemoteHost 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 RemoteHostFromLast added in v0.9.164

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

RemoteHostFromLast 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

Types

type Options added in v0.11.19

type Options struct {
	// 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
}

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 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")

u := "http://127.0.0.1:12345/foo"
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) 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.

Jump to

Keyboard shortcuts

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