2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
28 #include <condition_variable>
33 #include "circular_buffer.hh"
35 /* Writes can be submitted and they are atomically accepted. Either the whole write
36 ends up in the buffer or nothing ends up in the buffer.
37 In case nothing ends up in the buffer, an exception is thrown.
38 Similarly, EOF leads to this treatment
40 The filedescriptor can be in non-blocking mode.
42 This class is not threadsafe.
45 class CircularWriteBuffer
48 explicit CircularWriteBuffer(int fd, size_t size) : d_fd(fd), d_buffer(size)
52 void write(const std::string& str);
56 boost::circular_buffer<char> d_buffer;
59 class RemoteLoggerInterface
62 virtual ~RemoteLoggerInterface() {};
63 virtual void queueData(const std::string& data) = 0;
64 virtual std::string toString() const = 0;
66 bool logQueries(void) const { return d_logQueries; }
67 bool logResponses(void) const { return d_logResponses; }
68 void setLogQueries(bool flag) { d_logQueries = flag; }
69 void setLogResponses(bool flag) { d_logResponses = flag; }
72 bool d_logQueries{true};
73 bool d_logResponses{true};
76 /* Thread safe. Will connect asynchronously on request.
77 Runs a reconnection thread that also periodicall flushes.
78 Note that the buffer only runs as long as there is a connection.
79 If there is no connection we don't buffer a thing
81 class RemoteLogger : public RemoteLoggerInterface
84 RemoteLogger(const ComboAddress& remote, uint16_t timeout=2,
85 uint64_t maxQueuedBytes=100000,
86 uint8_t reconnectWaitTime=1,
87 bool asyncConnect=false);
89 void queueData(const std::string& data) override;
90 std::string toString() const override
92 return d_remote.toStringWithPort();
98 std::atomic<uint32_t> d_drops{0};
102 void maintenanceThread();
104 ComboAddress d_remote;
105 uint64_t d_maxQueuedBytes;
107 std::unique_ptr<CircularWriteBuffer> d_writer;
109 uint8_t d_reconnectWaitTime;
110 std::atomic<bool> d_exiting{false};
111 bool d_asyncConnect{false};
114 std::thread d_thread;