aerospike/policy/client_policy.rs
1// Copyright 2015-2018 Aerospike, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16use std::time::Duration;
17
18use crate::commands::admin_command::AdminCommand;
19use crate::errors::Result;
20
21/// `ClientPolicy` encapsulates parameters for client policy command.
22#[derive(Debug, Clone)]
23pub struct ClientPolicy {
24 /// User authentication to cluster. Leave empty for clusters running without restricted access.
25 pub user_password: Option<(String, String)>,
26
27 /// Initial host connection timeout in milliseconds. The timeout when opening a connection
28 /// to the server host for the first time.
29 pub timeout: Option<Duration>,
30
31 /// Connection idle timeout. Every time a connection is used, its idle
32 /// deadline will be extended by this duration. When this deadline is reached,
33 /// the connection will be closed and discarded from the connection pool.
34 pub idle_timeout: Option<Duration>,
35
36 /// Maximum number of synchronous connections allowed per server node.
37 pub max_conns_per_node: usize,
38
39 /// Number of connection pools used for each node. Machines with 8 CPU cores or less usually
40 /// need only one connection pool per node. Machines with larger number of CPU cores may have
41 /// their performance limited by contention for pooled connections. Contention for pooled
42 /// connections can be reduced by creating multiple mini connection pools per node.
43 pub conn_pools_per_node: usize,
44
45 /// Throw exception if host connection fails during addHost().
46 pub fail_if_not_connected: bool,
47
48 /// Threshold at which the buffer attached to the connection will be shrunk by deallocating
49 /// memory instead of just resetting the size of the underlying vec.
50 /// Should be set to a value that covers as large a percentile of payload sizes as possible,
51 /// while also being small enough not to occupy a significant amount of memory for the life
52 /// of the connection pool.
53 pub buffer_reclaim_threshold: usize,
54
55 /// TendInterval determines interval for checking for cluster state changes.
56 /// Minimum possible interval is 10 Milliseconds.
57 pub tend_interval: Duration,
58
59 /// A IP translation table is used in cases where different clients
60 /// use different server IP addresses. This may be necessary when
61 /// using clients from both inside and outside a local area
62 /// network. Default is no translation.
63 /// The key is the IP address returned from friend info requests to other servers.
64 /// The value is the real IP address used to connect to the server.
65 pub ip_map: Option<HashMap<String, String>>,
66
67 /// UseServicesAlternate determines if the client should use "services-alternate"
68 /// instead of "services" in info request during cluster tending.
69 /// "services-alternate" returns server configured external IP addresses that client
70 /// uses to talk to nodes. "services-alternate" can be used in place of
71 /// providing a client "ipMap".
72 /// This feature is recommended instead of using the client-side IpMap above.
73 ///
74 /// "services-alternate" is available with Aerospike Server versions >= 3.7.1.
75 pub use_services_alternate: bool,
76
77 /// Size of the thread pool used in scan and query commands. These commands are often sent to
78 /// multiple server nodes in parallel threads. A thread pool improves performance because
79 /// threads do not have to be created/destroyed for each command.
80 pub thread_pool_size: usize,
81
82 /// Expected cluster name. It not `None`, server nodes must return this cluster name in order
83 /// to join the client's view of the cluster. Should only be set when connecting to servers
84 /// that support the "cluster-name" info command.
85 pub cluster_name: Option<String>,
86}
87
88impl Default for ClientPolicy {
89 fn default() -> ClientPolicy {
90 ClientPolicy {
91 user_password: None,
92 timeout: Some(Duration::new(30, 0)),
93 idle_timeout: Some(Duration::new(5, 0)),
94 max_conns_per_node: 256,
95 conn_pools_per_node: 1,
96 fail_if_not_connected: true,
97 tend_interval: Duration::new(1, 0),
98 ip_map: None,
99 use_services_alternate: false,
100 thread_pool_size: 128,
101 cluster_name: None,
102 buffer_reclaim_threshold: 65536,
103 }
104 }
105}
106
107impl ClientPolicy {
108 /// Set username and password to use when authenticating to the cluster.
109 pub fn set_user_password(&mut self, username: String, password: String) -> Result<()> {
110 let password = AdminCommand::hash_password(&password)?;
111 self.user_password = Some((username, password));
112 Ok(())
113 }
114}