FreeTDS API

Loading...
Searching...
No Matches
pool.h
1/* TDSPool - Connection pooling for TDS based databases
2 * Copyright (C) 2001 Brian Bruns
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20#ifndef _tdsguard_clBmMDiJ1W6vO4Q4ftyzgV_
21#define _tdsguard_clBmMDiJ1W6vO4Q4ftyzgV_
22
23#include <assert.h>
24
25#if HAVE_SYS_TYPES_H
26#include <sys/types.h>
27#endif
28
29#if HAVE_NETINET_IN_H
30#include <netinet/in.h>
31#endif
32
33/*
34 * POSIX says fd_set type may be defined in either sys/select.h or sys/time.h.
35 */
36#if HAVE_SYS_TIME_H
37#include <sys/time.h>
38#endif
39
40#include <freetds/tds.h>
41#include <freetds/utils/dlist.h>
42#include <freetds/replacements.h>
43
44/* defines */
45#define PGSIZ 2048
46#define BLOCKSIZ 512
47#define MAX_POOL_USERS 1024
48
49/* enums and typedefs */
50 typedef enum
51{
52 TDS_SRV_WAIT, /* if no members are free wait */
53 TDS_SRV_QUERY,
54} TDS_USER_STATE;
55
56/* forward declaration */
57 typedef struct tds_pool_event TDS_POOL_EVENT;
58 typedef struct tds_pool_socket TDS_POOL_SOCKET;
59 typedef struct tds_pool_member TDS_POOL_MEMBER;
60 typedef struct tds_pool_user TDS_POOL_USER;
61 typedef struct tds_pool TDS_POOL;
62 typedef void (*TDS_POOL_EXECUTE)(TDS_POOL_EVENT *event);
63
64 struct tds_pool_event
65{
66 TDS_POOL_EVENT *next;
67 TDS_POOL_EXECUTE execute;
68};
69
70 struct tds_pool_socket
71{
72 TDSSOCKET *tds;
73 uint32_t poll_index;
74 bool poll_recv;
75 bool poll_send;
76};
77
78 struct tds_pool_user
79{
80 TDS_POOL_SOCKET sock;
81 DLIST_FIELDS(dlist_user_item);
82 TDSLOGIN *login;
83 TDS_USER_STATE user_state;
84 TDS_POOL_MEMBER *assigned_member;
85};
86
87 struct tds_pool_member
88{
89 TDS_POOL_SOCKET sock;
90 DLIST_FIELDS(dlist_member_item);
91 bool doing_async;
92 time_t last_used_tm;
93 TDS_POOL_USER *current_user;
94};
95
96#define DLIST_PREFIX dlist_member
97#define DLIST_LIST_TYPE dlist_members
98#define DLIST_ITEM_TYPE TDS_POOL_MEMBER
99#include <freetds/utils/dlist.tmpl.h>
100
101#define DLIST_PREFIX dlist_user
102#define DLIST_LIST_TYPE dlist_users
103#define DLIST_ITEM_TYPE TDS_POOL_USER
104#include <freetds/utils/dlist.tmpl.h>
105
106 struct tds_pool
107{
108 char *name;
109 char *user;
110 char *password;
111 char *server;
112 char *database;
113 char *server_user;
114 char *server_password;
115 int port;
116 int max_member_age; /* in seconds */
117 int min_open_conn;
118 int max_open_conn;
119 tds_mutex events_mtx;
120 TDS_SYS_SOCKET listen_fd;
121 TDS_SYS_SOCKET wakeup_fd;
122 TDS_SYS_SOCKET event_fd;
123 TDS_POOL_EVENT *events;
124
125 int num_active_members;
126 dlist_members active_members;
127 dlist_members idle_members;
128
130 dlist_users waiters;
131 int num_users;
132 dlist_users users;
133 TDSCONTEXT *ctx;
134
135 unsigned long user_logins;
136 unsigned long member_logins;
137};
138
139/* prototypes */
140
141/* member.c */
142 int pool_process_members(TDS_POOL * pool, struct pollfd *fds, unsigned num_fds);
143 TDS_POOL_MEMBER *pool_assign_idle_member(TDS_POOL * pool, TDS_POOL_USER *user);
144 void pool_mbr_init(TDS_POOL * pool);
145 void pool_mbr_destroy(TDS_POOL * pool);
146 void pool_free_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
147 void pool_assign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr, TDS_POOL_USER *puser);
148 void pool_deassign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
149 void pool_reset_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
150 bool pool_packet_read(TDSSOCKET * tds);
151#if ENABLE_EXTRA_CHECKS
152 void pool_mbr_check(TDS_POOL *pool);
153#else
154 static inline void pool_mbr_check(TDS_POOL *pool TDS_UNUSED)
155{
156}
157#endif
158
159
160/* user.c */
161 void pool_process_users(TDS_POOL * pool, struct pollfd *fds, unsigned num_fds);
162 void pool_user_init(TDS_POOL * pool);
163 void pool_user_destroy(TDS_POOL * pool);
164 TDS_POOL_USER *pool_user_create(TDS_POOL * pool, TDS_SYS_SOCKET s);
165 void pool_free_user(TDS_POOL * pool, TDS_POOL_USER * puser);
166 void pool_user_query(TDS_POOL * pool, TDS_POOL_USER * puser);
167 bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser);
168 void pool_user_finish_login(TDS_POOL * pool, TDS_POOL_USER * puser);
169
170/* util.c */
171 void dump_login(TDSLOGIN * login);
172 void pool_event_add(TDS_POOL *pool, TDS_POOL_EVENT *ev, TDS_POOL_EXECUTE execute);
173 int pool_write(TDS_SYS_SOCKET sock, const void *buf, size_t len);
174 bool pool_write_data(TDS_POOL_SOCKET *from, TDS_POOL_SOCKET *to);
175
176/* config.c */
177 bool pool_read_conf_files(const tds_dir_char *path, const char *poolname, TDS_POOL * pool, char **err);
178
179
180#endif
tds.h
Main include file for libtds.
tds_pool_event
Definition pool.h:65
tds_pool_socket
Definition pool.h:71
tds_pool_user
Definition pool.h:79
tds_pool_member
Definition pool.h:88
tds_pool
Definition pool.h:107
tds_pool::waiters
dlist_users waiters
users in wait state
Definition pool.h:130
pollfd
Definition poll.h:27
tds_login
Definition tds.h:498
tds_context
Definition tds.h:1015
tds_socket
Information for a server connection.
Definition tds.h:1163