openurl

package
v5.0.0-...-4117378 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package openurl provides helpers for URLMux and URLOpeners in portable APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSNToURL

func DSNToURL(driver string, dsn string, prefix string) *url.URL

func GetMemPoolShardExpr

func GetMemPoolShardExpr() string

func NewPoolWatcher

func NewPoolWatcher[W watch.Watcher](p *Pool[W]) watch.Watcher

func RegisterTemplateInjector

func RegisterTemplateInjector(injector TemplateInjector)

RegisterTemplateInjector appends a TemplateInjector to extract info from context before passing to templates

func RegisterTplFunc

func RegisterTplFunc(name string, fn any)

RegisterTplFunc appends an arbitrary executable func

func RegisterURLTemplate

func RegisterURLTemplate(scheme string, opener TemplateOpener, asDefault ...bool)

RegisterURLTemplate registers a template type by scheme. Default go-template is supported by default

func SetMemPoolShardExpr

func SetMemPoolShardExpr(v string)

func URLToDSN

func URLToDSN(u *url.URL) (driver string, dsn string, prefix string)

Types

type MustOpener

type MustOpener[T any] func(ctx context.Context, url string) T

type Opener

type Opener[T any] func(ctx context.Context, url string) (T, error)

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

func MemPool

func MemPool[T any](ctx context.Context, opener Opener[T], opt ...PoolOption[T]) (*Pool[T], error)

MemPool opens a pool that differentiate based on memPoolShardExpr

func MustMemPool

func MustMemPool[T any](ctx context.Context, opener MustOpener[T], opt ...PoolOption[T]) *Pool[T]

MustMemPool opens a pool that differentiate based on memPoolShardExpr, and does not trigger any errors

func OpenPool

func OpenPool[T any](ctx context.Context, uu []string, opener Opener[T], opt ...PoolOption[T]) (*Pool[T], error)

OpenPool creates a pool of resources that are resolved at Get() time. If PoolOption is passed, it will monitor idle resources and close them regulary to free memory Maybe PoolOptions could also be passed by the URL

func (*Pool[T]) Close

func (m *Pool[T]) Close(ctx context.Context, iterate ...func(key string, res T) error) error

Close closes all underlying resources TODO - options with close callback

func (*Pool[T]) Del

func (m *Pool[T]) Del(ctx context.Context, resolutionData ...map[string]interface{}) (bool, error)

func (*Pool[T]) Get

func (m *Pool[T]) Get(ctx context.Context, resolutionData ...map[string]interface{}) (T, error)

func (*Pool[T]) Iterate

func (m *Pool[T]) Iterate(ctx context.Context, it func(key string, res T) error) error

func (*Pool[T]) On

func (m *Pool[T]) On(action PoolAction, f func(k string, t T)) Stopper

type PoolAction

type PoolAction int32
const (
	ADD PoolAction = iota
	IDLE
	DELETE
)

type PoolOption

type PoolOption[T any] func(*PoolOptions[T])

func WithCleanTicker

func WithCleanTicker[T any](duration time.Duration) PoolOption[T]

func WithMaxIdleTime

func WithMaxIdleTime[T any](duration time.Duration) PoolOption[T]

func WithOpener

func WithOpener[T any](opener Opener[T]) PoolOption[T]

func WithProvider

func WithProvider[T any](provider Provider[T]) PoolOption[T]

type PoolOptions

type PoolOptions[T any] struct {
	CleanTicker time.Duration
	MaxIdleTime time.Duration
	Opener[T]
}

type Provider

type Provider[T any] interface {
	Open(ctx context.Context, url string) (T, error)
	Get(ctx context.Context, url string) (T, error)
}

type Resolver

type Resolver[T any] interface {
	Get(ctx context.Context, data ...map[string]interface{}) (T, error)
}

type ResourceClosable

type ResourceClosable interface {
	Close(context.Context) error
}

type SchemeMap

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

SchemeMap maps URL schemes to values. The zero value is an empty map, ready for use.

func (*SchemeMap) FromString

func (m *SchemeMap) FromString(typ, urlstr string) (interface{}, *url.URL, error)

FromString parses urlstr as an URL and looks up the value for the URL's scheme.

func (*SchemeMap) FromStringNoParse

func (m *SchemeMap) FromStringNoParse(typ, urlstr string) (interface{}, error)

FromStringNoParse parses urlstr as an URL and looks up the value for the URL's scheme.

func (*SchemeMap) FromURL

func (m *SchemeMap) FromURL(typ string, u *url.URL) (interface{}, error)

FromURL looks up the value for u's scheme.

func (*SchemeMap) Register

func (m *SchemeMap) Register(api, typ, scheme string, value interface{})

Register registers scheme for value; subsequent calls to FromString or FromURL with scheme will return value. api is the portable API name (e.g., "blob"); the same value should always be passed. It should be in all lowercase. typ is the portable type (e.g., "Bucket"). Register panics if scheme has already been registered.

func (*SchemeMap) Schemes

func (m *SchemeMap) Schemes() []string

Schemes returns a sorted slice of the registered schemes.

func (*SchemeMap) ValidScheme

func (m *SchemeMap) ValidScheme(scheme string) bool

ValidScheme returns true if scheme has been registered.

type Stopper

type Stopper func()

type StringTemplate

type StringTemplate interface {
	Resolve(ctx context.Context, optionalData ...map[string]interface{}) (string, error)
}

type Template

type Template interface {
	StringTemplate
	ResolveURL(ctx context.Context, optionalData ...map[string]interface{}) (*url.URL, error)
}

func URLTemplate

func URLTemplate(rawURL string) (Template, error)

URLTemplate initialize a Template based on rawURL for later resolution

type TemplateInjector

type TemplateInjector func(context.Context, map[string]interface{}) error

type TemplateOpener

type TemplateOpener func(tplString string) (Template, error)

type URLMux

type URLMux[T any] struct {
	// contains filtered or unexported fields
}

URLMux is a URL opener multiplexer. It matches the scheme of the URLs against a set of registered schemes and calls the opener that matches the URL's scheme. See https://gocloud.dev/concepts/urls/ for more information.

The zero value is a multiplexer with no registered schemes.

func NewURLMux

func NewURLMux[T any](api string) *URLMux[T]

Driver packages can use this to register their TopicURLOpener and/or SubscriptionURLOpener on the mux.

func (*URLMux[T]) Open

func (mux *URLMux[T]) Open(ctx context.Context, urlstr string) (T, error)

OpenStore calls OpenURL with the URL parsed from urlstr. OpenStore is safe to call from multiple goroutines.

func (*URLMux[T]) Register

func (mux *URLMux[T]) Register(scheme string, opener URLOpener[T])

Register registers the opener with the given scheme. If an opener already exists for the scheme, Register panics.

func (*URLMux[T]) Schemes

func (mux *URLMux[T]) Schemes() []string

Schemes returns a sorted slice of the registered schemes.

func (*URLMux[T]) ValidScheme

func (mux *URLMux[T]) ValidScheme(scheme string) bool

ValidScheme returns true if scheme has been registered.

type URLOpener

type URLOpener[T any] interface {
	Open(ctx context.Context, u *url.URL) (T, error)
}

URLOpener represents types than can open Registries based on a URL. The opener must not modify the URL argument. OpenURL must be safe to call from multiple goroutines.

This interface is generally implemented by types in driver packages.

Jump to

Keyboard shortcuts

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