discovery

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package discovery provides a pluggable interface and Consul-based implementation for service discovery and registration in distributed systems.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultHealthCheckTTL          = 5 * time.Second // DefaultTTL is the default TTL for health checks.
	DefaultDeregisterCriticalAfter = 5 * time.Minute // Deregister a service if critical for this long.
	DefaultTTLUpdateInterval       = 5 * time.Second // Default interval for updating the TTL.

)

Functions

This section is empty.

Types

type Options

type Options struct {
	Provider   ProviderType  // Provider specifies the type of service discovery backend (e.g., Consul, Etcd).
	Address    string        // Address is the endpoint URL of the discovery provider
	Token      string        // Token is an optional ACL token for authenticating with the discovery provider.
	Scheme     string        // Scheme is the URL scheme to use (e.g., "http" or "https").
	Datacenter string        // Datacenter specifies the target datacenter to use for lookups and registration.
	HTTPClient *http.Client  // HTTPClient allows the use of a custom HTTP client.
	TLSConfig  *TLSConfig    // TLSConfig contains optional TLS settings used when communicating over HTTPS.
	WaitTime   time.Duration // WaitTime is the maximum duration to wait for blocking queries (e.g., in Watch).
	Logger     logger.Logger // Logger for logging messages and errors.
}

Options defines the configuration parameters required to initialize a service discovery provider such as Consul, Etcd, etc.

func (*Options) Validate

func (o *Options) Validate() error

Validate checks if the required fields in Options are set. It returns an error if any required field is missing or invalid.

type ProviderType

type ProviderType string

ProviderType defines supported discovery provider types.

const (
	ProviderConsul ProviderType = "consul" // Consul discovery provider
)

type ServiceDiscovery

type ServiceDiscovery interface {
	// Register a service instance with the discovery provider.
	Register(ctx context.Context, instance *ServiceInstance) error

	// Deregister removes the service instance from the registry.
	Deregister(ctx context.Context, instanceID string) error

	// Discover returns healthy service instances by name.
	Discover(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

	// Watch monitors changes to a given service and triggers the callback.
	Watch(ctx context.Context, serviceName string, onChange func([]*ServiceInstance)) error

	// Close gracefully shuts down the discovery client (e.g., closes connections, stops watchers).
	Close() error
}

ServiceDiscovery defines the contract for a service discovery system.

func New

func New(ctx context.Context, opts *Options) (ServiceDiscovery, error)

New creates a new instance of ServiceDiscovery based on the given configuration.

Supported providers:

  • "consul": Uses HashiCorp Consul as the discovery backend.

Parameters:

  • ctx: Context for cancellation (used for health check registration, etc.).
  • cfg: Configuration specifying provider type and address.

Returns:

  • ServiceDiscovery: Concrete implementation (e.g., consulDiscovery).
  • error: If the provider is unsupported or setup fails.

func NewConsulDiscovery

func NewConsulDiscovery(opts *Options) (ServiceDiscovery, error)

NewConsulDiscovery creates and returns a new Consul-based ServiceDiscovery instance.

Parameters:

  • addr: Address of the Consul agent (e.g., "localhost:8500").

Returns:

  • ServiceDiscovery: A fully initialized instance of the Consul-based discovery system.
  • error: Any error encountered while creating the Consul client.

type ServiceInstance

type ServiceInstance struct {
	ID       string
	Name     string
	Address  string
	Port     int
	Tags     []string
	Metadata map[string]string
}

ServiceInstance represents a single instance of a registered service.

type TLSConfig

type TLSConfig struct {
	CAFile             string // path to the CA certificate used to verify the server's certificate.
	CertFile           string // path to the client's TLS certificate.
	KeyFile            string // path to the client's TLS private key.
	InsecureSkipVerify bool   // if true, disables verification of the server's certificate.
}

TLSConfig contains security parameters used for mutual TLS authentication and secure communication with the service discovery backend.

Jump to

Keyboard shortcuts

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