pool

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: 11 Imported by: 0

README

pool

Package pool implements a pool of connections for Tarantool 3.0 in Go language according to the design document.

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

Documentation

Overview

Package pool implements a pool of connections for Tarantool 3.0.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingDialerFactory is returned if a nil dialer factory is passed to
	// the Pool constructor.
	ErrMissingDialerFactory = fmt.Errorf("dialer factory argument is missing")
	// ErrMissingBalancer is returned if a nil balancer is passed to
	// the Pool constructor.
	ErrMissingBalancer = fmt.Errorf("balancer argument is missing")
	// ErrUnsubscribed is returned if the pool does not have an active
	// subscription to a Subscriber.
	ErrUnsubscribed = fmt.Errorf("pool is not subscribed yet")
	// ErrNoConnectedInstances is returned if there are no instances for the
	// mode currently in the pool.
	ErrNoConnectedInstances = fmt.Errorf("there are no connected instances")
)

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	// Add adds an instance to balancer. Returns false if an instance was not added.
	Add(instance discovery.Instance) error
	// Remove removes an instance from the balancer by name.
	Remove(instanceName string)
	// Next return a next instance according to the balancer logic.
	Next(mode discovery.Mode) (string, bool)
}

Balancer is an interface for pool balancer implementations.

type DoerAdapter

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

DoerAdapter adapts a ModeDoer to a tarantool.Doer interface. The adapter helps to use the Pool object as tarantool.Doer.

Example

ExampleModeAdapter demonstrates how to adapt a pool.Pool object to the tarantool.Doer interface and send request with specific mode to the pool.

package main

import (
	"fmt"
	"time"

	"github.com/tarantool/go-tarantool/v2"

	"github.com/tarantool/go-discovery"
	"github.com/tarantool/go-discovery/dial"
	"github.com/tarantool/go-discovery/pool"
)

func main() {
	balancer := pool.NewRoundRobinBalancer()
	dialerFactory := dial.NewNetDialerFactory("user", "pass", tarantool.Opts{
		Timeout: 5 * time.Second,
	})
	examplePool, err := pool.NewPool(dialerFactory, balancer)
	if err != nil {
		fmt.Println("Failed to create a pool: %w", err)
	}

	doer := tarantool.Doer(pool.NewDoerAdapter(examplePool, discovery.ModeRW))

	_, err = doer.Do(tarantool.NewPingRequest()).Get()
	// The pool is not subscribed to any configuration source yet so it will
	// return an error, see examples in the main package.
	fmt.Println(err, ".")

}
Output:
pool is not subscribed yet .

func NewDoerAdapter

func NewDoerAdapter(doer ModeDoer, mode discovery.Mode) *DoerAdapter

NewDoerAdapter creates a new DoerAdapter object for the specified mode.

func (*DoerAdapter) Do

func (d *DoerAdapter) Do(request tarantool.Request) *tarantool.Future

Do executes the request with the adapter's mode.

type ModeDoer

type ModeDoer interface {
	// Do executes the request on an instance with the specified mode.
	Do(tarantool.Request, discovery.Mode) *tarantool.Future
}

ModeDoer is an interface to execute a request on an instance with the specified mode.

type Pool

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

Pool is a connection pool. It implements discovery.Observer interface to be able to subscribe to instance configurations updates.

func NewPool

func NewPool(factory discovery.DialerFactory, balancer Balancer) (*Pool, error)

NewPool creates a Pool object. It is required to subscribe the created pool to any discovery.Subscriber to start receiving new instances configurations and create actual connections to instances.

func (*Pool) Do

func (p *Pool) Do(request tarantool.Request, mode discovery.Mode) *tarantool.Future

Do executes the request on instances with the specified mode. You could use ModeAdapter type to adapt it to tarantool.Doer interface.

func (*Pool) Observe

func (p *Pool) Observe(events []discovery.Event, err error)

Observe observes instances configurations update events. The subscription is supported to only a single discovery.Subscriber at the moment.

A concurrent subscription to >1 discovery.Subscriber will lead to an invalid events processing and undefined behavior:

subscriber.Subscribe(pool)
otherSubscriber.Subscribe(pool)

At the same time it supports re-subscription, so the code:

subscriber.Subscribe(pool)
subscriber.Unsubscribe(pool)
otherSubscriber.Subscribe(pool)
otherSubscriber.Unsubscribe(pool)

is valid.

type PriorityBalancer

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

PriorityBalancer implements priority-based round-robin logic for getting an instance.

Example

ExamplePriorityBalancer demonstrates how to create a Pool object with priority balancer. The balancer sends requests to instances with a higher priority in round-robin until it exists.

package main

import (
	"fmt"
	"time"

	"github.com/tarantool/go-tarantool/v2"

	"github.com/tarantool/go-discovery"
	"github.com/tarantool/go-discovery/dial"
	"github.com/tarantool/go-discovery/pool"
)

func main() {
	balancer := pool.NewPriorityBalancer(func(instance discovery.Instance) int {
		if instance.Replicaset == "datacenter-1" {
			// The balancer will send requests to replicaset "datacenter-1"
			// while there are any connected instance with the mode.
			return 20
		}
		if instance.Group == "Moscow" {
			// After that it will choose instances with Group == "Moscow".
			return 10
		}
		// It will send requests to other instances if instances with
		// Group == "Moscow" and Replicaset == "datacenter-1"
		// are not available for a mode.
		return 0
	})
	dialerFactory := dial.NewNetDialerFactory("user", "pass", tarantool.Opts{
		Timeout: 5 * time.Second,
	})

	examplePool, err := pool.NewPool(dialerFactory, balancer)
	if err != nil {
		fmt.Println("Failed to create a pool: %w", err)
	}

	_, err = examplePool.Do(tarantool.NewPingRequest(), discovery.ModeAny).Get()
	// The pool is not subscribed to any configuration source yet so it will
	// return an error, see examples in the main package.
	fmt.Println(err, ".")

}
Output:
pool is not subscribed yet .

func NewPriorityBalancer

func NewPriorityBalancer(priorityFunc func(discovery.Instance) int) *PriorityBalancer

NewPriorityBalancer creates new priority balancer.

func (*PriorityBalancer) Add

func (b *PriorityBalancer) Add(instance discovery.Instance) error

Add adds a new instance name to the balancer.

func (*PriorityBalancer) Next

func (b *PriorityBalancer) Next(mode discovery.Mode) (string, bool)

Next returns a next highest priority instance of the specified mode.

func (*PriorityBalancer) Remove

func (b *PriorityBalancer) Remove(instNameToRemove string)

Remove removes instance name from the balancer.

type RoundRobinBalancer

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

RoundRobinBalancer implements round-robin logic for getting an instance.

Example

ExampleRoundRobinBalancer demonstrates how to create a Pool object with round-robin balancer. It sends requests to instances with round-robin strategy.

package main

import (
	"fmt"
	"time"

	"github.com/tarantool/go-tarantool/v2"

	"github.com/tarantool/go-discovery"
	"github.com/tarantool/go-discovery/dial"
	"github.com/tarantool/go-discovery/pool"
)

func main() {
	balancer := pool.NewRoundRobinBalancer()
	dialerFactory := dial.NewNetDialerFactory("user", "pass", tarantool.Opts{
		Timeout: 5 * time.Second,
	})
	examplePool, err := pool.NewPool(dialerFactory, balancer)
	if err != nil {
		fmt.Println("Failed to create a pool: %w", err)
	}

	_, err = examplePool.Do(tarantool.NewPingRequest(), discovery.ModeAny).Get()
	// The pool is not subscribed to any configuration source yet so it will
	// return an error, see examples in the main package.
	fmt.Println(err, ".")

}
Output:
pool is not subscribed yet .

func NewRoundRobinBalancer

func NewRoundRobinBalancer() *RoundRobinBalancer

NewRoundRobinBalancer creates new round-robin balancer.

func (*RoundRobinBalancer) Add

func (b *RoundRobinBalancer) Add(instance discovery.Instance) error

Add adds new instance name to the balancer.

func (*RoundRobinBalancer) Next

func (b *RoundRobinBalancer) Next(mode discovery.Mode) (string, bool)

Next returns next instance.

func (*RoundRobinBalancer) Remove

func (b *RoundRobinBalancer) Remove(instNameToRemove string)

Remove removes instance name from the balancer.

Directories

Path Synopsis
internal
container/buffer
Package buffer implements buffers for a pool container.
Package buffer implements buffers for a pool container.

Jump to

Keyboard shortcuts

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