discoverer

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: BSD-2-Clause Imports: 15 Imported by: 0

README

discoverer

Package discoverer contains a base set of discoverers to retrieve a list of instance configurations from a configuration source.

See the main repository README.md for build and documentation details.

Documentation

Overview

Package discoverer implements a discoverer for Tarantool 3.0.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypedStorageNil is returned when typed storage is nil.
	ErrTypedStorageNil = errors.New("typed storage cannot be nil")
	// ErrRangeDataFailed is returned when range operation on storage fails.
	ErrRangeDataFailed = errors.New("failed to range data from storage")
	// ErrValidateDataFailed is returned when data validation fails.
	ErrValidateDataFailed = errors.New("failed to validate data")
	// ErrParseConfigFailed is returned when configuration parsing fails.
	ErrParseConfigFailed = errors.New("failed to parse configuration")
)
View Source
var ErrMissingDiscoverer = fmt.Errorf("an inner discoverer is missing")

ErrMissingDiscoverer is an error that tells that the provided inner discoverer is nil.

View Source
var ErrMissingEtcd = fmt.Errorf("etcd object is missing")

ErrMissingEtcd is an error that tells that the provided etcd object is nil.

View Source
var ErrMissingFactory = fmt.Errorf("a dialer factory is missing")

ErrMissingFactory is an error that tells that the provided DialerFactory is nil.

View Source
var ErrMissingTarantool = errors.New("no Tarantool connector was applied")

ErrMissingTarantool for case of wrong initialization.

Functions

This section is empty.

Types

type Cache added in v1.1.0

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

Cache wraps a Discoverer and caches its result so that subsequent calls to Discovery return the cached result immediately. This implementation is not thread-safe.

func NewCache added in v1.1.0

func NewCache(d discovery.Discoverer) *Cache

NewCache creates a new Cache that wraps the provided discoverer.

func (*Cache) Discovery added in v1.1.0

func (c *Cache) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery returns a list of instance configurations for this environment or its cached result if available.

Note: This method is not thread-safe. If called concurrently multiple times, it may call the underlying discoverer more than once.

type Connectable

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

Connectable gets a list of nodes from the inner discoverer and returns only those that are available for connection.

func NewConnectable

func NewConnectable(factory discovery.DialerFactory,
	discoverer discovery.Discoverer) *Connectable

NewConnectable creates a Connectable with a given dialer factory and an inner discoverer.

func (*Connectable) Discovery

func (d *Connectable) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery calls d.discoverer.Discovery() and returns a list of nodes that are available for connection.

type Etcd

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

Etcd discovers a list of instance configurations from etcd.

func NewEtcd

func NewEtcd(etcd clientv3.KV, prefix string) *Etcd

NewEtcd creates a new etcd discoverer to retrieve a list of instance configurations from etcd.

The prefix must have the same value as config.etcd.prefix.

func (*Etcd) Discovery

func (d *Etcd) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery retrieves a list of instance configurations from etcd.

Example
package main

import (
	"context"
	"fmt"
	"io"
	"log"

	clientv3 "go.etcd.io/etcd/client/v3"
	"go.etcd.io/etcd/tests/v3/integration"

	"github.com/tarantool/go-discovery/discoverer"
)

func init() {
	log.SetOutput(io.Discard)
}

func main() {
	cluster := integration.NewLazyCluster()
	defer cluster.Terminate()

	etcd, err := clientv3.New(clientv3.Config{
		Endpoints: cluster.EndpointsGRPC(),
	})
	if err != nil {
		fmt.Println("Unable to start etcd client:", err)
		return
	}
	defer func() { _ = etcd.Close() }()

	etcddiscoverer := discoverer.NewEtcd(etcd, "foo")
	instances, err := etcddiscoverer.Discovery(context.Background())

	fmt.Println("Without keys in the prefix:")
	fmt.Println("Instances:", instances)
	fmt.Println("Error:", err)

	_, err = etcd.Put(context.Background(), "foo/config/key", `
database:
  mode: ro
groups:
  foo:
    replicasets:
      bar:
        instances:
          zoo:
            iproto:
              advertise:
                client: localhost:3011
                peer:
                  params:
                    transport: ssl
            roles: [crud]
            labels:
              tags: "any,bar,3"
`)
	if err != nil {
		fmt.Println("Unable to put configuration into etcd:", err)
		return
	}

	instances, err = etcddiscoverer.Discovery(context.Background())

	fmt.Println("After publishing:")
	fmt.Println("Instances")
	for _, instance := range instances {
		fmt.Println("Group:", instance.Group)
		fmt.Println("Replicaset:", instance.Replicaset)
		fmt.Println("Name:", instance.Name)
		fmt.Println("Mode:", instance.Mode.String())
		fmt.Println("URI:", instance.URI)
		fmt.Println("Roles:", instance.Roles)
		fmt.Println("Labels:", instance.Labels)
	}
	fmt.Println("Error:", err)
	fmt.Println("Done.")

}
Output:
Without keys in the prefix:
Instances: []
Error: <nil>
After publishing:
Instances
Group: foo
Replicaset: bar
Name: zoo
Mode: ro
URI: [localhost:3011]
Roles: [crud]
Labels: map[tags:any,bar,3]
Error: <nil>
Done.
Example (Cancelled)
package main

import (
	"context"
	"fmt"
	"io"
	"log"

	clientv3 "go.etcd.io/etcd/client/v3"
	"go.etcd.io/etcd/tests/v3/integration"

	"github.com/tarantool/go-discovery/discoverer"
)

func init() {
	log.SetOutput(io.Discard)
}

func main() {
	cluster := integration.NewLazyCluster()
	defer cluster.Terminate()

	etcd, err := clientv3.New(clientv3.Config{
		Endpoints: cluster.EndpointsGRPC(),
	})
	if err != nil {
		fmt.Println("Unable to start etcd client:", err)
		return
	}
	defer func() { _ = etcd.Close() }()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	etcddiscoverer := discoverer.NewEtcd(etcd, "foo")
	instances, err := etcddiscoverer.Discovery(ctx)

	fmt.Println(instances)
	fmt.Println(err)
	fmt.Println("Done.")

}
Output:
[]
context canceled
Done.

type Filter

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

Filter filters the list of discovered nodes by some set of filters. It serves as a wrapper to another Discoverer.

func NewFilter

func NewFilter(discoverer discovery.Discoverer,
	filters ...discovery.Filter) *Filter

NewFilter creates a new Filter discoverer. It wraps the inner discoverer to filter its results according to passed filters.

func (*Filter) Discovery

func (d *Filter) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery calls Discovery of an inner discoverer and returns a filtered list of nodes.

Note that the context is only used in the inner discoverer.Discovery call. If the program is stuck on one of the Filter calls, context cancel would not cancel the method.

type Storage added in v1.1.0

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

Storage discovers a list of instance configurations from a storage.

func NewStorageDiscoverer added in v1.1.0

func NewStorageDiscoverer(s storage.Storage, prefix string) *Storage

NewStorageDiscoverer creates a new storage discoverer to retrieve a list of instance configurations from a storage.

func (*Storage) Discovery added in v1.1.0

func (d *Storage) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery retrieves instance configurations from the storage.

type Tarantool

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

Tarantool discovers a list of instance configurations from TcS.

func NewTarantool

func NewTarantool(conn tarantool.Doer, prefix string) *Tarantool

NewTarantool create decorator with Discovery method.

func (*Tarantool) Discovery

func (t *Tarantool) Discovery(ctx context.Context) ([]discovery.Instance, error)

Discovery retrieves a list of instance configurations from the Storage.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"log"

	"github.com/tarantool/go-discovery/discoverer"
	tcshelper "github.com/tarantool/go-tarantool/v2/test_helpers/tcs"
)

func main() {
	tcs, err := tcshelper.Start(0)
	if err != nil {
		if errors.Is(err, tcshelper.ErrNotSupported) {
			fmt.Println("TcS is not supported:", err)
			return
		}
		log.Fatalf("Failed to start TcS: %s", err)
	}
	discoverer := discoverer.NewTarantool(tcs.Doer(), "/foo")
	instances, err := discoverer.Discovery(context.Background())

	fmt.Println("Without keys in the prefix:")
	fmt.Println("Instances:", instances)
	fmt.Println("Error:", err)

	err = tcs.Put(context.Background(), "/foo/config/key", `
database:
  mode: ro
groups:
  foo:
    replicasets:
      bar:
        instances:
          zoo:
            iproto:
              advertise:
                client: localhost:3011
                peer:
                  params:
                    transport: ssl
            roles: [crud]
            labels:
              tags: "any,bar,3"
`)
	if err != nil {
		fmt.Println("Unable to put configuration into etcd:", err)
		return
	}

	instances, err = discoverer.Discovery(context.Background())

	fmt.Println("After publishing:")
	fmt.Println("Instances")
	for _, instance := range instances {
		fmt.Println("Group:", instance.Group)
		fmt.Println("Replicaset:", instance.Replicaset)
		fmt.Println("Name:", instance.Name)
		fmt.Println("Mode:", instance.Mode.String())
		fmt.Println("URI:", instance.URI)
		fmt.Println("Roles:", instance.Roles)
		fmt.Println("Labels:", instance.Labels)
	}
	fmt.Println("Error:", err)
	fmt.Println("Done.")
}

Jump to

Keyboard shortcuts

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