blob: becc102fac5383c042ae190be5f2ab6e771b682e [file] [log] [blame]
jsobele02e4242007-05-08 17:51:49 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
7#ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
8#define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1
9
10#include <vector>
11#include "TSocket.h"
12
T Jake Lucianib5e62212009-01-31 22:36:20 +000013namespace apache { namespace thrift { namespace transport {
jsobele02e4242007-05-08 17:51:49 +000014
David Reiss6d0cccd2008-02-28 21:20:12 +000015 /**
16 * Class to hold server information for TSocketPool
17 *
David Reiss6d0cccd2008-02-28 21:20:12 +000018 */
19class TSocketPoolServer {
20
21 public:
22 /**
23 * Default constructor for server info
24 */
25 TSocketPoolServer();
26
27 /**
28 * Constructor for TSocketPool server
29 */
30 TSocketPoolServer(const std::string &host, int port);
31
32 // Host name
33 std::string host_;
34
35 // Port to connect on
36 int port_;
37
David Reiss1997f102008-04-29 00:29:41 +000038 // Socket for the server
39 int socket_;
40
David Reiss6d0cccd2008-02-28 21:20:12 +000041 // Last time connecting to this server failed
42 int lastFailTime_;
43
44 // Number of consecutive times connecting to this server failed
45 int consecutiveFailures_;
46};
47
jsobele02e4242007-05-08 17:51:49 +000048/**
49 * TCP Socket implementation of the TTransport interface.
50 *
jsobele02e4242007-05-08 17:51:49 +000051 */
52class TSocketPool : public TSocket {
53
54 public:
David Reiss8f3bce42008-03-18 18:21:52 +000055
56 /**
57 * Socket pool constructor
58 */
59 TSocketPool();
60
jsobele02e4242007-05-08 17:51:49 +000061 /**
62 * Socket pool constructor
63 *
64 * @param hosts list of host names
65 * @param ports list of port names
66 */
67 TSocketPool(const std::vector<std::string> &hosts,
68 const std::vector<int> &ports);
69
70 /**
71 * Socket pool constructor
72 *
73 * @param servers list of pairs of host name and port
74 */
David Reiss907ad762008-03-02 00:25:58 +000075 TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
76
77 /**
78 * Socket pool constructor
79 *
80 * @param servers list of TSocketPoolServers
81 */
David Reiss8f3bce42008-03-18 18:21:52 +000082 TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
jsobele02e4242007-05-08 17:51:49 +000083
84 /**
dweatherfordd1372822007-10-09 22:57:23 +000085 * Socket pool constructor
86 *
87 * @param host single host
88 * @param port single port
89 */
90 TSocketPool(const std::string& host, int port);
91
92 /**
jsobele02e4242007-05-08 17:51:49 +000093 * Destroyes the socket object, closing it if necessary.
94 */
95 virtual ~TSocketPool();
96
97 /**
dweatherfordd1372822007-10-09 22:57:23 +000098 * Add a server to the pool
99 */
100 void addServer(const std::string& host, int port);
101
David Reiss8f3bce42008-03-18 18:21:52 +0000102 /**
103 * Set list of servers in this pool
104 */
105 void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000106
107 /**
108 * Get list of servers in this pool
109 */
David Reiss8f3bce42008-03-18 18:21:52 +0000110 void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000111
dweatherfordd1372822007-10-09 22:57:23 +0000112 /**
jsobele02e4242007-05-08 17:51:49 +0000113 * Sets how many times to keep retrying a host in the connect function.
114 */
115 void setNumRetries(int numRetries);
116
117 /**
118 * Sets how long to wait until retrying a host if it was marked down
119 */
120 void setRetryInterval(int retryInterval);
121
122 /**
123 * Sets how many times to keep retrying a host before marking it as down.
124 */
125 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
126
127 /**
128 * Turns randomization in connect order on or off.
129 */
130 void setRandomize(bool randomize);
131
132 /**
133 * Whether to always try the last server.
134 */
135 void setAlwaysTryLast(bool alwaysTryLast);
136
137 /**
138 * Creates and opens the UNIX socket.
139 */
140 void open();
141
David Reiss1997f102008-04-29 00:29:41 +0000142 /*
143 * Closes the UNIX socket
144 */
145 void close();
146
jsobele02e4242007-05-08 17:51:49 +0000147 protected:
148
David Reiss1997f102008-04-29 00:29:41 +0000149 void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server);
150
jsobele02e4242007-05-08 17:51:49 +0000151 /** List of servers to connect to */
David Reiss8f3bce42008-03-18 18:21:52 +0000152 std::vector< boost::shared_ptr<TSocketPoolServer> > servers_;
jsobele02e4242007-05-08 17:51:49 +0000153
David Reiss1997f102008-04-29 00:29:41 +0000154 /** Current server */
155 boost::shared_ptr<TSocketPoolServer> currentServer_;
156
jsobele02e4242007-05-08 17:51:49 +0000157 /** How many times to retry each host in connect */
158 int numRetries_;
159
160 /** Retry interval in seconds, how long to not try a host if it has been
161 * marked as down.
162 */
163 int retryInterval_;
164
165 /** Max consecutive failures before marking a host down. */
166 int maxConsecutiveFailures_;
167
168 /** Try hosts in order? or Randomized? */
169 bool randomize_;
170
171 /** Always try last host, even if marked down? */
172 bool alwaysTryLast_;
173};
174
T Jake Lucianib5e62212009-01-31 22:36:20 +0000175}}} // apache::thrift::transport
jsobele02e4242007-05-08 17:51:49 +0000176
177#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
178