From 005b93498130b86cc691c38154864c9dc1f5048c Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Wed, 23 Aug 2023 18:35:54 +0200 Subject: [PATCH] [CLIENT-2457] Add CreateClientWithPolicyAndHost --- aerospike_suite_test.go | 2 +- client_builder.go | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 client_builder.go diff --git a/aerospike_suite_test.go b/aerospike_suite_test.go index 88349121..a62356c3 100644 --- a/aerospike_suite_test.go +++ b/aerospike_suite_test.go @@ -117,7 +117,7 @@ func initTestVars() { log.Println("Connecting to seeds:", dbHosts) if *proxy { - client, err = as.NewProxyClientWithPolicyAndHost(clientPolicy, dbHosts[0]) + client, err = as.CreateClientWithPolicyAndHost(as.CTProxy, clientPolicy, dbHosts[0]) if err != nil { log.Fatal(err.Error()) } diff --git a/client_builder.go b/client_builder.go new file mode 100644 index 00000000..af19fc0c --- /dev/null +++ b/client_builder.go @@ -0,0 +1,48 @@ +// Copyright 2014-2022 Aerospike, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package aerospike + +import "github.com/aerospike/aerospike-client-go/v6/types" + +// ClientType determines the type of client to build. +type ClientType int + +const ( + // CTNative means: Create a native client. + CTNative ClientType = iota + + // CTProxy means: Create a proxy client. + CTProxy +) + +// CreateClientWithPolicyAndHost generates a new Client of the specified type +// with the specified ClientPolicy and sets up the cluster using the provided hosts. +// If the policy is nil, the default relevant policy will be used. +func CreateClientWithPolicyAndHost(typ ClientType, policy *ClientPolicy, hosts ...*Host) (ClientIfc, Error) { + if len(hosts) == 0 { + return nil, newError(types.SERVER_NOT_AVAILABLE, "No hosts were provided") + } + + switch typ { + case CTNative: + return NewClientWithPolicyAndHost(policy, hosts...) + case CTProxy: + if len(hosts) > 1 { + return nil, newError(types.GRPC_ERROR, "Only one proxy host is acceptable") + } + return NewProxyClientWithPolicyAndHost(policy, hosts[0]) + } + return nil, newError(types.SERVER_NOT_AVAILABLE, "Invalid client type") +}