root/trunk/whisperlib/net/http/failsafe_http_client.h

Revision 7, 3.8 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31 //
32 #ifndef __NET_HTTP_FAILSAFE_HTTP_CLIENT_H__
33 #define __NET_HTTP_FAILSAFE_HTTP_CLIENT_H__
34
35 #include <string>
36 #include <vector>
37 #include <deque>
38
39 #include <whisperlib/common/base/types.h>
40 #include <whisperlib/common/base/callback.h>
41 #include <whisperlib/net/http/http_client_protocol.h>
42
43
44 namespace http {
45 //
46 // This is a class that accepts a set of servers to connect to and
47 // requests that can go to any of these servers.
48 // The requests will be distributed to the servers in a load-balancing
49 // fashion.
50 // In case of a failure the request is tried w/ another server until
51 // successful fetch is performed (ie. no network related trouble happend),
52 // or until the retry count goes to zero.
53 //
54 class FailSafeClient {
55  public:
56   FailSafeClient(
57       net::Selector* selector,
58       const ClientParams* client_params,
59       const vector<net::HostPort>& servers,
60       ResultClosure<BaseClientConnection*>* connection_factory,
61       int num_retries,
62       int32 request_timeout_ms,
63       int32 reopen_connection_interval_ms,
64       bool force_host_header);
65   ~FailSafeClient();
66
67   void StartRequest(ClientRequest* request,
68                     Closure* completion_callback);
69
70  private:
71   struct PendingStruct {
72     int64 start_time_;
73     int retries_left_;
74     ClientRequest* req_;
75     Closure* completion_closure_;
76     PendingStruct(int64 start_time, int retries_left,
77                   ClientRequest* req, Closure* completion_closure)
78         : start_time_(start_time),
79           retries_left_(retries_left),
80           req_(req),
81           completion_closure_(completion_closure) {
82     }
83   };
84
85   void RequeuePendingAlarm();
86   void RequeuePending();
87   bool InternalStartRequest(PendingStruct* ps);
88   void CompletionCallback(PendingStruct* ps);
89
90   static const int32 kRequeuePendingAlarmMs = 1000;
91
92   net::Selector* const selector_;
93   const ClientParams* client_params_;
94   vector<net::HostPort> servers_;
95   ResultClosure<BaseClientConnection*>* connection_factory_;
96   const int num_retries_;
97   const int64 request_timeout_ms_;
98   const int64 reopen_connection_interval_ms_;
99   const bool force_host_header_;
100
101   vector<ClientProtocol*> clients_;
102   vector<int64> death_time_;
103
104   typedef deque<PendingStruct*> PendingQueue;
105   PendingQueue* pending_requests_;
106   bool closing_;
107   Closure* requeue_pending_callback_;
108 };
109 }
110
111 #endif
Note: See TracBrowser for help on using the browser.