]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Connection.cc
pconn_lifetime
[thirdparty/squid.git] / src / comm / Connection.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "CachePeer.h"
11 #include "cbdata.h"
12 #include "comm.h"
13 #include "comm/Connection.h"
14 #include "fde.h"
15 #include "neighbors.h"
16 #include "SquidConfig.h"
17 #include "SquidTime.h"
18
19 class CachePeer;
20 bool
21 Comm::IsConnOpen(const Comm::ConnectionPointer &conn)
22 {
23 return conn != NULL && conn->isOpen();
24 }
25
26 Comm::Connection::Connection() :
27 local(),
28 remote(),
29 peerType(HIER_NONE),
30 fd(-1),
31 tos(0),
32 nfmark(0),
33 flags(COMM_NONBLOCKING),
34 peer_(NULL),
35 startTime_(squid_curtime)
36 {
37 *rfc931 = 0; // quick init the head. the rest does not matter.
38 }
39
40 static int64_t lost_conn = 0;
41 Comm::Connection::~Connection()
42 {
43 if (fd >= 0) {
44 debugs(5, 4, "BUG #3329: Orphan Comm::Connection: " << *this);
45 debugs(5, 4, "NOTE: " << ++lost_conn << " Orphans since last started.");
46 close();
47 }
48
49 cbdataReferenceDone(peer_);
50 }
51
52 Comm::ConnectionPointer
53 Comm::Connection::copyDetails() const
54 {
55 ConnectionPointer c = new Comm::Connection;
56
57 c->setAddrs(local, remote);
58 c->peerType = peerType;
59 c->tos = tos;
60 c->nfmark = nfmark;
61 c->flags = flags;
62 c->startTime_ = startTime_;
63
64 // ensure FD is not open in the new copy.
65 c->fd = -1;
66
67 // ensure we have a cbdata reference to peer_ not a straight ptr copy.
68 c->peer_ = cbdataReference(getPeer());
69
70 return c;
71 }
72
73 void
74 Comm::Connection::close()
75 {
76 if (isOpen()) {
77 comm_close(fd);
78 fd = -1;
79 if (CachePeer *p=getPeer())
80 peerConnClosed(p);
81 }
82 }
83
84 CachePeer *
85 Comm::Connection::getPeer() const
86 {
87 if (cbdataReferenceValid(peer_))
88 return peer_;
89
90 return NULL;
91 }
92
93 void
94 Comm::Connection::setPeer(CachePeer *p)
95 {
96 /* set to self. nothing to do. */
97 if (getPeer() == p)
98 return;
99
100 cbdataReferenceDone(peer_);
101 if (p) {
102 peer_ = cbdataReference(p);
103 }
104 }
105
106 time_t
107 Comm::Connection::timeLeft(const time_t idleTimeout) const
108 {
109 if (!Config.Timeout.pconnLifetime)
110 return idleTimeout;
111
112 const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
113 return min(lifeTimeLeft, idleTimeout);
114 }