blob: a197c72b4deba648297470e7f278ef168e006650 [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
13namespace facebook { namespace thrift { namespace transport {
14
15/**
16 * TCP Socket implementation of the TTransport interface.
17 *
18 * @author Mark Slee <mcslee@facebook.com>
19 */
20class TSocketPool : public TSocket {
21
22 public:
23 /**
24 * Socket pool constructor
25 *
26 * @param hosts list of host names
27 * @param ports list of port names
28 */
29 TSocketPool(const std::vector<std::string> &hosts,
30 const std::vector<int> &ports);
31
32 /**
33 * Socket pool constructor
34 *
35 * @param servers list of pairs of host name and port
36 */
37 TSocketPool(const std::vector<std::pair<std::string, int> > servers);
38
39 /**
dweatherfordd1372822007-10-09 22:57:23 +000040 * Socket pool constructor
41 *
42 * @param host single host
43 * @param port single port
44 */
45 TSocketPool(const std::string& host, int port);
46
47 /**
jsobele02e4242007-05-08 17:51:49 +000048 * Destroyes the socket object, closing it if necessary.
49 */
50 virtual ~TSocketPool();
51
52 /**
dweatherfordd1372822007-10-09 22:57:23 +000053 * Add a server to the pool
54 */
55 void addServer(const std::string& host, int port);
56
57 /**
jsobele02e4242007-05-08 17:51:49 +000058 * Sets how many times to keep retrying a host in the connect function.
59 */
60 void setNumRetries(int numRetries);
61
62 /**
63 * Sets how long to wait until retrying a host if it was marked down
64 */
65 void setRetryInterval(int retryInterval);
66
67 /**
68 * Sets how many times to keep retrying a host before marking it as down.
69 */
70 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
71
72 /**
73 * Turns randomization in connect order on or off.
74 */
75 void setRandomize(bool randomize);
76
77 /**
78 * Whether to always try the last server.
79 */
80 void setAlwaysTryLast(bool alwaysTryLast);
81
82 /**
83 * Creates and opens the UNIX socket.
84 */
85 void open();
86
87 protected:
88
89 /** List of servers to connect to */
90 std::vector<std::pair<std::string, int> > servers_;
91
92 /** How many times to retry each host in connect */
93 int numRetries_;
94
95 /** Retry interval in seconds, how long to not try a host if it has been
96 * marked as down.
97 */
98 int retryInterval_;
99
100 /** Max consecutive failures before marking a host down. */
101 int maxConsecutiveFailures_;
102
103 /** Try hosts in order? or Randomized? */
104 bool randomize_;
105
106 /** Always try last host, even if marked down? */
107 bool alwaysTryLast_;
108};
109
110}}} // facebook::thrift::transport
111
112#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
113