registry

package
v0.0.0-...-e2755d2 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Consul constant for getting ConsulClient from Registry factory
	Consul = "consul"
)

Variables

This section is empty.

Functions

func GetKeys

func GetKeys(pairs []Pair) []string

GetKeys returns slice of keys from slice of pairs

Types

type Config

type Config struct {
	Driver       string
	ConsulConfig ConsulConfig
}

Config holds registry configuration

type ConsulClient

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

ConsulClient ...

func (*ConsulClient) Acquire

func (c *ConsulClient) Acquire(ctx context.Context, sessionID string, key string, value []byte) (bool, error)

Acquire a lock

func (*ConsulClient) DeleteTree

func (c *ConsulClient) DeleteTree(ctx context.Context, key string) error

DeleteTree deletes all keys under a prefix

func (*ConsulClient) Deregister

func (c *ConsulClient) Deregister(ctx context.Context, sessionID string) error

Deregister node, destroy consul session

func (*ConsulClient) Exists

func (c *ConsulClient) Exists(ctx context.Context, key string) (bool, error)

Exists checks the existence of a key

func (*ConsulClient) Get

func (c *ConsulClient) Get(ctx context.Context, key string) (*Pair, error)

Get returns a value for a key

func (*ConsulClient) IsAlive

func (c *ConsulClient) IsAlive(_ context.Context) (bool, error)

IsAlive checks the health of the consul

func (*ConsulClient) IsRegistered

func (c *ConsulClient) IsRegistered(ctx context.Context, sessionID string) bool

IsRegistered is used checking the registration status

func (*ConsulClient) List

func (c *ConsulClient) List(ctx context.Context, prefix string) ([]Pair, error)

List returns a slice of pairs for a key prefix

func (*ConsulClient) ListKeys

func (c *ConsulClient) ListKeys(ctx context.Context, key string) ([]string, error)

ListKeys returns a value for a key

func (*ConsulClient) Put

func (c *ConsulClient) Put(ctx context.Context, key string, value []byte) (string, error)

Put a key value pair

func (*ConsulClient) Register

func (c *ConsulClient) Register(ctx context.Context, name string, ttl time.Duration) (string, error)

Register is used for node registration which creates a session to consul

func (*ConsulClient) Release

func (c *ConsulClient) Release(ctx context.Context, sessionID string, key string, value string) bool

Release a lock

func (*ConsulClient) Renew

func (c *ConsulClient) Renew(ctx context.Context, sessionID string) error

Renew renews consul session

func (*ConsulClient) RenewPeriodic

func (c *ConsulClient) RenewPeriodic(ctx context.Context, sessionID string, ttl time.Duration, doneCh <-chan struct{}) error

RenewPeriodic renews consul session periodically until doneCh signals done

func (*ConsulClient) Watch

func (c *ConsulClient) Watch(ctx context.Context, wh *WatchConfig) (IWatcher, error)

Watch watches a key and returns a watcher instance, this instance should be used to terminate the watch

type ConsulConfig

type ConsulConfig struct {
	api.Config
}

ConsulConfig for ConsulClient

type ConsulWatcher

type ConsulWatcher struct {
	Config *WatchConfig
	// contains filtered or unexported fields
}

ConsulWatcher implements consul watch handler and stores the users handler function

func (*ConsulWatcher) StartWatch

func (cwh *ConsulWatcher) StartWatch() error

StartWatch will start the watch

func (*ConsulWatcher) StopWatch

func (cwh *ConsulWatcher) StopWatch()

StopWatch will cleanup the active consul watches

type HandlerFunc

type HandlerFunc func(context.Context, []Pair)

HandlerFunc provides interface for the watch handler func which is implemented by watch subscriber

type IRegistry

type IRegistry interface {
	// Register a node with the Registry with a given name
	// Returns a Registration id or error
	Register(ctx context.Context, name string, ttl time.Duration) (string, error)

	// Deregister a node which was registered with a id
	// Returns error on failure
	Deregister(ctx context.Context, id string) error

	// IsRegistered checks is node with registration_id is registred with registry
	IsRegistered(ctx context.Context, id string) bool

	// Renew a registration using registration_id
	Renew(ctx context.Context, id string) error

	// RenewPeriodic renews a registration id periodically based on TTL
	RenewPeriodic(ctx context.Context, id string, ttl time.Duration, doneCh <-chan struct{}) error

	// Acquire a lock for a registration_id on a given key and value pair
	Acquire(ctx context.Context, id string, key string, value []byte) (bool, error)

	// Release a lock for a registration_id on a given key and value pair
	Release(ctx context.Context, id string, key string, value string) bool

	// Watch on a key/keyprefix in registry
	Watch(ctx context.Context, wh *WatchConfig) (IWatcher, error)

	// Put a key value pair
	Put(ctx context.Context, key string, value []byte) (string, error)

	// Get returns a value for a key
	Get(ctx context.Context, key string) (*Pair, error)

	// List returns a keys with matching key prefix
	ListKeys(ctx context.Context, prefix string) ([]string, error)

	// List returns a slice of pairs with matching key prefix
	List(ctx context.Context, prefix string) ([]Pair, error)

	// Exists checks the existence of a key
	Exists(ctx context.Context, key string) (bool, error)

	// DeleteTree deletes all keys under a prefix
	DeleteTree(ctx context.Context, key string) error

	// IsAlive checks the health of the registry
	IsAlive(context.Context) (bool, error)
}

IRegistry implements a generic interface for service discovery

func NewConsulClient

func NewConsulClient(config *ConsulConfig) (IRegistry, error)

NewConsulClient creates a new consul client

func NewRegistry

func NewRegistry(config *Config) (IRegistry, error)

NewRegistry initializes the registry instance based on Config

type IWatcher

type IWatcher interface {
	// StartWatch interface implemented by registry watcher to start a watch
	StartWatch() error

	// StopWatch interface implemented by registry watcher to stop a watch
	// it should also release the resources held
	StopWatch()
}

IWatcher defines the watch interface for watch over registry

func NewConsulWatcher

func NewConsulWatcher(ctx context.Context, watchConfig *WatchConfig, plan *watch.Plan, client *api.Client) IWatcher

NewConsulWatcher is used to create a new struct of type WatchHandler

type Pair

type Pair struct {
	Key   string
	Value []byte
	// Version - The version of the stored key-value pair
	// Set by the registry, ModifyIndex in case of consul
	Version   string
	SessionID string
}

Pair is the registry struct returned to watch handler

func (*Pair) String

func (pair *Pair) String() string

type WatchConfig

type WatchConfig struct {
	WatchType string
	WatchPath string
	Handler   HandlerFunc
}

WatchConfig struct provides watch details on registry

Jump to

Keyboard shortcuts

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