sentinel

package module
v0.0.0-...-2e7e658 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: Unlicense Imports: 8 Imported by: 0

README

Sentinel

Sentinel is a highly-available reverse proxy that can be used to distribute traffic to multiple backends. It is designed to be simple to use and easy to configure.

Features

  • Simple to use
  • Easy to configure
  • Highly-available

Installation

To install Sentinel, you can use the following command:

go get github.com/juls0730/sentinel

Usage

all you need is a structure that holds the proxyManager structure and a function that returns a transport for the proxyManager

package main

import (
	"github.com/juls0730/sentinel"
	"net/http"
)

func main() {
	proxyManager := sentinel.NewProxyManager()
    
	proxyManager.ListenAndServe("localhost:8080")

    proxy, err := NewDeploymentProxy(socketPath)
	if err != nil {
		return err
	}

    proxyManager.AddProxy("text.local", proxy)
}

type unixDialer struct {
	socketPath string
}

// dialContext implements DialContext but ignored everthing and just gives you a connection to the unix socket
func (d *unixDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
	return net.Dial("unix", d.socketPath)
}

func getTransport(target string) *http.Transport {
	return &http.Transport{
		DialContext:         (&unixDialer{socketPath: socket}).DialContext,
		MaxIdleConns:        100,
		IdleConnTimeout:     90 * time.Second,
		MaxIdleConnsPerHost: 100,
		ForceAttemptHTTP2:   false,
	}

}

If you want more indepth examples of how to use Sentinel, you can check out gloom which is a plugin-based web server that uses Sentinel to distribute traffic to multiple backends, or Flux which is a mini-paas that uses Sentinel to distribute traffic to project containers.

Documentation

Index

Constants

View Source
const PROXY_SHUTDOWN_TIMEOUT = 30 * time.Second

TODO: make this configurable?

Variables

This section is empty.

Functions

This section is empty.

Types

type Proxy

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

func NewDeploymentProxy

func NewDeploymentProxy(target string, transportFunc func(string) *http.Transport) (*Proxy, error)

Creates a proxy for a given deployment

func (*Proxy) GracefulShutdown

func (p *Proxy) GracefulShutdown(shutdownFunc func()) error

waits for the proxy to be drained of connections within the shutdown timeout, then calls the shutdownFunc (the proxy should be removes or replaced in the ProxyMaager)

type ProxyManager

type ProxyManager struct {
	// string -> *Proxy
	syncmap.Map
	// contains filtered or unexported fields
}

this is the object that oversees the proxying of requests to the correct deployment

func NewProxyManager

func NewProxyManager(RequestLogger RequestLogger) *ProxyManager

func (*ProxyManager) AddProxy

func (proxyManager *ProxyManager) AddProxy(host string, proxy *Proxy)

Starts forwarding traffic to a deployment. The deployment must be ready to recieve requests before this is called.

func (*ProxyManager) ListenAndServe

func (proxyManager *ProxyManager) ListenAndServe(host string) error

func (*ProxyManager) RemoveDeployment

func (proxyManager *ProxyManager) RemoveDeployment(host string)

Stops forwarding traffic to a deployment

func (*ProxyManager) ServeHTTP

func (proxyManager *ProxyManager) ServeHTTP(w http.ResponseWriter, r *http.Request)

This function is responsible for taking an http request and forwarding it to the correct deployment

type RequestLogger

type RequestLogger interface {
	// LogRequest is called after an HTTP request has been processed by the proxy.
	// It provides details about the request and its outcome.
	LogRequest(host string, status int, latency time.Duration, ip, method, path string)
}

type ResponseWriterInterceptor

type ResponseWriterInterceptor struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriterInterceptor is a custom http.ResponseWriter that captures the status code.

func (*ResponseWriterInterceptor) WriteHeader

func (rw *ResponseWriterInterceptor) WriteHeader(code int)

Jump to

Keyboard shortcuts

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