Documentation
¶
Index ¶
- func BuildServerURL(server *Server) (string, error)
- func ChainMiddleware(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler
- func CleanupVisitors()
- func HealthChecker(server *Server) (bool, error)
- func NewProxy(target *url.URL) *httputil.ReverseProxy
- func Ping(w http.ResponseWriter, r *http.Request)
- func RateLimiterMiddleware(config any) func(next http.Handler) http.Handler
- type BalancerStrategy
- type HealthCheckStatus
- type MiddleWareName
- type Middleware
- type MiddlewareSets
- type MogolyMiddleware
- type RateLimitMiddlewareConfig
- type Server
- func (server *Server) AddNewBalancingServer(bs *Server)
- func (server *Server) CheckHealthAll() (*HealthCheckStatus, error)
- func (server *Server) CheckHealthAny(name string) (*ServerStatus, error)
- func (server *Server) CheckHealthSelf() (*ServerStatus, error)
- func (server *Server) DelBalancingServer(name string)
- func (server *Server) GetNextServer(strategy ServerStrategy) (*Server, error)
- func (server *Server) GetServer(name string) *Server
- func (server *Server) RollBack(servers []*Server)
- func (server *Server) RollBackAny(name string, newServer *Server) error
- func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (server *Server) UpgradeProxy() error
- type ServerStatus
- type ServerStrategy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildServerURL ¶
func ChainMiddleware ¶
func CleanupVisitors ¶
func CleanupVisitors()
func HealthChecker ¶
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 MiddlewareSets ¶
type MiddlewareSets map[MiddleWareName]struct { Fn MogolyMiddleware Conf any }
var MiddlewaresList MiddlewareSets = MiddlewareSets{ MogolyRatelimiter: struct { Fn MogolyMiddleware Conf any }{ Fn: RateLimiterMiddleware, Conf: RateLimitMiddlewareConfig{}, }, }
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 RandomStrategy ¶
func RoundRobinStrategy ¶
func (*Server) AddNewBalancingServer ¶
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) 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) RollBack ¶
RollBack replaces the current balancing set with the provided list atomically.
func (*Server) RollBackAny ¶
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 ¶
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" )