ractor_dns 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f04318c7ff68a828b56c907c840838eaf2db6bc713af5954d200a490a1fba78
4
- data.tar.gz: 3d63843ff098ab3de850f6a382b8b751045587c1e73001e939ccdb8abac09fdf
3
+ metadata.gz: f0b9397d08074bb1438ea182c5ff2087b56718ebd9cbe18b65005c524a392e06
4
+ data.tar.gz: 4da6a40911f678aefd63ba7619fac3039a69ec320433a3a7c3ead4876974fe68
5
5
  SHA512:
6
- metadata.gz: 5890c21087d4bfb40e99f6aad33b8e86bb3074ca2db3fd9eec968e7e6930b1dd60d96bad037a9c96a95a1faeb42e8e76b95f7231613bb3783254485c5d2241fc
7
- data.tar.gz: 9c0536c6a6910ac3ad69b56bda6740cbf372e186abf86b8164d832aa9314443f1afc0f9c18f196ae9712bd05a42983e58c962534fdd31aa85592fc13e8e7849d
6
+ metadata.gz: 071aaf6b4cf3fee5a7c1270cce98e96f4bbaa781cd8da1c1d4cb9ddc6e006116938e9637ecc452b6f5e95ff19f4b25b9cda805ad482091234cc6cef28373bf5b
7
+ data.tar.gz: a35e0277b777380d862f45d3d9b9e21745d751c4b7af1828b3cbcb079708e423e1efcd5d45eed2c00af2b9ca4919ab2d415d9982fd652e7559d476f31ee4baf8
@@ -1,7 +1,5 @@
1
1
  class RactorDNS::ActiveZone::Railtie < Rails::Railtie
2
- server do
3
- if ActiveZone.provider.is_a? RactorDNS::ActiveZone
4
- ActiveZone.provider.start(here: true)
5
- end
2
+ config.to_prepare do
3
+ ActiveZone.provider.start if ActiveZone.provider.is_a? RactorDNS::ActiveZone
6
4
  end
7
5
  end
@@ -0,0 +1,25 @@
1
+ class RactorDNS::ActiveZone::Zone < ActiveZone::Provider::Zone
2
+ def persist!
3
+ t = RactorDNS::Transaction.new do |zc|
4
+ zc[name] = resources.map(&:to_h)
5
+ end
6
+
7
+ provider.zones.transact t
8
+
9
+ @persisted = true
10
+ changes_applied
11
+ end
12
+
13
+ def delete!
14
+ t = RactorDNS::Transaction.new do |zc|
15
+ zc.delete(name)
16
+ end
17
+ provider.zones.transact t
18
+ @persisted = false
19
+ freeze
20
+ end
21
+
22
+ def provider
23
+ @@provider
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ class RactorDNS::ActiveZone::ZoneCollection < ActiveZone::Provider::ZoneCollection
2
+ def find(name)
3
+ RactorDNS::ActiveZone::Zone.new(resources: provider.zones[name], name: name, persisted: true) if provider.zones[name]
4
+ end
5
+
6
+ def each(&block)
7
+ if block_given?
8
+ provider.zones.to_h.keys.filter { |el| @set ? @set.include?(el.name) : true }.each do |zk|
9
+ block.call(RactorDNS::ActiveZone::Zone.new(resources: provider.zones[zk], name: zk, persisted: true))
10
+ end
11
+ else
12
+ to_enum(:each)
13
+ end
14
+ end
15
+
16
+ def destroy_all
17
+ each do |z|
18
+ z.destroy
19
+ end
20
+ end
21
+
22
+ def find_by(attributes = {})
23
+ each do |z|
24
+ broken = false
25
+ attributes.each do |k, v|
26
+ if v != z[k]
27
+ broken = true
28
+ break
29
+ end
30
+ end
31
+ return z if !broken
32
+ end
33
+ end
34
+
35
+ def [](n)
36
+ RactorDNS::ActiveZone::Zone.new(resources: provider.zones.to_h.values[n], persisted: true, name: provider.zones.to_h.keys[n])
37
+ end
38
+ end
@@ -1,4 +1,4 @@
1
- class RactorDNS::ActiveZone
1
+ class RactorDNS::ActiveZone < ActiveZone::Provider
2
2
  attr_accessor :server
3
3
  def initialize(name: "ns.ractordns.local", ttl: 300, ip: "0.0.0.0", server: nil, port: 8053, cpu_count: 8, zones:)
4
4
  @server = server.presence || RactorDNS::Server.new(
@@ -12,4 +12,7 @@ class RactorDNS::ActiveZone
12
12
  delegate_missing_to :server
13
13
  end
14
14
 
15
- require "ractor_dns/active_zone/railtie" if defined?(Rails::Railtie)
15
+ require "ractor_dns/active_zone/railtie" if defined?(Rails::Railtie)
16
+
17
+ require_relative "active_zone/zone" if defined?(ActiveZone::Provider)
18
+ require_relative "active_zone/zone_collection" if defined?(ActiveZone::Provider)
@@ -1,15 +1,18 @@
1
1
  module RactorDNS
2
2
  class Server
3
- attr_reader :authority, :ractor, :zones, :cpu_count, :port
3
+ attr_reader :authority, :ractor, :zones, :cpu_count, :port, :started
4
4
 
5
5
  def initialize(authority:, cpu_count:, zones:, port: 8053)
6
6
  @authority = authority
7
7
  @cpu_count = cpu_count
8
8
  @zones = ZoneCollection.new(zones)
9
9
  @port = port
10
+ @started = false
11
+ @threads = []
10
12
  end
11
13
 
12
14
  def start(here = false)
15
+ return if @started
13
16
  @pipe = Ractor.new do
14
17
  loop do
15
18
  recv = Ractor.recv
@@ -23,10 +26,10 @@ module RactorDNS
23
26
  end
24
27
  end
25
28
 
26
- @resolvers = @cpu_count.times.map do
29
+ @resolvers = (@cpu_count ^ 2).times.map do
27
30
  Ractor.new(@pipe, @authority) do |pipe, authority|
28
31
  loop do
29
- data, zone_h = pipe.take
32
+ data, zone_h = Ractor.recv
30
33
 
31
34
  break if data == :stop && zone_h.nil?
32
35
 
@@ -49,42 +52,49 @@ module RactorDNS
49
52
  end
50
53
  end
51
54
  end
52
-
53
- @endpoint = Async::IO::Endpoint.udp('0.0.0.0', @port)
54
55
 
55
56
  @zones.start
56
-
57
+
57
58
  if here
59
+
58
60
  start_main_loop
59
61
  else
60
- @thread = Thread.new do
61
- start_main_loop
62
+ @threads = @cpu_count.times.map do |i|
63
+ Thread.new do
64
+ start_main_loop
65
+ end
62
66
  end
63
67
  end
64
68
 
65
69
  at_exit do
66
70
  stop
67
71
  end
72
+
73
+ @started = true
68
74
  end
69
75
 
70
76
  def stop
71
77
  begin
72
- @thread.kill
73
- @pipe.send(:stop)
74
- @zones.stop
78
+ @threads.each do |t|
79
+ t.kill
80
+ end
81
+ @pipe&.send(:stop)
82
+ @zones&.stop
75
83
  rescue Ractor::ClosedError
76
84
  nil
77
85
  end
78
86
  end
79
87
 
80
- def start_main_loop
88
+ def start_main_loop(i = rand(0...@resolvers.length))
81
89
  Async do
82
- @endpoint.bind do |socket|
83
- loop do
84
- data, address = socket.recvfrom(1024)
85
- @pipe.send([data, @zones.freeze])
86
- _, encoded = Ractor.select(*@resolvers)
87
- socket.send(encoded, 0, address)
90
+ @cpu_count.times do
91
+ Async::IO::Endpoint.udp('0.0.0.0', @port).bind do |socket|
92
+ loop do
93
+ data, address = socket.recvfrom(1024)
94
+ @resolvers[i].send([data, @zones.freeze])
95
+ encoded = @resolvers[i].take
96
+ socket.send(encoded, 0, address)
97
+ end
88
98
  end
89
99
  end
90
100
  end
@@ -15,10 +15,10 @@ module RactorDNS
15
15
  rescue => e
16
16
  puts e
17
17
  rollback(obj)
18
- raise e
19
18
  end
20
19
 
21
20
  def rollback(obj)
21
+ puts "rolling back!"
22
22
  obj.replace(@original_state) if @original_state
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module RactorDNS
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -2,7 +2,6 @@ module RactorDNS
2
2
  class ZoneCollection
3
3
  def initialize(obj = {})
4
4
  @obj = obj
5
- start
6
5
  end
7
6
 
8
7
  def start
@@ -62,6 +61,9 @@ module RactorDNS
62
61
  end
63
62
 
64
63
  def transact(transaction)
64
+ if !transaction.is_a? RactorDNS::Transaction
65
+ raise "#{transaction.to_s} is not a transaction"
66
+ end
65
67
  @queue.send([:transact, Ractor.make_shareable(transaction)])
66
68
  @queue.take
67
69
  end
@@ -73,6 +75,16 @@ module RactorDNS
73
75
  def stop
74
76
  @queue.send([:stop, nil])
75
77
  end
78
+
79
+ def inspect
80
+ begin
81
+ to_h.inspect
82
+ rescue
83
+ "Unable to inspect, try starting the server"
84
+ end
85
+ end
86
+
87
+ alias_method :to_s, :inspect
76
88
  end
77
89
 
78
90
  class FrozenZoneCollection
@@ -88,10 +100,16 @@ module RactorDNS
88
100
  if name.nil?
89
101
  return nil
90
102
  end
103
+
91
104
  if name.is_a? RRs::Name
92
105
  name = name.to_a
93
106
  end
94
- @obj.dig(name.join(".")).presence || search(name.drop(1))
107
+
108
+ if name.length > 1
109
+ @obj.dig(name.join(".")).presence || search(name.drop(1))
110
+ else
111
+ @obj.dig(name)
112
+ end
95
113
  end
96
114
  end
97
115
  end
data/lib/ractor_dns.rb CHANGED
@@ -13,8 +13,9 @@ require "async/io/udp_socket"
13
13
 
14
14
  loader = Zeitwerk::Loader.for_gem
15
15
  loader.inflector = RactorDNS::Inflector.new
16
- loader.ignore("#{__dir__}/ractor_dns/active_zone.rb")
17
- loader.ignore("#{__dir__}/ractor_dns/active_zone/**/*")
16
+ loader.ignore("#{__dir__}/ractor_dns/active_zone.rb") if !defined?(ActiveZone)
17
+ loader.ignore("#{__dir__}/ractor_dns/active_zone/**/*") if !defined?(ActiveZone)
18
+ loader.ignore("#{__dir__}/ractor_dns/active_zone/railtie.rb") if !defined?(Rails::Railtie)
18
19
  loader.ignore("#{__dir__}/ractor_dns/railtie.rb")
19
20
  loader.ignore("#{__dir__}/ractor_dns/version.rb")
20
21
  loader.setup
@@ -23,6 +24,3 @@ module RactorDNS
23
24
  class Error < StandardError; end
24
25
  end
25
26
 
26
- loader.eager_load
27
-
28
- require "ractor_dns/active_zone" if defined?(ActiveZone)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ractor_dns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - reesericci
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-20 00:00:00.000000000 Z
11
+ date: 2024-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -81,6 +81,8 @@ files:
81
81
  - lib/ractor_dns.rb
82
82
  - lib/ractor_dns/active_zone.rb
83
83
  - lib/ractor_dns/active_zone/railtie.rb
84
+ - lib/ractor_dns/active_zone/zone.rb
85
+ - lib/ractor_dns/active_zone/zone_collection.rb
84
86
  - lib/ractor_dns/inflector.rb
85
87
  - lib/ractor_dns/server.rb
86
88
  - lib/ractor_dns/transaction.rb