Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AlternatorNodes ¶
type AlternatorNodes struct {
// contains filtered or unexported fields
}
AlternatorNodes holds the configuration for the load balanced alternator nodes as well as some locks for the load balancing thread.
Example ¶
// To run this example, just run: // go test example_test.go package main import ( "context" "crypto/tls" "crypto/x509" "fmt" "github.com/aws/aws-sdk-go-v2/service/dynamodb" alternatorlb "github.com/scylladb/alternator-load-balancing/go/v2" "net/http" "time" ) var customPEMCertificate = []byte(nil) func main() { ctx := context.Background() // Uncomment to use Amazon DynamoDB configured in ~/.aws/ // cfg, _ := config.LoadDefaultConfig(ctx) // db := dynamodb.NewFromConfig(cfg) // Use the local Alternator with our silly testing alternator/secret_pass // authentication - and the new load balancing code. alternatorNodes := alternatorlb.NewAlternatorNodes("http", 8000, "127.0.0.1") // To add custom CA certificate on top of system CA certificates: if customPEMCertificate != nil { systemPool, err := x509.SystemCertPool() if err != nil { panic(err) } if !systemPool.AppendCertsFromPEM(customPEMCertificate) { panic("failed to append custom certificate") } alternatorNodes.SetHTTPClient(&http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ RootCAs: systemPool, }, }, }) } alternatorNodes.Start(ctx, 1*time.Second) defer alternatorNodes.Stop() cfg := alternatorNodes.Config("dog.scylladb.com", "alternator", "secret_pass") db := dynamodb.NewFromConfig(cfg) for i := 1; i < 20; i++ { time.Sleep(300 * time.Millisecond) // Do the simplest possible request - DescribeEndpoints result, err := db.DescribeEndpoints(ctx, &dynamodb.DescribeEndpointsInput{}) if err != nil { fmt.Println(err.Error()) } else { fmt.Println("response:", *result.Endpoints[0].Address) } } }
Output:
func NewAlternatorNodes ¶
func NewAlternatorNodes(scheme string, port int, initialNodes ...string) *AlternatorNodes
NewAlternatorNodes creates a new, unstarted instance of the alternator nodes loadbalancing.
func (*AlternatorNodes) Config ¶
Config produces a conf for the AWS SDK that will integrate the alternator loadbalancing with the AWS SDK.
func (*AlternatorNodes) SetHTTPClient ¶
func (n *AlternatorNodes) SetHTTPClient(client httpClient)
SetHTTPClient changes underlying http client used for fetching the list of alternator nodes.
func (*AlternatorNodes) Start ¶
func (n *AlternatorNodes) Start(ctx context.Context, fetchInterval time.Duration)
Start will start the loadbalancing thread that keep available alternator instances in sync and selectes the next instance for load balancing.
func (*AlternatorNodes) Stop ¶
func (n *AlternatorNodes) Stop()
Stop will stop the loadbalancing thread and should be called once you are done with the AWS SDK session.