router

package
v0.0.0-...-0f67f6e Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 3 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	// Name is the name of the entry being changed.
	Name string
	// Old holds the original value, or nil if there was no original.
	Old any
	// New holds the new value, or nil if there is no new value.
	New any
	// Auto is true if New was created via a Factory.
	Auto bool
}

Change represents a change to this routers contents.

type Factory

type Factory func(string) (any, error) // returns the type MyServiceClient

type Option

type Option func(r *router)

func WithFactory

func WithFactory(f Factory) Option

WithFactory configures a Router to call the given function when Get is called and no existing client is known. Prefer using the generated WithMyServiceClientFactory methods in the trait packages. The given factory may be called multiple times with the same name if concurrent access is performed. Only one returned client will be remembered. Use WithOnCommit if you need to trigger side effects as part of your client creation.

func WithFallback

func WithFallback(f Factory) Option

WithFallback configures a Router to ask the given function when Get is called and no existing client is known. If WithFallback and WithFactory are both configured, WithFallback will be called first, only using WithFactory if WithFallback returns nil or an error.

func WithOnChange

func WithOnChange(onChange func(Change)) Option

WithOnChange registers a func that will be called whenever the contents of this router change. Changes include calls to Router.Add, Router.Remove, or Router.Get with a configured Factory.

type Router

type Router interface {
	// Add adds a named client to this Router.
	// Add returns the old client associated with this name, or nil if there wasn't one.
	// If HoldsType returns false for the given client, this will panic.
	Add(name string, client any) any
	// HoldsType returns true if this Router holds clients of the specified type.
	HoldsType(client any) bool
	// Remove removes and returns a named client.
	Remove(name string) any
	// Has returns true if this Router has a client with the given name.
	Has(name string) bool
	// Get returns the client for the given name.
	// An error will be returned if no such client exists.
	Get(name string) (any, error)
}

Router tracks a registry of gRPC clients. Typically used by code generated via the protoc-gen-router plugin.

func NewRouter

func NewRouter(opts ...Option) Router

NewRouter creates a new instance of Router with the given options.

Jump to

Keyboard shortcuts

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