blob: 6c3b0f7528aeb3b84049a9b276591929480002ca [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
David Reiss0c90f6f2008-02-06 22:18:40 +000013namespace facebook { 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
39 // Last time connecting to this server failed
40 int lastFailTime_;
41
42 // Number of consecutive times connecting to this server failed
43 int consecutiveFailures_;
44};
45
jsobele02e4242007-05-08 17:51:49 +000046/**
47 * TCP Socket implementation of the TTransport interface.
48 *
49 * @author Mark Slee <mcslee@facebook.com>
50 */
51class TSocketPool : public TSocket {
52
53 public:
David Reiss8f3bce42008-03-18 18:21:52 +000054
55 /**
56 * Socket pool constructor
57 */
58 TSocketPool();
59
jsobele02e4242007-05-08 17:51:49 +000060 /**
61 * Socket pool constructor
62 *
63 * @param hosts list of host names
64 * @param ports list of port names
65 */
66 TSocketPool(const std::vector<std::string> &hosts,
67 const std::vector<int> &ports);
68
69 /**
70 * Socket pool constructor
71 *
72 * @param servers list of pairs of host name and port
73 */
David Reiss907ad762008-03-02 00:25:58 +000074 TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
75
76 /**
77 * Socket pool constructor
78 *
79 * @param servers list of TSocketPoolServers
80 */
David Reiss8f3bce42008-03-18 18:21:52 +000081 TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
jsobele02e4242007-05-08 17:51:49 +000082
83 /**
dweatherfordd1372822007-10-09 22:57:23 +000084 * Socket pool constructor
85 *
86 * @param host single host
87 * @param port single port
88 */
89 TSocketPool(const std::string& host, int port);
90
91 /**
jsobele02e4242007-05-08 17:51:49 +000092 * Destroyes the socket object, closing it if necessary.
93 */
94 virtual ~TSocketPool();
95
96 /**
dweatherfordd1372822007-10-09 22:57:23 +000097 * Add a server to the pool
98 */
99 void addServer(const std::string& host, int port);
100
David Reiss8f3bce42008-03-18 18:21:52 +0000101 /**
102 * Set list of servers in this pool
103 */
104 void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000105
106 /**
107 * Get list of servers in this pool
108 */
David Reiss8f3bce42008-03-18 18:21:52 +0000109 void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000110
dweatherfordd1372822007-10-09 22:57:23 +0000111 /**
jsobele02e4242007-05-08 17:51:49 +0000112 * Sets how many times to keep retrying a host in the connect function.
113 */
114 void setNumRetries(int numRetries);
115
116 /**
117 * Sets how long to wait until retrying a host if it was marked down
118 */
119 void setRetryInterval(int retryInterval);
120
121 /**
122 * Sets how many times to keep retrying a host before marking it as down.
123 */
124 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
125
126 /**
127 * Turns randomization in connect order on or off.
128 */
129 void setRandomize(bool randomize);
130
131 /**
132 * Whether to always try the last server.
133 */
134 void setAlwaysTryLast(bool alwaysTryLast);
135
136 /**
137 * Creates and opens the UNIX socket.
138 */
139 void open();
140
141 protected:
142
143 /** List of servers to connect to */
David Reiss8f3bce42008-03-18 18:21:52 +0000144 std::vector< boost::shared_ptr<TSocketPoolServer> > servers_;
jsobele02e4242007-05-08 17:51:49 +0000145
146 /** How many times to retry each host in connect */
147 int numRetries_;
148
149 /** Retry interval in seconds, how long to not try a host if it has been
150 * marked as down.
151 */
152 int retryInterval_;
153
154 /** Max consecutive failures before marking a host down. */
155 int maxConsecutiveFailures_;
156
157 /** Try hosts in order? or Randomized? */
158 bool randomize_;
159
160 /** Always try last host, even if marked down? */
161 bool alwaysTryLast_;
162};
163
164}}} // facebook::thrift::transport
165
166#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
167