Class: Lich::Gemstone::Combat::AsyncProcessor
- Inherits:
-
Object
- Object
- Lich::Gemstone::Combat::AsyncProcessor
- Defined in:
- documented/gemstone/combat/async_processor.rb
Overview
Single-threaded async processor for combat chunks.
Queues game-stream lines from a downstream hook and processes them on a dedicated worker thread, preserving arrival order. This ordering is required for status add/remove, UCS updates, and damage calculations to work correctly. The single worker thread also guarantees that Creature/CreatureInstance objects are mutated from only one thread, eliminating the need for synchronization in creature logic.
The worker thread survives script exit because it is spawned from the game-stream thread, not the script context—allowing long-running processors to remain active across script reloads.
Instance Method Summary collapse
-
#initialize(_max_threads = 1) ⇒ AsyncProcessor
constructor
max_threads retained for call-site compatibility; processing is intentionally single-threaded to preserve event ordering.
-
#process_async(chunk, source: nil) ⇒ nil
Enqueue a chunk; O(1), never blocks the game stream.
-
#shutdown ⇒ Object
Drain remaining work and stop the worker.
-
#stats ⇒ Hash
Returns statistics about the async processor queue and worker.
Constructor Details
#initialize(_max_threads = 1) ⇒ AsyncProcessor
max_threads retained for call-site compatibility; processing is intentionally single-threaded to preserve event ordering.
37 38 39 40 41 42 43 |
# File 'documented/gemstone/combat/async_processor.rb', line 37 def initialize(_max_threads = 1) @queue = Queue.new @processing = false @chunks_processed = 0 @spawn_mutex = Mutex.new ensure_worker end |
Instance Method Details
#process_async(chunk, source: nil) ⇒ nil
Enqueue a chunk; O(1), never blocks the game stream.
Also revives the worker if it died: a worker spawned from a script context (enable! via autostart/;e) belongs to that script's thread group and is killed when the script exits. process_async runs on the downstream-hook (game) thread, so a worker respawned here survives script death.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'documented/gemstone/combat/async_processor.rb', line 56 def process_async(chunk, source: nil) return if chunk.empty? if source.is_a?(Hash) source = source.slice(:connection_id, :game, :character, :room_epoch, :sequence, :received_at) .transform_values { |value| value.is_a?(String) ? value.dup.freeze : value }.freeze else source = nil end @queue.push([chunk, source]) ensure_worker nil end |
#shutdown ⇒ Object
Drain remaining work and stop the worker.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'documented/gemstone/combat/async_processor.rb', line 70 def shutdown respond "[Combat] Waiting for #{@queue.size} queued chunks..." if Tracker.debug?(:verbose) @queue.push(:shutdown) @worker.join # Force GC after shutdown to help with memory fragmentation. # Compaction is routed through Lich::Util::GtkCompaction, which # keeps it safe to use alongside gtk3. GC.start Lich::Util::GtkCompaction.safe_compact! end |
#stats ⇒ Hash
Returns statistics about the async processor queue and worker.
88 89 90 91 92 93 94 95 |
# File 'documented/gemstone/combat/async_processor.rb', line 88 def stats { active: @processing ? 1 : 0, queued: @queue.size, total: @chunks_processed, worker_alive: !@worker.nil? && @worker.alive? } end |