Class: RactorDNS::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor_dns/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authority:, cpu_count:, zones:, port: 8053) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
12
# File 'lib/ractor_dns/server.rb', line 5

def initialize(authority:, cpu_count:, zones:, port: 8053)
  @authority = authority
  @cpu_count = cpu_count
  @zones = ZoneCollection.new(zones)
  @port = port
  @started = false
  @threads = []
end

Instance Attribute Details

#authorityObject (readonly)

Returns the value of attribute authority.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def authority
  @authority
end

#cpu_countObject (readonly)

Returns the value of attribute cpu_count.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def cpu_count
  @cpu_count
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def port
  @port
end

#ractorObject (readonly)

Returns the value of attribute ractor.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def ractor
  @ractor
end

#startedObject (readonly)

Returns the value of attribute started.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def started
  @started
end

#zonesObject (readonly)

Returns the value of attribute zones.



3
4
5
# File 'lib/ractor_dns/server.rb', line 3

def zones
  @zones
end

Instance Method Details

#start(here = false) ⇒ Object



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
# File 'lib/ractor_dns/server.rb', line 14

def start(here = false)
  return if @started
  @pipe = Ractor.new do
    loop do
      recv = Ractor.recv
      if recv == :stop
        Ractor.yield([recv, nil, nil])
        Ractor.current.close_incoming
        Ractor.current.close_outgoing
        break
      end
      Ractor.yield(recv)
    end
  end

  @resolvers = (@cpu_count ^ 2).times.map do
    Ractor.new(@pipe, @authority) do |pipe, authority|
      loop do
        data, zone_h = Ractor.recv

        break if data == :stop && zone_h.nil?

        decoded = RRs::Message.decode(data)

        response = RRs::Message.new(decoded.id)
        response.add_authority(*authority)
        response.instance_exec(decoded) do |decoded|
          @question.concat(decoded.question)
        end
        decoded.question.each do |q|
          (zone_h.search(q[0]) || []).filter { |el|
            el[:rr].class == q[1] && el[:name] == q[0].to_s
          }&.each do |ans|
            response.add_answer(ans[:name], ans[:ttl], ans[:rr])
          end
        end

        Ractor.yield(response.encode)
      end
    end
  end
  
  @zones.start
  
  if here
    
    start_main_loop
  else
    @threads = @cpu_count.times.map do |i|
      Thread.new do
        start_main_loop
      end
    end
  end

  at_exit do
    stop
  end
  
  @started = true
end

#start_main_loop(i = rand([email protected])) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ractor_dns/server.rb', line 88

def start_main_loop(i = rand(0...@resolvers.length))
  Async do
    @cpu_count.times do
      Async::IO::Endpoint.udp('0.0.0.0', @port).bind do |socket|
        loop do
          data, address = socket.recvfrom(1024)
          @resolvers[i].send([data, @zones.freeze])
          encoded = @resolvers[i].take
          socket.send(encoded, 0, address)
        end
      end
    end
  end
end

#stopObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ractor_dns/server.rb', line 76

def stop
  begin
    @threads.each do |t|
      t.kill
    end
    @pipe&.send(:stop)
    @zones&.stop
  rescue Ractor::ClosedError
    nil
  end
end