Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
session_pool_options.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
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 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SESSION_POOL_OPTIONS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SESSION_POOL_OPTIONS_H
17 
19 #include <algorithm>
20 #include <chrono>
21 #include <map>
22 #include <string>
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
27 inline namespace SPANNER_CLIENT_NS {
28 
29 // What action to take if the session pool is exhausted.
31 
49  public:
59  min_sessions_ = (std::max)(min_sessions_, 0);
60  max_sessions_per_channel_ = (std::max)(max_sessions_per_channel_, 1);
61  min_sessions_ =
62  (std::min)(min_sessions_, max_sessions_per_channel_ * num_channels);
63  max_idle_sessions_ = (std::max)(max_idle_sessions_, 0);
64  return *this;
65  }
66 
74  min_sessions_ = count;
75  return *this;
76  }
77 
79  int min_sessions() const { return min_sessions_; }
80 
86  max_sessions_per_channel_ = count;
87  return *this;
88  }
89 
91  int max_sessions_per_channel() const { return max_sessions_per_channel_; }
92 
98  max_idle_sessions_ = count;
99  return *this;
100  }
101 
103  int max_idle_sessions() const { return max_idle_sessions_; }
104 
107  action_on_exhaustion_ = action;
108  return *this;
109  }
110 
116  return action_on_exhaustion_;
117  }
118 
119  /*
120  * Set the interval at which we refresh sessions so they don't get
121  * collected by the backend GC. The GC collects objects older than 60
122  * minutes, so any duration below that (less some slack to allow the calls
123  * to be made to refresh the sessions) should suffice.
124  */
125  SessionPoolOptions& set_keep_alive_interval(std::chrono::seconds interval) {
126  keep_alive_interval_ = interval;
127  return *this;
128  }
129 
131  std::chrono::seconds keep_alive_interval() const {
132  return keep_alive_interval_;
133  }
134 
141  SessionPoolOptions& set_labels(std::map<std::string, std::string> labels) {
142  labels_ = std::move(labels);
143  return *this;
144  }
145 
147  std::map<std::string, std::string> const& labels() const { return labels_; }
148 
149  private:
150  int min_sessions_ = 0;
151  int max_sessions_per_channel_ = 100;
152  int max_idle_sessions_ = 0;
153  ActionOnExhaustion action_on_exhaustion_ = ActionOnExhaustion::kBlock;
154  std::chrono::seconds keep_alive_interval_ = std::chrono::minutes(55);
155  std::map<std::string, std::string> labels_;
156 };
157 
158 } // namespace SPANNER_CLIENT_NS
159 } // namespace spanner
160 } // namespace cloud
161 } // namespace google
162 
163 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SESSION_POOL_OPTIONS_H
SessionPoolOptions & set_max_idle_sessions(int count)
Set the maximum number of sessions to keep in the pool in an idle state.
SessionPoolOptions & set_labels(std::map< std::string, std::string > labels)
Set the labels used when creating sessions within the pool.
int min_sessions() const
Return the minimum number of sessions to keep in the pool.
SessionPoolOptions & set_keep_alive_interval(std::chrono::seconds interval)
ActionOnExhaustion action_on_exhaustion() const
Return the action to take (kBlock or kFail) when attempting to allocate a session when the pool is ex...
SessionPoolOptions & set_min_sessions(int count)
Set the minimum number of sessions to keep in the pool.
Controls the session pool maintained by a spanner::Client.
std::map< std::string, std::string > const & labels() const
Return the labels used when creating sessions within the pool.
int max_sessions_per_channel() const
Return the minimum number of sessions to keep in the pool.
SessionPoolOptions & EnforceConstraints(int num_channels)
Enforce the stated constraints on the option values, altering them if necessary.
Contains all the Cloud Spanner C++ client types and functions.
int max_idle_sessions() const
Return the maximum number of idle sessions to keep in the pool.
SessionPoolOptions & set_max_sessions_per_channel(int count)
Set the maximum number of sessions to create on each channel.
#define SPANNER_CLIENT_NS
Definition: version.h:22
SessionPoolOptions & set_action_on_exhaustion(ActionOnExhaustion action)
Set whether to block or fail on pool exhaustion.
std::chrono::seconds keep_alive_interval() const
Return the interval at which we refresh sessions to prevent GC.