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

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