summaryrefslogtreecommitdiffstats
path: root/net/base/client_socket_pool.cc
blob: 2fb67034c6cb30687e112bb802e7ca0a63c53b07 (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
177
178
179
180
181
// 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/base/client_socket_pool.h"

#include "base/message_loop.h"
#include "net/base/client_socket.h"
#include "net/base/client_socket_handle.h"
#include "net/base/net_errors.h"

namespace {

// The timeout value, in seconds, used to clean up disconnected idle sockets.
const int kCleanupInterval = 5;

}  // namespace

namespace net {

ClientSocketPool::ClientSocketPool(int max_sockets_per_group)
    : idle_socket_count_(0),
      max_sockets_per_group_(max_sockets_per_group) {
}

ClientSocketPool::~ClientSocketPool() {
  // Clean up any idle sockets.  Assert that we have no remaining active sockets
  // or pending requests.  They should have all been cleaned up prior to the
  // manager being destroyed.
  CloseIdleSockets();
  DCHECK(group_map_.empty());
}

int ClientSocketPool::RequestSocket(ClientSocketHandle* handle,
                                    CompletionCallback* callback) {
  Group& group = group_map_[handle->group_name_];

  // Can we make another active socket now?
  if (group.active_socket_count == max_sockets_per_group_) {
    Request r;
    r.handle = handle;
    DCHECK(callback);
    r.callback = callback;
    group.pending_requests.push_back(r);
    return ERR_IO_PENDING;
  }

  // OK, we are going to activate one.
  group.active_socket_count++;

  // Use idle sockets in LIFO order because they're more likely to be
  // still connected.
  while (!group.idle_sockets.empty()) {
    ClientSocketPtr* ptr = group.idle_sockets.back();
    group.idle_sockets.pop_back();
    DecrementIdleCount();
    if ((*ptr)->IsConnected()) {
      // We found one we can reuse!
      handle->socket_ = ptr;
      return OK;
    }
    delete ptr;
  }

  handle->socket_ = new ClientSocketPtr();
  return OK;
}

void ClientSocketPool::CancelRequest(ClientSocketHandle* handle) {
  Group& group = group_map_[handle->group_name_];

  // In order for us to be canceling a pending request, we must have active
  // sockets equaling the limit.  NOTE: The correctness of the code doesn't
  // require this assertion.
  DCHECK(group.active_socket_count == max_sockets_per_group_);

  // Search pending_requests for matching handle.
  std::deque<Request>::iterator it = group.pending_requests.begin();
  for (; it != group.pending_requests.end(); ++it) {
    if (it->handle == handle) {
      group.pending_requests.erase(it);
      break;
    }
  }
}

void ClientSocketPool::ReleaseSocket(ClientSocketHandle* handle) {
  // Run this asynchronously to allow the caller to finish before we let
  // another to begin doing work.  This also avoids nasty recursion issues.
  // NOTE: We cannot refer to the handle argument after this method returns.
  MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
      this, &ClientSocketPool::DoReleaseSocket, handle->group_name_,
      handle->socket_));
}

void ClientSocketPool::CloseIdleSockets() {
  MaybeCloseIdleSockets(false);
}

void ClientSocketPool::MaybeCloseIdleSockets(
    bool only_if_disconnected) {
  if (idle_socket_count_ == 0)
    return;

  GroupMap::iterator i = group_map_.begin();
  while (i != group_map_.end()) {
    Group& group = i->second;

    std::deque<ClientSocketPtr*>::iterator j = group.idle_sockets.begin();
    while (j != group.idle_sockets.end()) {
      if (!only_if_disconnected || !(*j)->get()->IsConnected()) {
        delete *j;
        j = group.idle_sockets.erase(j);
        DecrementIdleCount();
      } else {
        ++j;
      }
    }

    // Delete group if no longer needed.
    if (group.active_socket_count == 0 && group.idle_sockets.empty()) {
      DCHECK(group.pending_requests.empty());
      group_map_.erase(i++);
    } else {
      ++i;
    }
  }
}

void ClientSocketPool::IncrementIdleCount() {
  if (++idle_socket_count_ == 1)
    timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
                 &ClientSocketPool::DoTimeout);
}

void ClientSocketPool::DecrementIdleCount() {
  if (--idle_socket_count_ == 0)
    timer_.Stop();
}

void ClientSocketPool::DoReleaseSocket(const std::string& group_name,
                                       ClientSocketPtr* ptr) {
  GroupMap::iterator i = group_map_.find(group_name);
  DCHECK(i != group_map_.end());

  Group& group = i->second;

  DCHECK(group.active_socket_count > 0);
  group.active_socket_count--;

  bool can_reuse = ptr->get() && (*ptr)->IsConnected();
  if (can_reuse) {
    group.idle_sockets.push_back(ptr);
    IncrementIdleCount();
  } else {
    delete ptr;
  }

  // Process one pending request.
  if (!group.pending_requests.empty()) {
    Request r = group.pending_requests.front();
    group.pending_requests.pop_front();
    int rv = RequestSocket(r.handle, NULL);
    DCHECK(rv == OK);
    r.callback->Run(rv);
    return;
  }

  // Delete group if no longer needed.
  if (group.active_socket_count == 0 && group.idle_sockets.empty()) {
    DCHECK(group.pending_requests.empty());
    group_map_.erase(i);
  }
}

void ClientSocketPool::DoTimeout() {
  MaybeCloseIdleSockets(true);
}

}  // namespace net