Class: Lich::Gemstone::Combat::AsyncProcessor

Inherits:
Object
  • Object
show all
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.

See Also:

Instance Method Summary collapse

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.

Parameters:

  • chunk (Array<String>) —

    game lines to process in arrival order

  • source (Hash, nil) (defaults to: nil) —

    optional ingestion context; permitted scalar fields are copied before queueing and validated by Processor

Returns:

  • (nil) —

    after enqueueing, or when the chunk is empty



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.

Examples:

processor.stats #=> {active: 0, queued: 2, total: 150, worker_alive: true}

Returns:

  • (Hash) —

    with keys :active (0 or 1), :queued (number of pending chunks), :total (count of processed chunks since startup), and :worker_alive (boolean)



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