forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerPool.h
179 lines (173 loc) · 4.63 KB
/
ServerPool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#ifndef _PREDIXY_SERVER_POOL_H_
#define _PREDIXY_SERVER_POOL_H_
#include <string>
#include <map>
#include "Predixy.h"
#include <vector>
class ServerPool
{
public:
enum Type
{
Unknown,
Cluster,
Standalone
};
static const int DefaultServerRetryTimeout = 10000000;
static const int DefaultRefreshInterval = 1000000;
public:
virtual ~ServerPool();
void init(const ServerPoolConf& conf);
Proxy* proxy() const
{
return mProxy;
}
bool refresh();
int type() const
{
return mType;
}
const String& password() const
{
return mPassword;
}
int masterReadPriority() const
{
return mMasterReadPriority;
}
int staticSlaveReadPriority() const
{
return mStaticSlaveReadPriority;
}
int dynamicSlaveReadPriority() const
{
return mDynamicSlaveReadPriority;
}
long refreshInterval() const
{
return mRefreshInterval;
}
long serverTimeout() const
{
return mServerTimeout;
}
int serverFailureLimit() const
{
return mServerFailureLimit;
}
long serverRetryTimeout() const
{
return mServerRetryTimeout;
}
int keepalive() const
{
return mKeepAlive;
}
int dbNum() const
{
return mDbNum;
}
ServerGroup* getGroup(int idx)
{
return idx < (int)mGroupPool.size() ? mGroupPool[idx] : nullptr;
}
Server* getServer(const String& addr)
{
UniqueLock<Mutex> lck(mMtx);
auto it = mServs.find(addr);
return it == mServs.end() ? nullptr : it->second;
}
Server* getServer(Handler* h, Request* req, const String& key) const
{
return mGetServerFunc(this, h, req, key);
}
Server* iter(int& cursor) const
{
return mIterFunc(this, cursor);
}
void refreshRequest(Handler* h)
{
mRefreshRequestFunc(this, h);
}
void handleResponse(Handler* h, ConnectConnection* s, Request* req, Response* res);
protected:
typedef Server* (*GetServerFunc)(const ServerPool* p, Handler* h, Request* req, const String& key);
typedef Server* (*IterFunc)(const ServerPool* p, int& cursor);
typedef void (*RefreshRequestFunc)(ServerPool* p, Handler* h);
typedef void (*HandleResponseFunc)(ServerPool* p, Handler* h, ConnectConnection* s, Request* req, Response* res);
template<class T>
ServerPool(Proxy* p, int type, T* sub = nullptr):
mProxy(p),
mType(type),
mGetServerFunc(T::getServer),
mIterFunc(T::iter),
mRefreshRequestFunc(T::refreshRequest),
mHandleResponseFunc(T::handleResponse),
mLastRefreshTime(0),
mMasterReadPriority(50),
mStaticSlaveReadPriority(0),
mDynamicSlaveReadPriority(0),
mRefreshInterval(DefaultRefreshInterval),
mServerFailureLimit(10),
mServerRetryTimeout(DefaultServerRetryTimeout),
mDbNum(1)
{
}
static Server* randServer(Handler* h, const std::vector<Server*>& servs);
static Server* iter(const std::vector<Server*>& servs, int& cursor);
protected:
std::map<String, Server*> mServs;
std::vector<ServerGroup*> mGroupPool;
private:
Proxy* mProxy;
int mType;
GetServerFunc mGetServerFunc;
IterFunc mIterFunc;
RefreshRequestFunc mRefreshRequestFunc;
HandleResponseFunc mHandleResponseFunc;
AtomicLong mLastRefreshTime;
Mutex mMtx;
String mPassword;
int mMasterReadPriority;
int mStaticSlaveReadPriority;
int mDynamicSlaveReadPriority;
long mRefreshInterval;
long mServerTimeout;
int mServerFailureLimit;
long mServerRetryTimeout;
int mKeepAlive;
int mDbNum;
};
template<class T>
class ServerPoolTmpl : public ServerPool
{
public:
ServerPoolTmpl(Proxy* p, int type):
ServerPool(p, type, (ServerPoolTmpl<T>*)this)
{
}
private:
static Server* getServer(const ServerPool* p, Handler* h, Request* req, const String& key)
{
return static_cast<const T*>(p)->getServer(h, req, key);
}
static Server* iter(const ServerPool* p, int& cursor)
{
return static_cast<const T*>(p)->iter(cursor);
}
static void refreshRequest(ServerPool* p, Handler* h)
{
static_cast<T*>(p)->refreshRequest(h);
}
static void handleResponse(ServerPool* p, Handler* h, ConnectConnection* s, Request* req, Response* res)
{
static_cast<T*>(p)->handleResponse(h, s, req, res);
}
friend class ServerPool;
};
#endif