Skip to content

Commit 9907b87

Browse files
authored
Add examples for load balanced connection creation in V2 (#538)
1 parent c97369d commit 9907b87

File tree

6 files changed

+115
-8
lines changed

6 files changed

+115
-8
lines changed

v2/connection/endpoints_maglev_hash.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import (
3333
// RequestHashValueExtractor accepts request method and full request path and must return a value which will be used for hash calculation
3434
type RequestHashValueExtractor func(requestMethod, requestPath string) (string, error)
3535

36-
// NewMaglevHashEndpoints returns Endpoint manager which runs round-robin
36+
// NewMaglevHashEndpoints returns Endpoint manager which consistently returns the same endpoint based on value
37+
// extracted from request using provided RequestHashValueExtractor
38+
// e.g. if you want to use DB name from URL for hashing you can use RequestDBNameValueExtractor
3739
func NewMaglevHashEndpoints(eps []string, extractor RequestHashValueExtractor) (Endpoint, error) {
3840
// order of endpoints affects hashing result
3941
sort.Strings(eps)

v2/examples/example_client_async_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ import (
3030
"github.com/arangodb/go-driver/v2/connection"
3131
)
3232

33+
// ExampleNewConnectionAsyncWrapper shows how to create a connection wrapper for async requests
34+
// It lets use async requests on demand
3335
func ExampleNewConnectionAsyncWrapper() {
3436
// Create an HTTP connection to the database
35-
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig())
37+
endpoint := connection.NewRoundRobinEndpoints([]string{"http://localhost:8529"})
38+
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig(endpoint))
3639

3740
// Create ASYNC wrapper for the connection
3841
conn = connection.NewConnectionAsyncWrapper(conn)

v2/examples/example_client_test.go renamed to v2/examples/example_client_basic_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import (
3232
"github.com/arangodb/go-driver/v2/connection"
3333
)
3434

35+
// ExampleNewClient shows how to create the simple client with a single endpoint
3536
func ExampleNewClient() {
3637
// Create an HTTP connection to the database
37-
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig())
38+
endpoint := connection.NewRoundRobinEndpoints([]string{"http://localhost:8529"})
39+
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig(endpoint))
3840

3941
// Create a client
4042
client := arangodb.NewClient(conn)
@@ -48,9 +50,9 @@ func ExampleNewClient() {
4850
}
4951
}
5052

51-
func exampleJSONHTTPConnectionConfig() connection.HttpConfiguration {
53+
func exampleJSONHTTPConnectionConfig(endpoint connection.Endpoint) connection.HttpConfiguration {
5254
return connection.HttpConfiguration{
53-
Endpoint: connection.NewEndpoints("http://localhost:8529"),
55+
Endpoint: endpoint,
5456
ContentType: connection.ApplicationJSON,
5557
Transport: &http.Transport{
5658
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package examples
22+
23+
import (
24+
"context"
25+
"fmt"
26+
27+
"github.com/arangodb/go-driver/v2/arangodb"
28+
"github.com/arangodb/go-driver/v2/connection"
29+
)
30+
31+
// ExampleNewMaglevHashEndpoints shows how to create a new MaglevHashEndpoints
32+
// It lets use different endpoints for different databases by using a RequestDBNameValueExtractor function.
33+
// E.g.:
34+
// - all requests to _db/<db-name-1> will use endpoint 1
35+
// - all requests to _db/<db-name-2> will use endpoint 2
36+
func ExampleNewMaglevHashEndpoints() {
37+
// Create an HTTP connection to the database
38+
endpoints, err := connection.NewMaglevHashEndpoints(
39+
[]string{"https://a:8529", "https://a:8539", "https://b:8529"},
40+
connection.RequestDBNameValueExtractor,
41+
)
42+
43+
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig(endpoints))
44+
45+
// Create a client
46+
client := arangodb.NewClient(conn)
47+
48+
// Ask the version of the server
49+
versionInfo, err := client.Version(context.Background())
50+
if err != nil {
51+
fmt.Printf("Failed to get version info: %v", err)
52+
} else {
53+
fmt.Printf("Database has version '%s' and license '%s'\n", versionInfo.Version, versionInfo.License)
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package examples
22+
23+
import (
24+
"context"
25+
"fmt"
26+
27+
"github.com/arangodb/go-driver/v2/arangodb"
28+
"github.com/arangodb/go-driver/v2/connection"
29+
)
30+
31+
// ExampleNewRoundRobinEndpoints shows how to create a client with round-robin endpoint list
32+
func ExampleNewRoundRobinEndpoints() {
33+
// Create an HTTP connection to the database
34+
endpoints := connection.NewRoundRobinEndpoints([]string{"https://a:8529", "https://a:8539", "https://b:8529"})
35+
conn := connection.NewHttpConnection(exampleJSONHTTPConnectionConfig(endpoints))
36+
37+
// Create a client
38+
client := arangodb.NewClient(conn)
39+
40+
// Ask the version of the server
41+
versionInfo, err := client.Version(context.Background())
42+
if err != nil {
43+
fmt.Printf("Failed to get version info: %v", err)
44+
} else {
45+
fmt.Printf("Database has version '%s' and license '%s'\n", versionInfo.Version, versionInfo.License)
46+
}
47+
}

v2/tests/run_wrap_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func waitForConnection(t testing.TB, client arangodb.Client) arangodb.Client {
232232

233233
// pick the first one endpoint which is always the leader in AF mode
234234
// also for Cluster mode we only need one endpoint to avoid the problem with the data propagation in tests
235-
endpoint := connection.NewEndpoints(connection.FixupEndpointURLScheme(cer.Endpoints[nextEndpoint].Endpoint))
235+
endpoint := connection.NewRoundRobinEndpoints([]string{connection.FixupEndpointURLScheme(cer.Endpoints[nextEndpoint].Endpoint)})
236236
err = client.Connection().SetEndpoint(endpoint)
237237
if err != nil {
238238
log.Warn().Err(err).Msgf("Unable to set endpoints")
@@ -282,7 +282,6 @@ func connectionJsonHttp(t testing.TB) connection.Connection {
282282
DialContext: (&net.Dialer{
283283
Timeout: 30 * time.Second,
284284
KeepAlive: 90 * time.Second,
285-
DualStack: true,
286285
}).DialContext,
287286
MaxIdleConns: 100,
288287
IdleConnTimeout: 90 * time.Second,
@@ -308,7 +307,6 @@ func connectionVPACKHttp(t testing.TB) connection.Connection {
308307
DialContext: (&net.Dialer{
309308
Timeout: 30 * time.Second,
310309
KeepAlive: 90 * time.Second,
311-
DualStack: true,
312310
}).DialContext,
313311
MaxIdleConns: 100,
314312
IdleConnTimeout: 90 * time.Second,

0 commit comments

Comments
 (0)