FreeTDS API: src/pool/pool.h Source File

FreeTDS API

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 _pool_h_
21 #define _pool_h_
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
43 /* defines */
44 #define PGSIZ 2048
45 #define BLOCKSIZ 512
46 #define MAX_POOL_USERS 1024
47
48 /* enums and typedefs */
49 typedef enum
50 {
51 TDS_SRV_WAIT, /* if no members are free wait */
52 TDS_SRV_QUERY,
53 } TDS_USER_STATE;
54
55 /* forward declaration */
56 typedef struct tds_pool_event TDS_POOL_EVENT;
57 typedef struct tds_pool_socket TDS_POOL_SOCKET;
58 typedef struct tds_pool_member TDS_POOL_MEMBER;
59 typedef struct tds_pool_user TDS_POOL_USER;
60 typedef struct tds_pool TDS_POOL;
61 typedef void (*TDS_POOL_EXECUTE)(TDS_POOL_EVENT *event);
62
63 struct tds_pool_event
64 {
65 TDS_POOL_EVENT *next;
66 TDS_POOL_EXECUTE execute;
67 };
68
69 struct tds_pool_socket
70 {
71 TDSSOCKET *tds;
72 bool poll_recv;
73 bool poll_send;
74 };
75
76 struct tds_pool_user
77 {
78 TDS_POOL_SOCKET sock;
79 DLIST_FIELDS(dlist_user_item);
80 TDSLOGIN *login;
81 TDS_USER_STATE user_state;
82 TDS_POOL_MEMBER *assigned_member;
83 };
84
85 struct tds_pool_member
86 {
87 TDS_POOL_SOCKET sock;
88 DLIST_FIELDS(dlist_member_item);
89 bool doing_async;
90 time_t last_used_tm;
91 TDS_POOL_USER *current_user;
92 };
93
94 #define DLIST_PREFIX dlist_member
95 #define DLIST_LIST_TYPE dlist_members
96 #define DLIST_ITEM_TYPE TDS_POOL_MEMBER
97 #include <freetds/utils/dlist.tmpl.h>
98
99 #define DLIST_PREFIX dlist_user
100 #define DLIST_LIST_TYPE dlist_users
101 #define DLIST_ITEM_TYPE TDS_POOL_USER
102 #include <freetds/utils/dlist.tmpl.h>
103
104 struct tds_pool
105 {
106 char *name;
107 char *user;
108 char *password;
109 char *server;
110 char *database;
111 char *server_user;
112 char *server_password;
113 int port;
114 int max_member_age; /* in seconds */
115 int min_open_conn;
116 int max_open_conn;
117 tds_mutex events_mtx;
118 TDS_SYS_SOCKET listen_fd;
119 TDS_SYS_SOCKET wakeup_fd;
120 TDS_SYS_SOCKET event_fd;
121 TDS_POOL_EVENT *events;
122
123 int num_active_members;
124 dlist_members active_members;
125 dlist_members idle_members;
126
128 dlist_users waiters;
129 int num_users;
130 dlist_users users;
131 TDSCONTEXT *ctx;
132
133 unsigned long user_logins;
134 unsigned long member_logins;
135 };
136
137 /* prototypes */
138
139 /* member.c */
140 void pool_process_members(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
141 TDS_POOL_MEMBER *pool_assign_idle_member(TDS_POOL * pool, TDS_POOL_USER *user);
142 void pool_mbr_init(TDS_POOL * pool);
143 void pool_mbr_destroy(TDS_POOL * pool);
144 void pool_free_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
145 void pool_assign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr, TDS_POOL_USER *puser);
146 void pool_deassign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
147 void pool_reset_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
148 bool pool_packet_read(TDSSOCKET * tds);
149
150 /* user.c */
151 void pool_process_users(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
152 void pool_user_init(TDS_POOL * pool);
153 void pool_user_destroy(TDS_POOL * pool);
154 TDS_POOL_USER *pool_user_create(TDS_POOL * pool, TDS_SYS_SOCKET s);
155 void pool_free_user(TDS_POOL * pool, TDS_POOL_USER * puser);
156 void pool_user_query(TDS_POOL * pool, TDS_POOL_USER * puser);
157 bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser);
158 void pool_user_finish_login(TDS_POOL * pool, TDS_POOL_USER * puser);
159
160 /* util.c */
161 void dump_login(TDSLOGIN * login);
162 void pool_event_add(TDS_POOL *pool, TDS_POOL_EVENT *ev, TDS_POOL_EXECUTE execute);
163 int pool_write(TDS_SYS_SOCKET sock, const void *buf, size_t len);
164 bool pool_write_data(TDS_POOL_SOCKET *from, TDS_POOL_SOCKET *to);
165
166 /* config.c */
167 bool pool_read_conf_files(const char *path, const char *poolname, TDS_POOL * pool, char **err);
168
169
170 #endif
tds_socket
Information for a server connection.
Definition: tds.h:1141
tds_login
Definition: tds.h:511
tds_pool_user
Definition: pool.h:76
tds_pool
Definition: pool.h:104
tds_context
Definition: tds.h:1029
tds_pool_socket
Definition: pool.h:69
tds_pool::waiters
dlist_users waiters
users in wait state
Definition: pool.h:128
tds_pool_member
Definition: pool.h:85
tds_pool_event
Definition: pool.h:63
tds.h
Main include file for libtds.