discovery

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 4 Imported by: 61

Documentation

Overview

Package discovery defines interfaces for service discovery. Developers that are willing to customize service discovery should implement their own Resolver and supply it with the option WithResolver at client's creation.

Index

Constants

View Source
const (
	ChangeEventName = "discovery_change"
	DeleteEventName = "discovery_delete"
)

Predefined discovery event names.

View Source
const DefaultWeight = 10

DefaultWeight is the default weight for an instance.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Result  Result
	Added   []Instance
	Updated []Instance
	Removed []Instance
}

Change contains the difference between the current discovery result and the previous one. It is designed for providing detail information when dispatching an event for service discovery result change. Since the loadbalancer may rely on caching the result of resolver to improve performance, the resolver implementation should dispatch an event when result changes.

func DefaultDiff

func DefaultDiff(cacheKey string, prev, next Result) (Change, bool)

DefaultDiff provides a natural implementation for the Diff method of the Resolver interface.

type Instance

type Instance interface {
	Address() net.Addr
	Weight() int
	Tag(key string) (value string, exist bool)
}

Instance contains information of an instance from the target service.

func NewInstance

func NewInstance(network, address string, weight int, tags map[string]string) Instance

NewInstance creates a Instance using the given network, address and tags

type Resolver

type Resolver interface {
	// Target should return a description for the given target that is suitable for being a key for cache.
	Target(ctx context.Context, target rpcinfo.EndpointInfo) (description string)

	// Resolve returns a list of instances for the given description of a target.
	Resolve(ctx context.Context, desc string) (Result, error)

	// Diff computes the difference between two results.
	// When `next` is cacheable, the Change should be cacheable, too. And the `Result` field's CacheKey in
	// the return value should be set with the given cacheKey.
	Diff(cacheKey string, prev, next Result) (Change, bool)

	// Name returns the name of the resolver.
	Name() string
}

Resolver resolves the target endpoint into a list of Instance.

type Result

type Result struct {
	Cacheable bool
	CacheKey  string
	Instances []Instance
}

Result contains the result of service discovery process. Cacheable tells whether the instance list can/should be cached. When Cacheable is true, CacheKey can be used to map the instance list in cache.

type SynthesizedResolver

type SynthesizedResolver struct {
	TargetFunc  func(ctx context.Context, target rpcinfo.EndpointInfo) string
	ResolveFunc func(ctx context.Context, key string) (Result, error)
	DiffFunc    func(key string, prev, next Result) (Change, bool)
	NameFunc    func() string
}

SynthesizedResolver synthesizes a Resolver using a resolve function.

func (SynthesizedResolver) Diff

func (sr SynthesizedResolver) Diff(key string, prev, next Result) (Change, bool)

Diff implements the Resolver interface.

func (SynthesizedResolver) Name

func (sr SynthesizedResolver) Name() string

Name implements the Resolver interface

func (SynthesizedResolver) Resolve

func (sr SynthesizedResolver) Resolve(ctx context.Context, key string) (Result, error)

Resolve implements the Resolver interface.

func (SynthesizedResolver) Target

Target implements the Resolver interface.

Jump to

Keyboard shortcuts

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