Class: Takagi::Controller::ResourceAllocator

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/controller/resource_allocator.rb

Overview

Allocates thread pool resources to controllers

Supports two modes:

  1. Manual: Controllers specify exact thread counts via profiles

  2. Automatic: Divides global pool among controllers based on weights

Examples:

Manual allocation

ResourceAllocator.allocate(
  controllers: [IngressController, ConfigController],
  mode: :manual
)
# IngressController gets profile's thread count (30)
# ConfigController gets profile's thread count (2)

Automatic allocation

ResourceAllocator.allocate(
  controllers: [IngressController, ConfigController, TelemetryController],
  mode: :automatic,
  total_threads: 40
)
# Divides 40 threads proportionally based on profile weights

Constant Summary collapse

PROFILE_WEIGHTS =

Profile weights for automatic allocation Higher weight = more resources

{
  minimal: 1,
  low_traffic: 2,
  long_lived: 8,
  high_throughput: 16,
  large_payloads: 4,
  custom: 4
}.freeze

Class Method Summary collapse

Class Method Details

.allocate(controllers:, mode: :automatic, total_threads: nil, protocol: :tcp) ⇒ Hash

Allocate thread pool resources to controllers

Examples:

allocations = ResourceAllocator.allocate(
  controllers: [IngressController, ConfigController],
  mode: :automatic,
  total_threads: 40
)
# => {
#   IngressController => { threads: 30, processes: nil },
#   ConfigController => { threads: 10, processes: nil }
# }

Parameters:

  • controllers (Array<Class>)

    Controller classes to allocate to

  • mode (Symbol) (defaults to: :automatic)

    :manual or :automatic

  • total_threads (Integer) (defaults to: nil)

    Total threads available (for automatic mode)

  • protocol (Symbol) (defaults to: :tcp)

    :udp or :tcp (affects process allocation)

Returns:

  • (Hash)

    Map of controller class => allocation hash



57
58
59
60
61
62
63
64
65
66
# File 'lib/takagi/controller/resource_allocator.rb', line 57

def allocate(controllers:, mode: :automatic, total_threads: nil, protocol: :tcp)
  case mode
  when :manual
    allocate_manual(controllers, protocol: protocol)
  when :automatic
    allocate_automatic(controllers, total_threads: total_threads, protocol: protocol)
  else
    raise ArgumentError, "Unknown allocation mode: #{mode}. Use :manual or :automatic"
  end
end

.validate!(allocations, total_threads:) ⇒ Object

Validate that allocations don’t exceed available resources

Parameters:

  • allocations (Hash)

    Controller => allocation map

  • total_threads (Integer)

    Total available threads

Raises:

  • (ArgumentError)

    if allocations exceed resources



73
74
75
76
77
78
79
80
81
82
# File 'lib/takagi/controller/resource_allocator.rb', line 73

def validate!(allocations, total_threads:)
  allocated = allocations.values.sum { |alloc| alloc[:threads] || 0 }

  if allocated > total_threads
    raise ArgumentError,
          "Controller thread allocations (#{allocated}) exceed available threads (#{total_threads})"
  end

  allocations
end