blob: bed4cca0acf3db1e9411783ae9f1eb3bcaf31e14 [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 */
68 TSocketPool(const std::vector<std::pair<std::string, int> > servers);
69
70 /**
dweatherfordd1372822007-10-09 22:57:23 +000071 * Socket pool constructor
72 *
73 * @param host single host
74 * @param port single port
75 */
76 TSocketPool(const std::string& host, int port);
77
78 /**
jsobele02e4242007-05-08 17:51:49 +000079 * Destroyes the socket object, closing it if necessary.
80 */
81 virtual ~TSocketPool();
82
83 /**
dweatherfordd1372822007-10-09 22:57:23 +000084 * Add a server to the pool
85 */
86 void addServer(const std::string& host, int port);
87
88 /**
jsobele02e4242007-05-08 17:51:49 +000089 * Sets how many times to keep retrying a host in the connect function.
90 */
91 void setNumRetries(int numRetries);
92
93 /**
94 * Sets how long to wait until retrying a host if it was marked down
95 */
96 void setRetryInterval(int retryInterval);
97
98 /**
99 * Sets how many times to keep retrying a host before marking it as down.
100 */
101 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
102
103 /**
104 * Turns randomization in connect order on or off.
105 */
106 void setRandomize(bool randomize);
107
108 /**
109 * Whether to always try the last server.
110 */
111 void setAlwaysTryLast(bool alwaysTryLast);
112
113 /**
114 * Creates and opens the UNIX socket.
115 */
116 void open();
117
118 protected:
119
120 /** List of servers to connect to */
David Reiss6d0cccd2008-02-28 21:20:12 +0000121 std::vector<TSocketPoolServer> servers_;
jsobele02e4242007-05-08 17:51:49 +0000122
123 /** How many times to retry each host in connect */
124 int numRetries_;
125
126 /** Retry interval in seconds, how long to not try a host if it has been
127 * marked as down.
128 */
129 int retryInterval_;
130
131 /** Max consecutive failures before marking a host down. */
132 int maxConsecutiveFailures_;
133
134 /** Try hosts in order? or Randomized? */
135 bool randomize_;
136
137 /** Always try last host, even if marked down? */
138 bool alwaysTryLast_;
139};
140
141}}} // facebook::thrift::transport
142
143#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
144