blob: b72dfb2f878864da47fa821b9d1f7c751ab76127 [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 *
18 * @author Akhil Wable <akhil@facebook.com>
19 */
20class TSocketPoolServer {
21
22 public:
23 /**
24 * Default constructor for server info
25 */
26 TSocketPoolServer();
27
28 /**
29 * Constructor for TSocketPool server
30 */
31 TSocketPoolServer(const std::string &host, int port);
32
33 // Host name
34 std::string host_;
35
36 // Port to connect on
37 int port_;
38
David Reiss1997f102008-04-29 00:29:41 +000039 // Socket for the server
40 int socket_;
41
David Reiss6d0cccd2008-02-28 21:20:12 +000042 // Last time connecting to this server failed
43 int lastFailTime_;
44
45 // Number of consecutive times connecting to this server failed
46 int consecutiveFailures_;
47};
48
jsobele02e4242007-05-08 17:51:49 +000049/**
50 * TCP Socket implementation of the TTransport interface.
51 *
52 * @author Mark Slee <mcslee@facebook.com>
53 */
54class TSocketPool : public TSocket {
55
56 public:
David Reiss8f3bce42008-03-18 18:21:52 +000057
58 /**
59 * Socket pool constructor
60 */
61 TSocketPool();
62
jsobele02e4242007-05-08 17:51:49 +000063 /**
64 * Socket pool constructor
65 *
66 * @param hosts list of host names
67 * @param ports list of port names
68 */
69 TSocketPool(const std::vector<std::string> &hosts,
70 const std::vector<int> &ports);
71
72 /**
73 * Socket pool constructor
74 *
75 * @param servers list of pairs of host name and port
76 */
David Reiss907ad762008-03-02 00:25:58 +000077 TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
78
79 /**
80 * Socket pool constructor
81 *
82 * @param servers list of TSocketPoolServers
83 */
David Reiss8f3bce42008-03-18 18:21:52 +000084 TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
jsobele02e4242007-05-08 17:51:49 +000085
86 /**
dweatherfordd1372822007-10-09 22:57:23 +000087 * Socket pool constructor
88 *
89 * @param host single host
90 * @param port single port
91 */
92 TSocketPool(const std::string& host, int port);
93
94 /**
jsobele02e4242007-05-08 17:51:49 +000095 * Destroyes the socket object, closing it if necessary.
96 */
97 virtual ~TSocketPool();
98
99 /**
dweatherfordd1372822007-10-09 22:57:23 +0000100 * Add a server to the pool
101 */
102 void addServer(const std::string& host, int port);
103
David Reiss8f3bce42008-03-18 18:21:52 +0000104 /**
105 * Set list of servers in this pool
106 */
107 void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000108
109 /**
110 * Get list of servers in this pool
111 */
David Reiss8f3bce42008-03-18 18:21:52 +0000112 void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000113
dweatherfordd1372822007-10-09 22:57:23 +0000114 /**
jsobele02e4242007-05-08 17:51:49 +0000115 * Sets how many times to keep retrying a host in the connect function.
116 */
117 void setNumRetries(int numRetries);
118
119 /**
120 * Sets how long to wait until retrying a host if it was marked down
121 */
122 void setRetryInterval(int retryInterval);
123
124 /**
125 * Sets how many times to keep retrying a host before marking it as down.
126 */
127 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
128
129 /**
130 * Turns randomization in connect order on or off.
131 */
132 void setRandomize(bool randomize);
133
134 /**
135 * Whether to always try the last server.
136 */
137 void setAlwaysTryLast(bool alwaysTryLast);
138
139 /**
140 * Creates and opens the UNIX socket.
141 */
142 void open();
143
David Reiss1997f102008-04-29 00:29:41 +0000144 /*
145 * Closes the UNIX socket
146 */
147 void close();
148
jsobele02e4242007-05-08 17:51:49 +0000149 protected:
150
David Reiss1997f102008-04-29 00:29:41 +0000151 void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server);
152
jsobele02e4242007-05-08 17:51:49 +0000153 /** List of servers to connect to */
David Reiss8f3bce42008-03-18 18:21:52 +0000154 std::vector< boost::shared_ptr<TSocketPoolServer> > servers_;
jsobele02e4242007-05-08 17:51:49 +0000155
David Reiss1997f102008-04-29 00:29:41 +0000156 /** Current server */
157 boost::shared_ptr<TSocketPoolServer> currentServer_;
158
jsobele02e4242007-05-08 17:51:49 +0000159 /** How many times to retry each host in connect */
160 int numRetries_;
161
162 /** Retry interval in seconds, how long to not try a host if it has been
163 * marked as down.
164 */
165 int retryInterval_;
166
167 /** Max consecutive failures before marking a host down. */
168 int maxConsecutiveFailures_;
169
170 /** Try hosts in order? or Randomized? */
171 bool randomize_;
172
173 /** Always try last host, even if marked down? */
174 bool alwaysTryLast_;
175};
176
T Jake Lucianib5e62212009-01-31 22:36:20 +0000177}}} // apache::thrift::transport
jsobele02e4242007-05-08 17:51:49 +0000178
179#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
180