/src/pdns/pdns/dnsdistdist/delaypipe.cc
Line | Count | Source |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of version 2 of the GNU General Public License as |
7 | | * published by the Free Software Foundation. |
8 | | * |
9 | | * In addition, for the avoidance of any doubt, permission is granted to |
10 | | * link this program with OpenSSL and to (re)distribute the binaries |
11 | | * produced as the result of such linking. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | | */ |
22 | | #include "delaypipe.hh" |
23 | | #include "misc.hh" |
24 | | #include "gettime.hh" |
25 | | #include <thread> |
26 | | #include "threadname.hh" |
27 | | |
28 | | template <class T> |
29 | | ObjectPipe<T>::ObjectPipe() |
30 | | { |
31 | | auto [sender, receiver] = pdns::channel::createObjectQueue<T>(pdns::channel::SenderBlockingMode::SenderBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, 0, false); |
32 | | d_sender = std::move(sender); |
33 | | d_receiver = std::move(receiver); |
34 | | } |
35 | | |
36 | | template <class T> |
37 | | void ObjectPipe<T>::close() |
38 | 0 | { |
39 | 0 | d_sender.close(); |
40 | 0 | } |
41 | | |
42 | | template <class T> |
43 | | void ObjectPipe<T>::write(T& t) |
44 | 0 | { |
45 | 0 | auto ptr = std::make_unique<T>(t); |
46 | 0 | if (!d_sender.send(std::move(ptr))) { |
47 | 0 | unixDie("writing to the DelayPipe"); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | | template <class T> |
52 | | int ObjectPipe<T>::readTimeout(T* t, double msec) |
53 | | { |
54 | | while (true) { |
55 | | int ret = waitForData(d_receiver.getDescriptor(), 0, msec); |
56 | | if (ret < 0) { |
57 | | if (errno == EINTR) { |
58 | | continue; |
59 | | } |
60 | | unixDie("waiting for data in object pipe"); |
61 | | } |
62 | | else if (ret == 0) { |
63 | | return -1; |
64 | | } |
65 | | |
66 | | try { |
67 | | auto tmp = d_receiver.receive(); |
68 | | if (!tmp) { |
69 | | if (d_receiver.isClosed()) { |
70 | | return 0; |
71 | | } |
72 | | continue; |
73 | | } |
74 | | |
75 | | *t = **tmp; |
76 | | return 1; |
77 | | } |
78 | | catch (const std::exception& e) { |
79 | | throw std::runtime_error("reading from the delay pipe: " + std::string(e.what())); |
80 | | } |
81 | | } |
82 | | } |
83 | | |
84 | | template <class T> |
85 | | DelayPipe<T>::DelayPipe() : |
86 | | d_thread(&DelayPipe<T>::worker, this) |
87 | | { |
88 | | } |
89 | | |
90 | | template <class T> |
91 | | void DelayPipe<T>::gettime(struct timespec* ts) |
92 | 0 | { |
93 | 0 | ::gettime(ts); |
94 | 0 | } |
95 | | |
96 | | template <class T> |
97 | | void DelayPipe<T>::submit(T& t, int msec) |
98 | 0 | { |
99 | 0 | struct timespec now; |
100 | 0 | gettime(&now); |
101 | 0 | now.tv_nsec += msec * 1e6; |
102 | 0 | while (now.tv_nsec > 1e9) { |
103 | 0 | now.tv_sec++; |
104 | 0 | now.tv_nsec -= 1e9; |
105 | 0 | } |
106 | 0 | Combo c{t, now}; |
107 | 0 | d_pipe.write(c); |
108 | 0 | } |
109 | | |
110 | | template <class T> |
111 | | DelayPipe<T>::~DelayPipe() |
112 | 0 | { |
113 | 0 | d_pipe.close(); |
114 | 0 | d_thread.join(); |
115 | 0 | } |
116 | | |
117 | | template <class T> |
118 | | void DelayPipe<T>::worker() |
119 | | { |
120 | | setThreadName("dnsdist/delayPi"); |
121 | | Combo c; |
122 | | for (;;) { |
123 | | /* this code is slightly too subtle, but I don't see how it could be any simpler. |
124 | | So we have a set of work to do, and we need to wait until the time arrives to do it. |
125 | | Simultaneously new work might come in. So we try to combine both of these things by |
126 | | setting a timeout on listening to the pipe over which new work comes in. This timeout |
127 | | is equal to the wait until the first thing that needs to be done. |
128 | | |
129 | | Two additional cases exist: we have no work to wait for, so we can wait infinitely long. |
130 | | The other special case is that the first we have to do.. is in the past, so we need to do it |
131 | | immediately. */ |
132 | | |
133 | | double delay = -1; // infinite |
134 | | struct timespec now; |
135 | | if (!d_work.empty()) { |
136 | | gettime(&now); |
137 | | delay = 1000 * tsdelta(d_work.begin()->first, now); |
138 | | if (delay < 0) { |
139 | | delay = 0; // don't wait - we have work that is late already! |
140 | | } |
141 | | } |
142 | | if (delay != 0) { |
143 | | int ret = d_pipe.readTimeout(&c, delay); |
144 | | if (ret > 0) { // we got an object |
145 | | d_work.emplace(c.when, c.what); |
146 | | } |
147 | | else if (ret == 0) { // EOF |
148 | | break; |
149 | | } |
150 | | else { |
151 | | ; |
152 | | } |
153 | | gettime(&now); |
154 | | } |
155 | | |
156 | | tscomp cmp; |
157 | | |
158 | | for (auto iter = d_work.begin(); iter != d_work.end();) { // do the needful |
159 | | if (cmp(iter->first, now)) { |
160 | | iter->second(); |
161 | | d_work.erase(iter++); |
162 | | } |
163 | | else { |
164 | | break; |
165 | | } |
166 | | } |
167 | | } |
168 | | } |