|
3 | 3 | # Released under the MIT License. |
4 | 4 | # Copyright, 2025, by Samuel Williams. |
5 | 5 |
|
6 | | -require "io/stream" |
7 | 6 | require_relative "connection" |
| 7 | +require_relative "dispatchable" |
8 | 8 |
|
9 | 9 | module Async |
10 | 10 | module Container |
11 | 11 | module Supervisor |
12 | 12 | class Client |
13 | | - def self.run(...) |
14 | | - self.new(...).run |
| 13 | + def initialize(endpoint: Supervisor.endpoint) |
| 14 | + @endpoint = endpoint |
15 | 15 | end |
16 | 16 |
|
17 | | - def initialize(instance, endpoint = Supervisor.endpoint) |
18 | | - @instance = instance |
19 | | - @endpoint = endpoint |
| 17 | + include Dispatchable |
| 18 | + |
| 19 | + protected def connect! |
| 20 | + peer = @endpoint.connect |
| 21 | + return Connection.new(peer, 0) |
20 | 22 | end |
21 | 23 |
|
22 | | - def dispatch(call) |
23 | | - method_name = "do_#{call.message[:do]}" |
24 | | - self.public_send(method_name, call) |
| 24 | + # Called when a connection is established. |
| 25 | + protected def connected!(connection) |
| 26 | + # Do nothing by default. |
25 | 27 | end |
26 | 28 |
|
| 29 | + # Connect to the server. |
27 | 30 | def connect |
28 | | - unless @connection |
29 | | - peer = @endpoint.connect |
30 | | - stream = IO::Stream(peer) |
31 | | - @connection = Connection.new(stream, 0, instance: @instance) |
32 | | - |
33 | | - # Register the instance with the server: |
34 | | - Async do |
35 | | - @connection.call(do: :register, state: @instance) |
36 | | - end |
37 | | - end |
| 31 | + connection = connect! |
| 32 | + connection.run_in_background(self) |
38 | 33 |
|
39 | | - return @connection unless block_given? |
| 34 | + connected!(connection) |
| 35 | + |
| 36 | + return connection unless block_given? |
40 | 37 |
|
41 | 38 | begin |
42 | | - yield @connection |
| 39 | + yield connection |
43 | 40 | ensure |
44 | | - @connection.close |
45 | | - end |
46 | | - end |
47 | | - |
48 | | - def close |
49 | | - if connection = @connection |
50 | | - @connection = nil |
51 | 41 | connection.close |
52 | 42 | end |
53 | 43 | end |
54 | 44 |
|
55 | | - private def dump(call) |
56 | | - if path = call[:path] |
57 | | - File.open(path, "w") do |file| |
58 | | - yield file |
59 | | - end |
60 | | - |
61 | | - call.finish(path: path) |
62 | | - else |
63 | | - buffer = StringIO.new |
64 | | - yield buffer |
65 | | - |
66 | | - call.finish(data: buffer.string) |
67 | | - end |
68 | | - end |
69 | | - |
70 | | - def do_scheduler_dump(call) |
71 | | - dump(call) do |file| |
72 | | - Fiber.scheduler.print_hierarchy(file) |
73 | | - end |
74 | | - end |
75 | | - |
76 | | - def do_memory_dump(call) |
77 | | - require "objspace" |
78 | | - |
79 | | - dump(call) do |file| |
80 | | - ObjectSpace.dump_all(output: file) |
81 | | - end |
82 | | - end |
83 | | - |
84 | | - def do_thread_dump(call) |
85 | | - dump(call) do |file| |
86 | | - Thread.list.each do |thread| |
87 | | - file.puts(thread.inspect) |
88 | | - file.puts(thread.backtrace) |
89 | | - end |
90 | | - end |
91 | | - end |
92 | | - |
93 | | - def do_garbage_profile_start(call) |
94 | | - GC::Profiler.enable |
95 | | - call.finish(started: true) |
96 | | - end |
97 | | - |
98 | | - def do_garbage_profile_stop(call) |
99 | | - GC::Profiler.disable |
100 | | - |
101 | | - dump(connection, message) do |file| |
102 | | - file.puts GC::Profiler.result |
103 | | - end |
104 | | - end |
105 | | - |
| 45 | + # Run the client in a loop, reconnecting if necessary. |
106 | 46 | def run |
107 | | - Async do |task| |
| 47 | + Async do |
108 | 48 | loop do |
109 | | - connect do |connection| |
110 | | - connection.run(self) |
| 49 | + connection = connect! |
| 50 | + |
| 51 | + Async do |
| 52 | + connected!(connection) |
111 | 53 | end |
112 | | - rescue => error |
113 | | - Console.error(self, "Unexpected error while running client!", exception: error) |
114 | 54 |
|
115 | | - # Retry after a small delay: |
| 55 | + connection.run(self) |
| 56 | + rescue => error |
| 57 | + Console.error(self, "Connection failed:", exception: error) |
116 | 58 | sleep(rand) |
| 59 | + ensure |
| 60 | + connection.close |
117 | 61 | end |
118 | | - ensure |
119 | | - task.stop |
120 | 62 | end |
121 | 63 | end |
122 | 64 | end |
|
0 commit comments