File: leader.go

package info (click to toggle)
golang-github-cowsql-go-cowsql 1.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 716 kB
  • sloc: sh: 373; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package client

import (
	"context"

	"github.com/cowsql/go-cowsql/internal/protocol"
)

// FindLeader returns a Client connected to the current cluster leader.
//
// The function will iterate through to all nodes in the given store, and for
// each of them check if it's the current leader. If no leader is found, the
// function will keep retrying (with a capped exponential backoff) until the
// given context is canceled.
func FindLeader(ctx context.Context, store NodeStore, options ...Option) (*Client, error) {
	o := defaultOptions()

	for _, option := range options {
		option(o)
	}

	config := protocol.Config{
		Dial: o.DialFunc,
	}
	connector := protocol.NewConnector(0, store, config, o.LogFunc)
	protocol, err := connector.Connect(ctx)
	if err != nil {
		return nil, err
	}

	client := &Client{protocol: protocol}

	return client, nil
}