blob: 2d0496bb84d3e0c3710787940cd7ccbc3b686ec8 [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:
54 /**
55 * Socket pool constructor
56 *
57 * @param hosts list of host names
58 * @param ports list of port names
59 */
60 TSocketPool(const std::vector<std::string> &hosts,
61 const std::vector<int> &ports);
62
63 /**
64 * Socket pool constructor
65 *
66 * @param servers list of pairs of host name and port
67 */
David Reiss907ad762008-03-02 00:25:58 +000068 TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
69
70 /**
71 * Socket pool constructor
72 *
73 * @param servers list of TSocketPoolServers
74 */
75 TSocketPool(const std::vector<TSocketPoolServer>& servers);
jsobele02e4242007-05-08 17:51:49 +000076
77 /**
dweatherfordd1372822007-10-09 22:57:23 +000078 * Socket pool constructor
79 *
80 * @param host single host
81 * @param port single port
82 */
83 TSocketPool(const std::string& host, int port);
84
85 /**
jsobele02e4242007-05-08 17:51:49 +000086 * Destroyes the socket object, closing it if necessary.
87 */
88 virtual ~TSocketPool();
89
90 /**
dweatherfordd1372822007-10-09 22:57:23 +000091 * Add a server to the pool
92 */
93 void addServer(const std::string& host, int port);
94
David Reiss907ad762008-03-02 00:25:58 +000095
96 /**
97 * Get list of servers in this pool
98 */
99 std::vector<TSocketPoolServer> getServers();
100
dweatherfordd1372822007-10-09 22:57:23 +0000101 /**
jsobele02e4242007-05-08 17:51:49 +0000102 * Sets how many times to keep retrying a host in the connect function.
103 */
104 void setNumRetries(int numRetries);
105
106 /**
107 * Sets how long to wait until retrying a host if it was marked down
108 */
109 void setRetryInterval(int retryInterval);
110
111 /**
112 * Sets how many times to keep retrying a host before marking it as down.
113 */
114 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
115
116 /**
117 * Turns randomization in connect order on or off.
118 */
119 void setRandomize(bool randomize);
120
121 /**
122 * Whether to always try the last server.
123 */
124 void setAlwaysTryLast(bool alwaysTryLast);
125
126 /**
127 * Creates and opens the UNIX socket.
128 */
129 void open();
130
131 protected:
132
133 /** List of servers to connect to */
David Reiss6d0cccd2008-02-28 21:20:12 +0000134 std::vector<TSocketPoolServer> servers_;
jsobele02e4242007-05-08 17:51:49 +0000135
136 /** How many times to retry each host in connect */
137 int numRetries_;
138
139 /** Retry interval in seconds, how long to not try a host if it has been
140 * marked as down.
141 */
142 int retryInterval_;
143
144 /** Max consecutive failures before marking a host down. */
145 int maxConsecutiveFailures_;
146
147 /** Try hosts in order? or Randomized? */
148 bool randomize_;
149
150 /** Always try last host, even if marked down? */
151 bool alwaysTryLast_;
152};
153
154}}} // facebook::thrift::transport
155
156#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
157