proxy

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: MIT Imports: 6 Imported by: 0

README

Gin Proxy

A production-grade reverse proxy library implemented in Go. It not only provides high performance and multiple load balancing strategies, but also supports dynamic route management, allowing you to add or remove backend servers at runtime via API — without restarting the service.

Core Features

  • Dynamic Service Discovery: Add or remove backend nodes in real-time through HTTP APIs.
  • High Performance Core: Built on net/http/httputil with deeply optimized connection pooling for effortless high-concurrency handling.
  • Rich Load Balancing Strategies: Includes Round Robin, The Least Connections, and IP Hash.
  • Active Health Checks: Automatically detects and isolates unhealthy nodes, and brings them back online once they recover.
  • Multi-route Support: Distribute traffic to different backend groups based on path prefixes.

Example of Usage

package main

import (
    "fmt"
    "github.com/go-dev-frame/sponge/pkg/gin/proxy"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    p := proxy.New(r) // default configuration, managerPrefixPath = "/endpoints"
    pass1(p)
    pass2(p)

    // Other normal routes
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{"message": "pong"})
    })

    fmt.Println("Gin server with dynamic proxy started on http://localhost:8080")

    r.Run(":8080")
}

func pass1(p *proxy.Proxy) {
    prefixPath := "/proxy/"
    initialTargets := []string{"http://localhost:8081", "http://localhost:8082"}
    err := p.Pass(prefixPath, initialTargets) // default configuration, balancer = RoundRobin, healthCheckInterval = 5s, healthCheckTimeout = 3s
    if err != nil {
        panic(err)
    }
}

func pass2(p *proxy.Proxy) {
    prefixPath := "/personal/"
    initialTargets := []string{"http://localhost:8083", "http://localhost:8084"}
    err := p.Pass(prefixPath, initialTargets)
    if err != nil {
        panic(err)
    }
}

Advanced Settings

  1. Configure the management endpoints' route prefix, middleware, and logger through function parameters of proxy.New.

    p := proxy.New(r, proxy.Config{
        proxy.WithManagerEndpoints("/admin", Middlewares...),
        proxy.WithLogger(zap.NewExample()),
    })
    
  2. Configure the load balancing type, health check interval, and middleware for the endpoints through function parameters of p.Pass.

    err := p.Pass("/proxy/", []string{"http://localhost:8081", "http://localhost:8082"}, proxy.Config{
        proxy.WithPassBalancer(proxy.BalancerIPHash),
        proxy.WithPassHealthCheck(time.Second * 5, time.Second * 3),
        proxy.WithPassMiddlewares(Middlewares...),
    })
    

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BalancerRoundRobin = "round_robin"
	BalancerLeastConn  = "least_conn"
	BalancerIPHash     = "ip_hash"
)

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option set options.

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger sets logger.

func WithManagerEndpoints

func WithManagerEndpoints(managerPrefixPath string, middlewares ...gin.HandlerFunc) Option

WithManagerEndpoints sets manager prefix path and middlewares, managerPrefixPath default "/endpoints".

type PassOption

type PassOption func(*passOptions)

PassOption set passOptions.

func WithPassBalancer

func WithPassBalancer(balancerType string) PassOption

WithPassBalancer sets balancer type.

func WithPassHealthCheck

func WithPassHealthCheck(interval time.Duration, timeout time.Duration) PassOption

WithPassHealthCheck sets health check interval and timeout.

func WithPassMiddlewares

func WithPassMiddlewares(middlewares ...gin.HandlerFunc) PassOption

WithPassMiddlewares sets proxy middlewares.

type Proxy

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

Proxy is a proxy server.

func New

func New(r *gin.Engine, opts ...Option) *Proxy

New creates a new Proxy instance.

func (*Proxy) Pass

func (p *Proxy) Pass(prefixPath string, endpoints []string, opts ...PassOption) error

Pass registers proxy endpoints to gin engine.

Jump to

Keyboard shortcuts

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