File: taskqueue.hh

package info (click to toggle)
pdns-recursor 4.8.8-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,620 kB
  • sloc: cpp: 95,714; javascript: 20,651; sh: 4,679; makefile: 652; xml: 37
file content (138 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download
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
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#pragma once

#include <sys/time.h>
#include <thread>

union ComboAddress;
namespace boost
{
size_t hash_value(const ComboAddress&);
}

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/tag.hpp>

#include "dnsname.hh"
#include "iputils.hh"
#include "qtype.hh"

namespace pdns
{
using namespace ::boost::multi_index;

// ATM we have one task type, if we get more, the unique key in the index needs to be adapted
struct ResolveTask
{
  DNSName d_qname;
  uint16_t d_qtype;
  // Deadline is not part of index and not used by operator<()
  time_t d_deadline;
  // Whether to run this task in regular mode (false) or in the mode that refreshes almost expired tasks
  bool d_refreshMode;
  // Use a function pointer as comparing std::functions is a nuisance
  using TaskFunction = void (*)(const struct timeval& now, bool logErrors, const ResolveTask& task);
  TaskFunction d_func;
  // IP used by DoT probe tasks
  ComboAddress d_ip;
  // NS name used by DoT probe task, not part of index and not used by operator<()
  DNSName d_nsname;
  Netmask d_netmask;

  bool operator<(const ResolveTask& a) const
  {
    return std::tie(d_qname, d_qtype, d_refreshMode, d_func, d_ip, d_netmask) < std::tie(a.d_qname, a.d_qtype, a.d_refreshMode, a.d_func, a.d_ip, a.d_netmask);
  }

  bool run(bool logErrors);
};

class TaskQueue
{
public:
  bool empty() const
  {
    return d_queue.empty();
  }

  size_t size() const
  {
    return d_queue.size();
  }

  bool push(ResolveTask&& task);
  ResolveTask pop();

  uint64_t getPushes()
  {
    return d_pushes;
  }

  uint64_t getExpired()
  {
    return d_expired;
  }

  void incExpired()
  {
    d_expired++;
  }

  void clear()
  {
    d_queue.clear();
  }

private:
  struct HashTag
  {
  };

  struct SequencedTag
  {
  };

  typedef multi_index_container<
    ResolveTask,
    indexed_by<
      ordered_unique<tag<HashTag>,
                     composite_key<ResolveTask,
                                   member<ResolveTask, DNSName, &ResolveTask::d_qname>,
                                   member<ResolveTask, uint16_t, &ResolveTask::d_qtype>,
                                   member<ResolveTask, bool, &ResolveTask::d_refreshMode>,
                                   member<ResolveTask, ResolveTask::TaskFunction, &ResolveTask::d_func>,
                                   member<ResolveTask, ComboAddress, &ResolveTask::d_ip>,
                                   member<ResolveTask, Netmask, &ResolveTask::d_netmask>>>,
      sequenced<tag<SequencedTag>>>>
    queue_t;

  queue_t d_queue;
  uint64_t d_pushes{0};
  uint64_t d_expired{0};
};

}