ncoclient

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientConfig

type ClientConfig struct {
	// Connector is object that can open DB connections
	Connector db.DBConnector
	// SeedList is list of OMNIbus cluster nodes
	SeedList []db.Addr
	// MaxPoolSize - max connections that client can open.
	// If MaxPoolSize <= 0, pool with default size will be created
	MaxPoolSize int
	// UseRandomFailOver enables connmanager.WithRandomFailOver pool option.
	// Useful for Display level of OMNIBus cluster
	UseRandomFailOver bool
	// UseFailBack enables connmanager.WithFailBack pool option, overrides UseRandomFailOver.
	// Useful for Aggregation level of OMNIBus cluster
	UseFailBack bool
	// FailBackDelay is time after that client will try to reconnect to the first node in SeedList.
	// Takes effect if UseRandomFailOver is true
	FailBackDelay time.Duration
	// Logger sets logger for client and underlying pool. By default no-op logger is used
	Logger *app.Logger
}

type NcoClient

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

NcoClient implements querycoordinator.Client interface to work with coordinator. On the other hand, NcoClient interacts with the connmanager.Pool to acquire connections and execute queries

func NewNcoClient

func NewNcoClient(name string, config ClientConfig) (client *NcoClient, err error)

NewNcoClient returns ready to use instance of client. Calls connmanager.NewPool to create underlying connmanager.Pool

Example
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	conf := nc.ClientConfig{
		Connector: &DemoConnector{},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	client, err := nc.NewNcoClient("AGG1", conf)

	fmt.Printf("%T, %v", client, err)
}
Output:

*ncoclient.NcoClient, <nil>
Example (Empty_name_fail)
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	conf := nc.ClientConfig{
		Connector: &DemoConnector{},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	_, err := nc.NewNcoClient("", conf)

	fmt.Println(err)
}
Output:

ERR_VALIDATION: invalid client config, empty name

func (*NcoClient) Close

func (c *NcoClient) Close() error

Close stops underlying Pool to prevent acquire connections, so client becomes 'close' too - you cannot use it to run queries anymore

Example
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	demoConn := &DemoConnection{
		Data: models.RowSet{
			Columns: []string{"col1", "col2"},
			Rows:    [][]any{{"data1", 3}, {"data2", 5}},
		},
	}

	conf := nc.ClientConfig{
		Connector: &DemoConnector{demoConn},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	client, _ := nc.NewNcoClient("AGG1", conf)

	ctx := context.Background()
	query := models.Query{SQL: "select 1"}
	credentials := models.Credentials{UserName: "someuser", Password: "superpass"}

	errClose := client.Close()
	result := client.Exec(ctx, query, credentials)
	name := client.Name()

	fmt.Println(errClose)
	fmt.Println(result)
	fmt.Println(name)
}
Output:

<nil>
{{[] []} 0 ERR_UNKNOWN: pool is closed already}
AGG1

func (*NcoClient) Exec

func (c *NcoClient) Exec(ctx context.Context, query models.Query, credentials models.Credentials) models.QueryResult

Exec runs query and return result or exit on context cancellation

Example
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	demoConn := &DemoConnection{
		Data: models.RowSet{
			Columns: []string{"col1", "col2"},
			Rows:    [][]any{{"data1", 3}, {"data2", 5}},
		},
	}

	conf := nc.ClientConfig{
		Connector: &DemoConnector{demoConn},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	client, _ := nc.NewNcoClient("AGG1", conf)

	ctx := context.Background()
	query := models.Query{SQL: "select 1"}
	credentials := models.Credentials{UserName: "someuser", Password: "superpass"}

	result := client.Exec(ctx, query, credentials)

	fmt.Println(result)
}
Output:

{{[col1 col2] [[data1 3] [data2 5]]} 0 <nil>}
Example (Cancel)
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	demoConn := &DemoConnection{
		Data: models.RowSet{
			Columns: []string{"col1", "col2"},
			Rows:    [][]any{{"data1", 3}, {"data2", 5}},
		},
	}
	conf := nc.ClientConfig{
		Connector: &DemoConnector{demoConn},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	client, _ := nc.NewNcoClient("AGG1", conf)

	ctx, cancel := context.WithCancel(context.Background())
	query := models.Query{SQL: "select 1"}
	credentials := models.Credentials{UserName: "someuser", Password: "superpass"}

	cancel()
	result := client.Exec(ctx, query, credentials)

	fmt.Println(result)
}
Output:

{{[] []} 0 context canceled}

func (*NcoClient) Name

func (c *NcoClient) Name() string

Name returns name of instance

Example
package main

import (
	"context"
	"fmt"
	"time"

	db "github.com/ncotds/nco-qoordinator/internal/dbconnector"
	nc "github.com/ncotds/nco-qoordinator/internal/ncoclient"
	"github.com/ncotds/nco-qoordinator/pkg/models"
)

// DemoConnector implements dbconnector.DBConnector interface for examples only, use your own implementation
type DemoConnector struct {
	Conn *DemoConnection
}

func (dc *DemoConnector) Connect(
	ctx context.Context,
	addr db.Addr,
	credentials models.Credentials,
) (conn db.ExecutorCloser, err error) {

	<-time.After(10 * time.Millisecond)
	return dc.Conn, err
}

// DemoConnection implements dbconnector.ExecutorCloser interface for examples only, use your own implementation
type DemoConnection struct {
	Data     models.RowSet
	Affected int
	Err      error
}

func (c *DemoConnection) Exec(_ context.Context, _ models.Query) (models.RowSet, int, error) {
	<-time.After(10 * time.Millisecond)
	return c.Data, c.Affected, c.Err
}

func (c *DemoConnection) Close() error {
	return nil
}

func main() {
	conf := nc.ClientConfig{
		Connector: &DemoConnector{},
		SeedList:  []db.Addr{"host1", "host2"},
	}
	client, _ := nc.NewNcoClient("AGG1", conf)

	name := client.Name()

	fmt.Println(name)
}
Output:

AGG1

Jump to

Keyboard shortcuts

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