client

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package client is a Piko client to listen on upstream endpoints and connect to upstream endpoints.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed = errors.New("closed")
)

Functions

This section is empty.

Types

type Dialer

type Dialer struct {
	// URL is the URL of the Piko server to connect to.
	//
	// This must be the URL of the 'proxy' port. Defaults to
	// 'http://localhost:8000'.
	URL *url.URL

	// Token configures the API key token to authenticate the listener with the
	// Piko server.
	//
	// Defaults to no authentication.
	Token string

	// TLSConfig specifies the TLS configuration to use with the Piko server.
	//
	// If nil, the default configuration is used.
	TLSConfig *tls.Config
}

Dialer manages opening connections to upstream endpoints.

Note when using HTTP, you can open a TCP direction directly to Piko without using a Piko client as the endpoint to connect to is specified in the HTTP request. You only need to use Dialer when using 'raw' TCP so the client can specify which endpoint to connect to.

Example

ExampleDialer opens a connection to endpoint 'my-endpoint' using Piko.

package main

import (
	"context"

	piko "github.com/andydunstall/piko/client"
)

func main() {
	dialer := &piko.Dialer{
		// ...
	}
	conn, err := dialer.Dial(context.Background(), "my-endpoint")
	if err != nil {
		panic("dial: " + err.Error())
	}

	if _, err := conn.Write([]byte("hello")); err != nil {
		panic("write: " + err.Error())
	}
}
Output:

func (*Dialer) Dial

func (d *Dialer) Dial(ctx context.Context, endpointID string) (net.Conn, error)

Dial opens a TCP connection to the endpoint with the given ID and returns a net.Conn.

type Forwarder

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

Forwarder manages forwarding incoming connections to another address.

func (*Forwarder) Close

func (f *Forwarder) Close() error

Close closes the listener and all active connections.

func (*Forwarder) Wait

func (f *Forwarder) Wait() error

Wait blocks until the forwarder exits, either due to an error or being closed.

type Listener

type Listener interface {
	net.Listener

	// EndpointID returns the ID of the endpoint this is listening for
	// connections on.
	EndpointID() string
}

Listener is a net.Listener that accepts incoming connections for Piko endpoints.

type Logger

type Logger interface {
	Debug(msg string, fields ...zap.Field)
	Info(msg string, fields ...zap.Field)
	Warn(msg string, fields ...zap.Field)
	Error(msg string, fields ...zap.Field)
	Sync() error
}

Logger is a logger compatible with zap.Logger.

type Upstream

type Upstream struct {
	// URL is the URL of the Piko server to connect to.
	//
	// This must be the URL of the 'upstream' port.
	// Defaults to 'http://localhost:8001'.
	URL *url.URL

	// Token configures the API key token to authenticate the listener with the
	// Piko server.
	//
	// Defaults to no authentication.
	Token string

	// TLSConfig specifies the TLS configuration to use with the Piko server.
	//
	// If nil, the default configuration is used.
	TLSConfig *tls.Config

	// MinReconnectBackoff is the minimum backoff when reconnecting.
	//
	// Defaults to 100ms.
	MinReconnectBackoff time.Duration

	// MaxReconnectBackoff is the maximum backoff when reconnecting.
	//
	// Defaults to 15s.
	MaxReconnectBackoff time.Duration

	// Logger is an optional logger to log connection state changes.
	Logger Logger
}

Upstream manages listening on upstream endpoints.

The client establishes an outbound-only connection to the Piko server for each listener. Connections to the listener are then multiplexed via this outbound-connection. This means the client can treat incoming connections as normal connections using net.Listener, without exposing a port.

Listens recover from any transient networking issues by reconnecting to the Piko server.

func (*Upstream) Listen

func (u *Upstream) Listen(ctx context.Context, endpointID string) (Listener, error)

Listen listens for connections on the given endpoint.

Example

ExampleUpstream listens on endpoint 'my-endpoint' and uses the listener in a HTTP server.

package main

import (
	"context"
	"fmt"
	"net/http"

	piko "github.com/andydunstall/piko/client"
)

func main() {
	upstream := &piko.Upstream{
		// ...
	}

	ln, err := upstream.Listen(context.Background(), "my-endpoint")
	if err != nil {
		panic("listen: " + err.Error())
	}

	handler := func(w http.ResponseWriter, _ *http.Request) {
		// nolint
		fmt.Fprintln(w, "Hello from Piko!")
	}
	// As ln in a standard net.Listener, it can be used in a HTTP server.
	if err := http.Serve(ln, http.HandlerFunc(handler)); err != nil {
		panic("serve: " + err.Error())
	}
}
Output:

func (*Upstream) ListenAndForward

func (u *Upstream) ListenAndForward(
	ctx context.Context, endpointID string, addr string,
) (*Forwarder, error)

ListenAndForward listens for connections on the given endpoint and forwards them to the upstream address.

This synchronously connects to the Piko server and listens on the endpoint, then starts a background 'forwarder'. The forwarder is returned and will run in the background until either the context is canclled or the returned forwarder is closed.

Example

ExampleUpstream listens on endpoint 'my-endpoint' and forwards connections to 'localhost:8000'.

package main

import (
	"context"

	piko "github.com/andydunstall/piko/client"
)

func main() {
	upstream := &piko.Upstream{
		// ...
	}

	forwarder, err := upstream.ListenAndForward(
		context.Background(), "my-endpoint", "localhost:6000",
	)
	if err != nil {
		panic("listen: " + err.Error())
	}
	defer forwarder.Close()

	if err := forwarder.Wait(); err != nil {
		panic("forwarder: " + err.Error())
	}
}
Output:

Jump to

Keyboard shortcuts

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