stateful_proxy

package module
v0.0.0-...-231c85c Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 7 Imported by: 0

README

Stateful Proxy

API Reference

This project is just a middleware that works as a reverse proxy to route request to another instance of the same application.

This may be useful for steteful applications that may hold session in memory or any other reason that may benefit due to performance or whatever reason that routing to a same instance may be a good thing.

It relies on a Redis Cluster to store routes.

Install

go get github.com/dalthon/stateful_proxy

Usage

This is the example provided at examples/01-simple.go:

package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
	"time"

	sp "github.com/dalthon/stateful_proxy"

	redis "github.com/redis/go-redis/v9"
)

func main() {
	cluster := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs:    strings.Split(os.Getenv("REDIS_URLS"), ","),
		Username: os.Getenv("REDIS_USERNAME"),
		Password: os.Getenv("REDIS_PASSWORD"),
		PoolSize: 20,
	})

	url := "http://" + os.Getenv("HOSTNAME") + ":3000"

	heartbeatDuration := 10 * time.Second
	proxy := sp.New(cluster, url, heartbeatDuration, &sp.Config{})

	config := &sp.Config{
		Duration: 30 * time.Second,
	}

	http.HandleFunc("/", proxy.Middleware(getRoot, config))
	http.HandleFunc("/hello", proxy.Middleware(getHello, config))

	fmt.Println("Starting ", url)
	if err := http.ListenAndServe(":3000", nil); err != nil {
		panic(err)
	}

	fmt.Println("Finished!")
}

func getRoot(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("got / request\n")
	io.WriteString(w, "This is my website!\n")
}

func getHello(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("got /hello request\n")
	io.WriteString(w, "Hello, HTTP!\n")
}

TODO

  • TEST IT!

Contributing

Pull requests and issues are welcome! I'll try to review them as soon as I can.

This project is quite simple and its Makefile is quite useful to do whatever you may need. Run make help for more info.

To run tests, run make test.

To run test with coverage, run make cover.

To run a full featured example available at examples/01-simple.go, run make example-01.

License

This project is released under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	PartitionKey  func(http.ResponseWriter, *http.Request) string
	Duration      time.Duration
	ManualRelease bool
}

type StatefulProxy

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

func New

func New(cluster *redis.ClusterClient, stringUrl string, heartbeatDuration time.Duration, config *Config) *StatefulProxy

func (*StatefulProxy) Close

func (proxy *StatefulProxy) Close()

func (*StatefulProxy) Middleware

func (proxy *StatefulProxy) Middleware(handleFunc handlerFunc, config *Config) handlerFunc

func (*StatefulProxy) PartitionHeartbeat

func (proxy *StatefulProxy) PartitionHeartbeat(label string, duration time.Duration)

func (*StatefulProxy) Release

func (proxy *StatefulProxy) Release(label string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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