provider

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 6 Imported by: 0

README

provider

Generic provider registry, manager, and selection strategies for pluggable backends.

Install

go get github.com/kbukum/gokit

Quick Start

package main

import (
    "context"
    "fmt"
    "github.com/kbukum/gokit/provider"
)

// MyProvider implements provider.Provider
type MyProvider struct{ name string }
func (p *MyProvider) Name() string                          { return p.name }
func (p *MyProvider) IsAvailable(ctx context.Context) bool  { return true }

func main() {
    registry := provider.NewRegistry[*MyProvider]()
    selector := &provider.PrioritySelector[*MyProvider]{}
    mgr := provider.NewManager(registry, selector)

    // Register a factory
    mgr.Register("primary", func(cfg map[string]any) (*MyProvider, error) {
        return &MyProvider{name: "primary"}, nil
    })
    mgr.Initialize("primary", nil)
    mgr.SetDefault("primary")

    // Get best available provider
    p, err := mgr.Get(context.Background())
    fmt.Println(p.Name(), err) // "primary" <nil>
}

Key Types & Functions

Name Description
Provider Interface: Name() + IsAvailable()
Registry[T] Generic provider registry with factory support
Manager[T] Manages provider lifecycle and selection
Factory[T] Factory function type for creating providers
Selector[T] Interface for provider selection strategy
PrioritySelector / RoundRobinSelector / HealthCheckSelector Built-in selection strategies

⬅ Back to main README

Documentation

Overview

Package provider implements a generic provider framework using Go generics for swappable backends with runtime switching capabilities.

It provides a registry for managing multiple provider implementations with factory-based instantiation, availability checking, and runtime selection.

Usage

reg := provider.NewRegistry[MyProvider]()
reg.Register("default", myFactory)
p, err := reg.Get("default")

This package is used by gokit's provider-pattern modules (llm, transcription, diarization) for pluggable backend support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory[T Provider] func(cfg map[string]any) (T, error)

Factory creates a provider instance from configuration.

type HealthCheckSelector

type HealthCheckSelector[T Provider] struct{}

HealthCheckSelector picks the first available provider by calling IsAvailable.

func (*HealthCheckSelector[T]) Select

func (s *HealthCheckSelector[T]) Select(ctx context.Context, providers map[string]T) (T, error)

Select returns the first provider that reports as available.

type Manager

type Manager[T Provider] struct {
	// contains filtered or unexported fields
}

Manager provides the main API for working with providers, combining a Registry for storage and a Selector for choosing providers.

func NewManager

func NewManager[T Provider](registry *Registry[T], selector Selector[T]) *Manager[T]

NewManager creates a Manager backed by the given registry and selector.

func (*Manager[T]) Available

func (m *Manager[T]) Available() []string

Available returns the names of all initialized providers.

func (*Manager[T]) Get

func (m *Manager[T]) Get(ctx context.Context) (T, error)

Get returns a provider chosen by the selector, or the default if set.

func (*Manager[T]) GetByName

func (m *Manager[T]) GetByName(name string) (T, error)

GetByName returns a specific provider by name.

func (*Manager[T]) Initialize

func (m *Manager[T]) Initialize(name string, cfg map[string]any) error

Initialize creates a provider from its factory and stores it for use.

func (*Manager[T]) Register

func (m *Manager[T]) Register(name string, factory Factory[T])

Register adds a factory to the underlying registry.

func (*Manager[T]) SetDefault

func (m *Manager[T]) SetDefault(name string) error

SetDefault sets the default provider by name.

type PrioritySelector

type PrioritySelector[T Provider] struct {
	// Priority is the ordered list of provider names to try.
	Priority []string
}

PrioritySelector tries providers in the given priority order and returns the first one that is available.

func (*PrioritySelector[T]) Select

func (s *PrioritySelector[T]) Select(ctx context.Context, providers map[string]T) (T, error)

Select returns the first available provider in priority order.

type Provider

type Provider interface {
	// Name returns the provider's unique name.
	Name() string
	// IsAvailable checks if the provider is ready to handle requests.
	IsAvailable(ctx context.Context) bool
}

Provider is the base interface all providers must implement.

type Registry

type Registry[T Provider] struct {
	// contains filtered or unexported fields
}

Registry manages named provider factories and cached instances.

func NewRegistry

func NewRegistry[T Provider]() *Registry[T]

NewRegistry creates a new empty Registry.

func (*Registry[T]) Create

func (r *Registry[T]) Create(name string, cfg map[string]any) (T, error)

Create instantiates a provider using the named factory and config.

func (*Registry[T]) Get

func (r *Registry[T]) Get(name string) (T, bool)

Get returns a cached provider instance by name.

func (*Registry[T]) List

func (r *Registry[T]) List() []string

List returns sorted names of all registered factories.

func (*Registry[T]) RegisterFactory

func (r *Registry[T]) RegisterFactory(name string, factory Factory[T])

RegisterFactory registers a named factory for creating providers.

func (*Registry[T]) Set

func (r *Registry[T]) Set(name string, instance T)

Set caches a provider instance by name.

type RoundRobinSelector

type RoundRobinSelector[T Provider] struct {
	// contains filtered or unexported fields
}

RoundRobinSelector distributes requests across providers.

func (*RoundRobinSelector[T]) Select

func (s *RoundRobinSelector[T]) Select(ctx context.Context, providers map[string]T) (T, error)

Select picks the next provider using round-robin distribution.

type Selector

type Selector[T Provider] interface {
	Select(ctx context.Context, providers map[string]T) (T, error)
}

Selector picks a provider from the available options.

Jump to

Keyboard shortcuts

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