44# Copyright, 2025, by Samuel Williams.
55
66require "io/stream"
7- require_relative "wrapper "
7+ require_relative "connection "
88
99module Async
1010 module Container
@@ -20,46 +20,86 @@ def initialize(instance, endpoint = Supervisor.endpoint)
2020 end
2121
2222 def connect
23- unless @wrapper
23+ unless @connection
2424 peer = @endpoint . connect
2525 stream = IO ::Stream ( peer )
26- @wrapper = Wrapper . new ( stream )
26+ @connection = Connection . new ( stream , 0 , instance : @instance )
2727
28- @wrapper . write ( action : "register" , instance : @instance )
28+ # Register the instance with the server:
29+ @connection . call ( do : :register , state : @instance )
2930 end
3031
31- return @wrapper unless block_given?
32+ return @connection unless block_given?
3233
3334 begin
34- yield @wrapper
35+ yield @connection
3536 ensure
36- @wrapper . close
37+ @connection . close
3738 end
3839 end
3940
4041 def close
41- if wrapper = @wrapper
42- @wrapper = nil
43- wrapper . close
42+ if connection = @connection
43+ @connection = nil
44+ connection . close
4445 end
4546 end
4647
47- def do_memory_dump ( wrapper , message )
48- Console . info ( self , "Memory dump:" , message )
49- path = message [ :path ]
50-
51- File . open ( path , "w" ) do |file |
48+ private def dump ( call )
49+ if path = message [ :path ]
50+ File . open ( path , "w" ) do |file |
51+ yield file
52+ end
53+
54+ call . finish ( path : path )
55+ else
56+ buffer = StringIO . new
57+ yield buffer
58+
59+ call . finish ( data : buffer . string )
60+ end
61+ end
62+
63+ def do_scheduler_dump ( call )
64+ dump ( call ) do |file |
65+ Fiber . scheduler . print_hierarchy ( file )
66+ end
67+ end
68+
69+ def do_memory_dump ( call )
70+ dump ( call ) do |file |
5271 ObjectSpace . dump_all ( output : file )
5372 end
5473 end
5574
75+ def do_thread_dump ( call )
76+ dump ( call ) do |file |
77+ Thread . list . each do |thread |
78+ file . puts ( thread . inspect )
79+ file . puts ( thread . backtrace )
80+ end
81+ end
82+ end
83+
84+ def do_garbage_profile_start ( connection , message )
85+ Console . info ( self , "Garbage profile start:" , message )
86+ GC ::Profiler . enable
87+ end
88+
89+ def do_garbage_profile_stop ( connection , message )
90+ Console . info ( self , "Garbage profile stop:" , message )
91+ GC ::Profiler . disable
92+
93+ dump ( connection , message ) do |file |
94+ file . puts GC ::Profiler . result
95+ end
96+ end
97+
5698 def run
5799 Async do |task |
58100 loop do
59- Console . info ( self , "Connecting to supervisor..." )
60- connect do |wrapper |
61- Console . info ( self , "Connected to supervisor." )
62- wrapper . run ( self )
101+ connect do |connection |
102+ connection . run ( self )
63103 end
64104 rescue => error
65105 Console . error ( self , "Unexpected error while running client!" , exception : error )
0 commit comments