Skip to content

Commit

Permalink
[CLIENT-2457] Add CreateClientWithPolicyAndHost
Browse files Browse the repository at this point in the history
  • Loading branch information
khaf committed Aug 23, 2023
1 parent d7e10d3 commit 005b934
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aerospike_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
48 changes: 48 additions & 0 deletions client_builder.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 005b934

Please sign in to comment.