blob: b6440fc826e02d0a677ed9374f0ee8280a7f1d76 [file] [log] [blame]
jsobele02e4242007-05-08 17:51:49 +00001// Copyright (c) 2007- 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#include <algorithm>
8#include <iostream>
9
10#include "TSocketPool.h"
11
12namespace facebook { namespace thrift { namespace transport {
13
14using namespace std;
15
16/**
17 * TSocketPool implementation.
18 *
19 * @author Jason Sobel <jsobel@facebook.com>
20 */
21
22TSocketPool::TSocketPool(const vector<string> &hosts,
23 const vector<int> &ports) : TSocket(),
24 numRetries_(1),
25 retryInterval_(60),
26 maxConsecutiveFailures_(1),
27 randomize_(true),
28 alwaysTryLast_(true)
29{
30 if (hosts.size() != ports.size()) {
boz6ded7752007-06-05 22:41:18 +000031 GlobalOutput("TSocketPool::TSocketPool: hosts.size != ports.size");
jsobele02e4242007-05-08 17:51:49 +000032 throw TTransportException(TTransportException::BAD_ARGS);
33 }
34
35 for (unsigned int i = 0; i < hosts.size(); ++i) {
dweatherfordd1372822007-10-09 22:57:23 +000036 addServer(hosts[i], ports[i]);
jsobele02e4242007-05-08 17:51:49 +000037 }
38}
39
40TSocketPool::TSocketPool(const vector<pair<string, int> > servers) : TSocket(),
41 servers_(servers),
42 numRetries_(1),
43 retryInterval_(60),
44 maxConsecutiveFailures_(1),
45 randomize_(true),
46 alwaysTryLast_(true)
47{
48}
49
dweatherfordd1372822007-10-09 22:57:23 +000050TSocketPool::TSocketPool(const string& host, int port) : TSocket(),
51 numRetries_(1),
52 retryInterval_(60),
53 maxConsecutiveFailures_(1),
54 randomize_(true),
55 alwaysTryLast_(true)
56{
57 addServer(host, port);
58}
59
jsobele02e4242007-05-08 17:51:49 +000060TSocketPool::~TSocketPool() {
61 close();
62}
63
dweatherfordd1372822007-10-09 22:57:23 +000064void TSocketPool::addServer(const string& host, int port) {
65 servers_.push_back(pair<string, int>(host, port));
66}
67
jsobele02e4242007-05-08 17:51:49 +000068void TSocketPool::setNumRetries(int numRetries) {
69 numRetries_ = numRetries;
70}
71
72void TSocketPool::setRetryInterval(int retryInterval) {
73 retryInterval_ = retryInterval;
74}
75
76
77void TSocketPool::setMaxConsecutiveFailures(int maxConsecutiveFailures) {
78 maxConsecutiveFailures_ = maxConsecutiveFailures;
79}
80
81void TSocketPool::setRandomize(bool randomize) {
82 randomize_ = randomize;
83}
84
85void TSocketPool::setAlwaysTryLast(bool alwaysTryLast) {
86 alwaysTryLast_ = alwaysTryLast;
87}
88
89/* TODO: without apc we ignore a lot of functionality from the php version */
90void TSocketPool::open() {
91 if (randomize_) {
92 std::random_shuffle(servers_.begin(), servers_.end());
93 }
94
95 for (unsigned int i = 0; i < servers_.size(); ++i) {
96 host_ = servers_[i].first;
97 port_ = servers_[i].second;
98
99 for (int j = 0; j < numRetries_; ++j) {
100 try {
101 TSocket::open();
102
103 // success
104 return;
105 } catch (TException e) {
106 // connection failed
107 }
108 }
109 }
110
boz6ded7752007-06-05 22:41:18 +0000111 GlobalOutput("TSocketPool::open: all connections failed");
jsobele02e4242007-05-08 17:51:49 +0000112 throw TTransportException(TTransportException::NOT_OPEN);
113}
114
115}}} // facebook::thrift::transport