Loading...
Searching...
No Matches
as_conn_pool.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2020 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <aerospike/as_queue.h>
20#include <aerospike/as_socket.h>
21#include <pthread.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/******************************************************************************
28 * TYPES
29 *****************************************************************************/
30
31/**
32 * @private
33 * Sync connection pool.
34 */
35typedef struct as_conn_pool_s {
36 /**
37 * Mutex lock.
38 */
39 pthread_mutex_t lock;
40
41 /**
42 * Queue.
43 */
45
46 /**
47 * Minimum number of connections.
48 */
49 uint32_t min_size;
51
52/******************************************************************************
53 * FUNCTIONS
54 *****************************************************************************/
55
56/**
57 * @private
58 * Initialize a connection pool.
59 */
60static inline void
61as_conn_pool_init(as_conn_pool* pool, uint32_t item_size, uint32_t min_size, uint32_t max_size)
62{
63 pthread_mutex_init(&pool->lock, NULL);
64 as_queue_init(&pool->queue, item_size, max_size);
65 pool->min_size = min_size;
66}
67
68/**
69 * @private
70 * Pop connection from head of pool.
71 */
72static inline bool
74{
75 pthread_mutex_lock(&pool->lock);
76 bool status = as_queue_pop(&pool->queue, sock);
77 pthread_mutex_unlock(&pool->lock);
78 return status;
79}
80
81/**
82 * @private
83 * Pop connection from tail of pool.
84 */
85static inline bool
87{
88 pthread_mutex_lock(&pool->lock);
89 bool status = as_queue_pop_tail(&pool->queue, sock);
90 pthread_mutex_unlock(&pool->lock);
91 return status;
92}
93
94/**
95 * @private
96 * Push connection to head of pool if size < capacity.
97 */
98static inline bool
100{
101 pthread_mutex_lock(&pool->lock);
102 bool status = as_queue_push_head_limit(&pool->queue, sock);
103 pthread_mutex_unlock(&pool->lock);
104 return status;
105}
106
107/**
108 * @private
109 * Push connection to tail of pool if size < capacity.
110 */
111static inline bool
113{
114 pthread_mutex_lock(&pool->lock);
115 bool status = as_queue_push_limit(&pool->queue, sock);
116 pthread_mutex_unlock(&pool->lock);
117 return status;
118}
119
120/**
121 * @private
122 * Increment connection total.
123 * Return true if connection total is within limits.
124 */
125static inline bool
127{
128 return as_faa_uint32(&pool->queue.total, 1) < pool->queue.capacity;
129}
130
131/**
132 * @private
133 * Decrement connection total.
134 */
135static inline void
140
141/**
142 * @private
143 * Return number of connections that might be closed.
144 */
145static inline int
147{
148 return as_load_uint32(&pool->queue.total) - pool->min_size;
149}
150
151/**
152 * @private
153 * Destroy a connection pool.
154 */
155static inline void
157{
158 as_socket sock;
159
160 pthread_mutex_lock(&pool->lock);
161
162 while (as_queue_pop(&pool->queue, &sock)) {
163 as_socket_close(&sock);
164 }
165
166 as_queue_destroy(&pool->queue);
167 pthread_mutex_unlock(&pool->lock);
168 pthread_mutex_destroy(&pool->lock);
169}
170
171#ifdef __cplusplus
172} // end extern "C"
173#endif
#define as_faa_uint32(_target, _value)
#define as_decr_uint32(_target)
#define as_load_uint32(_target)
static bool as_conn_pool_incr(as_conn_pool *pool)
static void as_conn_pool_destroy(as_conn_pool *pool)
static bool as_conn_pool_pop_head(as_conn_pool *pool, as_socket *sock)
static int as_conn_pool_excess(as_conn_pool *pool)
static bool as_conn_pool_push_head(as_conn_pool *pool, as_socket *sock)
static void as_conn_pool_init(as_conn_pool *pool, uint32_t item_size, uint32_t min_size, uint32_t max_size)
static void as_conn_pool_decr(as_conn_pool *pool)
static bool as_conn_pool_push_tail(as_conn_pool *pool, as_socket *sock)
static bool as_conn_pool_pop_tail(as_conn_pool *pool, as_socket *sock)
static bool as_queue_pop(as_queue *queue, void *ptr)
Definition as_queue.h:165
AS_EXTERN bool as_queue_push_limit(as_queue *queue, const void *ptr)
AS_EXTERN void as_queue_destroy(as_queue *queue)
static bool as_queue_pop_tail(as_queue *queue, void *ptr)
Definition as_queue.h:185
AS_EXTERN bool as_queue_init(as_queue *queue, uint32_t item_size, uint32_t capacity)
AS_EXTERN bool as_queue_push_head_limit(as_queue *queue, const void *ptr)
void as_socket_close(as_socket *sock)
pthread_mutex_t lock
as_queue queue
uint32_t min_size
uint32_t capacity
Definition as_queue.h:43
uint32_t total
Definition as_queue.h:63