hub

package
v0.0.35 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 3 Imported by: 0

README

sup/hub

Go Reference Test License

hub is a high-performance, generic load balancer and distribution utility for Go. Designed as a companion for the sup actor library, it allows you to group multiple function signatures (routees) and call them using various strategies like Round-Robin, Random selection, or Fixed affinity.

It is stateless, thread-safe, and designed to be virtually overhead-free in your hot path.

Features

  • Generic-first: Works with any function signature or type using Go Generics.
  • Multiple Strategies: Built-in Round-Robin and Random selection.
  • Resilience: Built-in Retry logic with automatic failover and error propagation.
  • Bulk Operations: Broadcast for sequential updates and FanOut for parallel execution.

Installation

go get github.com/webermarci/sup/hub

Quick Start

package main

import (
	"github.com/webermarci/sup"
	"github.com/webermarci/sup/hub"
)

type Worker struct{
}

func (w *Worker) Process(data string) error {
  if data == "fail" {
    return fmt.Errorf("processing failed")
  }
  return nil
}

func main() {
	w1 := Worker{}
	w2 := Worker{}

	// Create a Round-Robin Hub for the "Process" capability
	workers := hub.NewRoundRobin(w1.Process, w2.Process)

	if err := workers.Next()("task-data"); err != nil {
		fmt.Println("Error processing task:", err)
	}
	
	workers.Broadcast("update-data") // Broadcast to all workers
	workser.FanOut("parallel-task-data") // Fan-out to all workers in parallel
	workers.FanOutWait("parallel-task-data") // Fan-out and wait for all to complete

	if err := workers.Retry(3, func(fn func(data string) error) error {
		return fn("task-data") // Just call the function, but you could add backoff, logging, etc. here
	}); err != nil {
    fmt.Println("All retries failed:", err)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hub

type Hub[F any] struct {
	// contains filtered or unexported fields
}

Hub is a simple load balancer that distributes work across a set of routees (workers). It supports different routing strategies, such as round-robin and random selection. The Next method returns the next routee according to the configured strategy. The Broadcast method applies a function to all routees sequentially, while FanOut does so concurrently.

func NewRandom

func NewRandom[F any](routees ...F) *Hub[F]

NewRandom creates a new Hub that uses random selection to distribute work across the given routees.

func NewRoundRobin

func NewRoundRobin[F any](routees ...F) *Hub[F]

NewRoundRobin creates a new Hub that uses round-robin routing to distribute work across the given routees.

func (*Hub[F]) Broadcast

func (h *Hub[F]) Broadcast(fn func(F))

Broadcast applies the given function to all routees sequentially.

func (*Hub[F]) FanOut

func (h *Hub[F]) FanOut(fn func(F))

FanOut applies the given function to all routees concurrently.

func (*Hub[F]) FanOutWait

func (h *Hub[F]) FanOutWait(fn func(F))

FanOutWait applies the given function to all routees concurrently and waits for all of them to complete before returning.

func (*Hub[F]) Len

func (h *Hub[F]) Len() int

Len returns the number of routees in the Hub.

func (*Hub[F]) Next

func (h *Hub[F]) Next() F

Next returns the next routee according to the configured routing strategy.

func (*Hub[F]) Retry

func (h *Hub[F]) Retry(limit int, run func(F) error) error

Retry attempts to execute a function against routees until it succeeds or the limit is reached. It returns the last error encountered if all attempts fail.

func (*Hub[F]) Sticky

func (h *Hub[F]) Sticky(key uint64) F

Sticky returns a routee based on the given key, using modulo to ensure it falls within the range of available routees.

Jump to

Keyboard shortcuts

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