FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 
27 #if HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 
31 /*
32  * POSIX says fd_set type may be defined in either sys/select.h or sys/time.h.
33  */
34 #if HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
37 
38 #include <freetds/tds.h>
39 #include <freetds/dlist.h>
40 
41 /* defines */
42 #define PGSIZ 2048
43 #define BLOCKSIZ 512
44 #define MAX_POOL_USERS 1024
45 
46 /* enums and typedefs */
47 typedef enum
48 {
49  TDS_SRV_WAIT, /* if no members are free wait */
50  TDS_SRV_QUERY,
51 } TDS_USER_STATE;
52 
53 /* forward declaration */
54 typedef struct tds_pool_event TDS_POOL_EVENT;
55 typedef struct tds_pool_socket TDS_POOL_SOCKET;
56 typedef struct tds_pool_member TDS_POOL_MEMBER;
57 typedef struct tds_pool_user TDS_POOL_USER;
58 typedef struct tds_pool TDS_POOL;
59 typedef void (*TDS_POOL_EXECUTE)(TDS_POOL_EVENT *event);
60 
62 {
63  TDS_POOL_EVENT *next;
64  TDS_POOL_EXECUTE execute;
65 };
66 
68 {
69  TDSSOCKET *tds;
70  bool poll_recv;
71  bool poll_send;
72 };
73 
75 {
76  TDS_POOL_SOCKET sock;
77  TDSLOGIN *login;
78  TDS_USER_STATE user_state;
79  TDS_POOL_MEMBER *assigned_member;
80 };
81 
83 {
84  TDS_POOL_SOCKET sock;
85  DLIST_FIELDS(TDS_POOL_MEMBER);
86  bool doing_async;
87  time_t last_used_tm;
88  TDS_POOL_USER *current_user;
89 };
90 
91 #define DLIST_FUNC(suffix) dlist_member_ ## suffix
92 #define DLIST_LIST_TYPE dlist_member
93 #define DLIST_TYPE TDS_POOL_MEMBER
94 #include <freetds/dlist.tmpl.h>
95 
96 struct tds_pool
97 {
98  char *name;
99  char *user;
100  char *password;
101  char *server;
102  char *database;
103  int port;
104  int max_member_age; /* in seconds */
105  int min_open_conn;
106  int max_open_conn;
107  tds_mutex events_mtx;
108  TDS_SYS_SOCKET event_fd;
109  TDS_POOL_EVENT *events;
110 
111  int num_active_members;
112  dlist_member active_members;
113 
115  int waiters;
116  int max_users;
117  TDS_POOL_USER *users;
118  TDSCONTEXT *ctx;
119 };
120 
121 /* prototypes */
122 
123 /* member.c */
124 void pool_process_members(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
125 TDS_POOL_MEMBER *pool_assign_idle_member(TDS_POOL * pool, TDS_POOL_USER *user);
126 void pool_mbr_init(TDS_POOL * pool);
127 void pool_mbr_destroy(TDS_POOL * pool);
128 void pool_free_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
129 void pool_assign_member(TDS_POOL_MEMBER * pmbr, TDS_POOL_USER *puser);
130 void pool_deassign_member(TDS_POOL_MEMBER * pmbr);
131 void pool_reset_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
132 bool pool_packet_read(TDSSOCKET * tds);
133 
134 /* user.c */
135 void pool_process_users(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
136 void pool_user_init(TDS_POOL * pool);
137 void pool_user_destroy(TDS_POOL * pool);
138 TDS_POOL_USER *pool_user_create(TDS_POOL * pool, TDS_SYS_SOCKET s);
139 void pool_free_user(TDS_POOL * pool, TDS_POOL_USER * puser);
140 void pool_user_query(TDS_POOL * pool, TDS_POOL_USER * puser);
141 bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser);
142 void pool_user_finish_login(TDS_POOL * pool, TDS_POOL_USER * puser);
143 
144 /* util.c */
145 void dump_login(TDSLOGIN * login);
146 void die_if(int expr, const char *msg);
147 void pool_event_add(TDS_POOL *pool, TDS_POOL_EVENT *ev, TDS_POOL_EXECUTE execute);
148 bool pool_write_data(TDS_POOL_SOCKET *from, TDS_POOL_SOCKET *to);
149 
150 /* config.c */
151 int pool_read_conf_file(const char *poolname, TDS_POOL * pool);
152 
153 
154 #endif
Definition: tds.h:1050
Definition: pool.h:82
Definition: tds.h:539
Information for a server connection.
Definition: tds.h:1155
Definition: pool.h:61
Main include file for libtds.
Definition: pool.h:74
Definition: pool.h:96
Definition: pool.h:67
int waiters
number of users in wait state
Definition: pool.h:115