summaryrefslogtreecommitdiffstats
path: root/net/http/winhttp_request_throttle.cc
blob: 79415b37d0c304bea33471a1078e6147a4403a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http/winhttp_request_throttle.h"

#include "base/logging.h"
#include "net/http/http_transaction_winhttp.h"

namespace {

// The arguments to a WinHttpSendRequest call.
struct SendRequestArgs {
  SendRequestArgs() : request_handle(NULL), total_size(0), context(0) {}

  SendRequestArgs(HINTERNET handle, DWORD size, DWORD_PTR context_value)
      : request_handle(handle), total_size(size), context(context_value) {}

  HINTERNET request_handle;
  DWORD total_size;
  DWORD_PTR context;
};

}  // namespace

namespace net {

// Per-server queue for WinHttpSendRequest calls.
class WinHttpRequestThrottle::RequestQueue {
 public:
  RequestQueue() {}

  // Adds |args| to the end of the queue.
  void PushBack(const SendRequestArgs& args) { queue_.push_back(args); }

  // If the queue is not empty, pops the first entry off the queue, saves it
  // in |*args|, and returns true.  If the queue is empty, returns false.
  bool GetFront(SendRequestArgs* args);

  // If the queue has an entry containing |request_handle|, removes it and
  // returns true.  Otherwise, returns false.
  bool Remove(HINTERNET request_handle);

  bool empty() const { return queue_.empty(); }

 private:
  std::list<SendRequestArgs> queue_;

  DISALLOW_EVIL_CONSTRUCTORS(RequestQueue);
};

bool WinHttpRequestThrottle::RequestQueue::GetFront(SendRequestArgs* args) {
  if (queue_.empty())
    return false;
  *args = queue_.front();
  queue_.pop_front();
  return true;
}

bool WinHttpRequestThrottle::RequestQueue::Remove(HINTERNET request_handle) {
  std::list<SendRequestArgs>::iterator it;
  for (it = queue_.begin(); it != queue_.end(); ++it) {
    if (it->request_handle == request_handle) {
      queue_.erase(it);
      return true;
    }
  }
  return false;
}

WinHttpRequestThrottle::~WinHttpRequestThrottle() {
#ifndef NDEBUG
  ThrottleMap::const_iterator throttle_iter = throttles_.begin();
  for (; throttle_iter != throttles_.end(); ++throttle_iter) {
    const PerServerThrottle& throttle = throttle_iter->second;
    DCHECK(throttle.num_requests == 0);
    DCHECK(!throttle.request_queue.get() || throttle.request_queue->empty());
  }
#endif
}

BOOL WinHttpRequestThrottle::SubmitRequest(const std::string &server,
                                           HINTERNET request_handle,
                                           DWORD total_size,
                                           DWORD_PTR context) {
  PerServerThrottle& throttle = throttles_[server];
  DCHECK(throttle.num_requests >= 0 &&
         throttle.num_requests <= kMaxConnectionsPerServer);
  if (throttle.num_requests >= kMaxConnectionsPerServer) {
    if (!throttle.request_queue.get())
      throttle.request_queue.reset(new RequestQueue);
    SendRequestArgs args(request_handle, total_size, context);
    throttle.request_queue->PushBack(args);
    return TRUE;
  }

  BOOL ok = SendRequest(request_handle, total_size, context, false);
  if (ok)
    throttle.num_requests += 1;
  return ok;
}

void WinHttpRequestThrottle::NotifyRequestDone(const std::string& server) {
  PerServerThrottle& throttle = throttles_[server];
  DCHECK(throttle.num_requests > 0 &&
         throttle.num_requests <= kMaxConnectionsPerServer);
  throttle.num_requests -= 1;
  SendRequestArgs args;
  if (throttle.request_queue.get() &&
      throttle.request_queue->GetFront(&args)) {
    throttle.num_requests += 1;
    SendRequest(args.request_handle, args.total_size, args.context, true);
  }
  if (throttles_.size() > static_cast<size_t>(kGarbageCollectionThreshold))
    GarbageCollect();
}

void WinHttpRequestThrottle::RemoveRequest(const std::string& server,
                                           HINTERNET request_handle) {
  PerServerThrottle& throttle = throttles_[server];
  if (throttle.request_queue.get() &&
      throttle.request_queue->Remove(request_handle))
    return;
  NotifyRequestDone(server);
}

BOOL WinHttpRequestThrottle::SendRequest(HINTERNET request_handle,
                                         DWORD total_size,
                                         DWORD_PTR context,
                                         bool report_async_error) {
  BOOL ok = WinHttpSendRequest(request_handle,
                               WINHTTP_NO_ADDITIONAL_HEADERS,
                               0,
                               WINHTTP_NO_REQUEST_DATA,
                               0,
                               total_size,
                               context);
  if (!ok && report_async_error) {
    WINHTTP_ASYNC_RESULT async_result = { API_SEND_REQUEST, GetLastError() };
    HttpTransactionWinHttp::StatusCallback(
        request_handle, context,
        WINHTTP_CALLBACK_STATUS_REQUEST_ERROR,
        &async_result, sizeof(async_result));
  }
  return ok;
}

WinHttpRequestThrottle::PerServerThrottle::PerServerThrottle()
    : num_requests(0) {
}

WinHttpRequestThrottle::PerServerThrottle::~PerServerThrottle() {
}

// static
const int WinHttpRequestThrottle::kMaxConnectionsPerServer = 6;

// static
const int WinHttpRequestThrottle::kGarbageCollectionThreshold = 64;

void WinHttpRequestThrottle::GarbageCollect() {
  ThrottleMap::iterator throttle_iter = throttles_.begin();
  while (throttle_iter != throttles_.end()) {
    PerServerThrottle& throttle = throttle_iter->second;
    if (throttle.num_requests == 0 &&
       (!throttle.request_queue.get() || throttle.request_queue->empty())) {
      // Erase the current item but keep the iterator valid.
      throttles_.erase(throttle_iter++);
    } else {
      ++throttle_iter;
    }
  }
}

}  // namespace net