server

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildServerURL

func BuildServerURL(server *Server) (string, error)

func ChainMiddleware

func ChainMiddleware(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler

func CleanupVisitors

func CleanupVisitors()

func HealthChecker

func HealthChecker(server *Server) (bool, error)

func NewProxy

func NewProxy(target *url.URL) *httputil.ReverseProxy

func Ping

func Ping(w http.ResponseWriter, r *http.Request)

ping returns a "pong" message consider registering this Handler for the health checking logic

func RateLimiterMiddleware

func RateLimiterMiddleware(config any) func(next http.Handler) http.Handler

Types

type BalancerStrategy

type BalancerStrategy interface {
	GetNextServer(strategy ServerStrategy) (*Server, error)
}

type HealthCheckStatus

type HealthCheckStatus struct {
	Pass      []ServerStatus `json:"pass" yaml:"pass"` // Array of successful HealthCheck result
	Fail      []ServerStatus `json:"fail" yaml:"fail"` // Array of failure HealthCheck Result
	CheckTime time.Time
	Duration  time.Duration
}

type MiddleWareName

type MiddleWareName string
const (
	MogolyRatelimiter MiddleWareName = "mogoly:ratelimiter"
)

type Middleware

type Middleware struct {
	Name   string `json:"name" yaml:"name"`
	Config any    `json:"config,omitempty" yaml:"config,omitempty"`
}

type MiddlewareSets

type MiddlewareSets map[MiddleWareName]struct {
	Fn   MogolyMiddleware
	Conf any
}

type MogolyMiddleware

type MogolyMiddleware func(config any) func(next http.Handler) http.Handler

type RateLimitMiddlewareConfig

type RateLimitMiddlewareConfig struct {
	ReqPerMinute int           `json:"request_per_minute,omitempty" yaml:"request_per_minute,omitempty"`
	LimitWindow  time.Duration `json:"limit_window,omitempty" yaml:"limit_window,omitempty"`
}

type Server

type Server struct {
	ID               string       // THe server ID based on its registration order
	Name             string       `json:"name,omitempty" yaml:"name,omitempty"`             // The server name
	Protocol         string       `json:"protocol,omitempty" yaml:"protocol,omitempty"`     // The protocol for the server this field can be `http` or `https`
	Host             string       `json:"host,omitempty" yaml:"host,omitempty"`             // The server host
	Port             int          `json:"port,omitempty" yaml:"port,omitempty"`             // The port on which the server is running
	URL              string       `json:"url,omitempty" yaml:"url,omitempty"`               // If this field is provided the URL will be used for request forwarding
	IsHealthy        bool         `json:"is_healthy,omitempty" yaml:"is_healthy,omitempty"` // Specifying the server health check state
	BalancingServers []*Server    `json:"balance,omitempty" yaml:"balance,omitempty"`       // If specified these servers will be used for load balancing request
	Middlewares      []Middleware `json:"middlewares,omitempty" yaml:"middlewares,omitempty"`
	LastHealthCheck  *time.Time

	ForceTLS bool
	// contains filtered or unexported fields
}

func LeastConnectionsStrategy

func LeastConnectionsStrategy(server *Server) (*Server, error)

func RandomStrategy

func RandomStrategy(server *Server) (*Server, error)

func RoundRobinStrategy

func RoundRobinStrategy(server *Server) (*Server, error)

func (*Server) AddNewBalancingServer

func (server *Server) AddNewBalancingServer(bs *Server)

func (*Server) CheckHealthAll

func (server *Server) CheckHealthAll() (*HealthCheckStatus, error)

CheckHealthAll performs health checks without holding the lock during network I/O.

func (*Server) CheckHealthAny

func (server *Server) CheckHealthAny(name string) (*ServerStatus, error)

func (*Server) CheckHealthSelf

func (server *Server) CheckHealthSelf() (*ServerStatus, error)

func (*Server) DelBalancingServer

func (server *Server) DelBalancingServer(name string)

func (*Server) GetNextServer

func (server *Server) GetNextServer(strategy ServerStrategy) (*Server, error)

GetNextServer returns the next healthy server using round-robin. If all are unhealthy or list is empty, an error is returned.

func (*Server) GetServer

func (server *Server) GetServer(name string) *Server

func (*Server) RollBack

func (server *Server) RollBack(servers []*Server)

RollBack replaces the current balancing set with the provided list atomically.

func (*Server) RollBackAny

func (server *Server) RollBackAny(name string, newServer *Server) error

RollBackAny replaces a named server with a new one, or appends when name is empty.

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements a minimal LB+proxy. It avoids mutating the receiver on retry and constructs the target URL using ResolveReference to handle paths and queries correctly.

func (*Server) UpgradeProxy

func (server *Server) UpgradeProxy() error

UpgradeProxy ensures server.Proxy is initialized for this server.

type ServerStatus

type ServerStatus struct {
	Name    string `json:"name" yaml:"name"`       // The server name
	Url     string `json:"url" yaml:"url"`         // HealthCheck url
	Healthy bool   `json:"healthy" yaml:"healthy"` // healthCheck status
}

The result of a health checking process for a server

type ServerStrategy

type ServerStrategy string
const (
	RoundRobin       ServerStrategy = "round_robin"
	LeastConnections ServerStrategy = "least_connections"
	Random           ServerStrategy = "random"
)
const (
	ServerStrategyRoundRobin ServerStrategy = "round_robin"
	ServerStrategyRandom     ServerStrategy = "random"
)

Jump to

Keyboard shortcuts

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