transport

package
v0.0.0-...-5b5a8c3 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewClientStream = func(host, endpoint string) (ClientStreamTransport, error) {

	u := url.URL{Scheme: "ws", Host: host, Path: endpoint}
	h := http.Header{}
	h.Set("Sec-WebSocket-Protocol", "grpc-websockets")
	var conn *websocket.Conn
	conn, _, err := websocket.DefaultDialer.Dial(u.String(), h)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to dial to '%s'", u.String())
	}

	return &webSocketTransport{
		host:     host,
		endpoint: endpoint,
		conn:     conn,
	}, nil
}
View Source
var NewUnary = func(host string, opts *ConnectOptions) UnaryTransport {
	cl := http.DefaultClient
	transport := &http.Transport{}
	if opts.WithTLS {
		transport.TLSClientConfig = &tls.Config{}
		if opts.TLSCertificate != nil {
			certPool := x509.NewCertPool()
			decoded, _ := pem.Decode(opts.TLSCertificate)
			if decoded == nil {
				panic("failed to decode cert")
			}
			cert, err := x509.ParseCertificate(decoded.Bytes)
			if err != nil {
				panic(err)
			}
			certPool.AddCert(cert)
			transport.TLSClientConfig.RootCAs = certPool
			transport.TLSClientConfig.ServerName = cert.DNSNames[0]
		}
		transport.TLSClientConfig.InsecureSkipVerify = opts.TlsInsecureSkipVerify
	}
	if opts.Timeout != 0 {
		cl.Timeout = time.Second
	} else {
		cl.Timeout = opts.Timeout
	}

	if opts.IdleConnTimeout != 0 {
		transport.IdleConnTimeout = opts.IdleConnTimeout
	}

	if opts.TlsHandshakeTimeout != 0 {
		transport.TLSHandshakeTimeout = opts.TlsHandshakeTimeout
	}

	if opts.ExpectContinueTimeout != 0 {
		transport.ExpectContinueTimeout = opts.ExpectContinueTimeout
	}

	cl.Transport = transport
	return &httpTransport{
		host:       host,
		client:     cl,
		opts:       opts,
		header:     make(http.Header),
		clientLock: &sync.RWMutex{},
	}
}

NewUnary returns an httpTransport object wrapped as a UnaryTransport object

Functions

This section is empty.

Types

type ClientStreamTransport

type ClientStreamTransport interface {
	Header() (http.Header, error)
	Trailer() http.Header

	// SetRequestHeader sets headers to send gRPC-Web server.
	// It should be called before calling Send.
	SetRequestHeader(h http.Header)
	Send(ctx context.Context, body io.Reader) error
	Receive(ctx context.Context) (io.ReadCloser, error)

	// CloseSend sends a close signal to the server.
	CloseSend() error

	// Close closes the connection.
	Close() error
}

type ConnectOptions

type ConnectOptions struct {
	// Toggle tls on/off
	WithTLS bool
	// Certificate for TLS connections
	TLSCertificate []byte
	// IdleConnTimeout is the maximum amount of time an idle
	// (keep-alive) connection will remain idle before closing
	// itself.
	// Zero means no limit.
	IdleConnTimeout time.Duration
	// TLSHandshakeTimeout specifies the maximum amount of time waiting to
	// wait for a TLS handshake. Zero means no timeout.
	TlsHandshakeTimeout time.Duration
	// ExpectContinueTimeout, if non-zero, specifies the amount of
	// time to wait for a server's first response headers after fully
	// writing the request headers
	ExpectContinueTimeout time.Duration
	// Skip standard tls certificate verifications
	TlsInsecureSkipVerify bool

	Timeout time.Duration
}

ConnectOptions struct contains configuration parameters for DialContext

type UnaryTransport

type UnaryTransport interface {
	Header() http.Header
	Send(ctx context.Context, endpoint, contentType string, body io.Reader) (http.Header, []byte, error)
	GetRemoteCertificate() (*x509.Certificate, error)
	Close() error
}

UnaryTransport is the public interface for the transport package

Jump to

Keyboard shortcuts

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