Class: Falcon::CompositeServer

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/composite_server.rb

Overview

A composite server that manages multiple Server instances.

This class coordinates running multiple server instances concurrently, allowing you to:

  • Serve different applications on different endpoints.

  • Use different configurations per server.

  • Access statistics for each named server.

  • Control all servers as a single unit.

Examples:

Create and run several applications.

require 'falcon/composite_server'
require 'falcon/server'

# Create individual servers
servers = {
  "http" => Falcon::Server.new(app1, endpoint1),
  "https" => Falcon::Server.new(app2, endpoint2)
}

# Create the composite server
composite = Falcon::CompositeServer.new(servers)

# Run all servers
composite.run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers) ⇒ CompositeServer

Initialize a composite server with the given server instances.



36
37
38
# File 'lib/falcon/composite_server.rb', line 36

def initialize(servers)
  @servers = servers
end

Instance Attribute Details

#serversObject (readonly)

The individual server instances mapped by name.



42
43
44
# File 'lib/falcon/composite_server.rb', line 42

def servers
  @servers
end

Instance Method Details

#detailed_statisticsObject

Get detailed statistics for each server.



73
74
75
# File 'lib/falcon/composite_server.rb', line 73

def detailed_statistics
  @servers.transform_values(&:statistics_string)
end

#runObject

Run the composite server, starting all individual servers.

This method should be called within an Async context. It will run all server instances concurrently.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/falcon/composite_server.rb', line 49

def run
  Async do |task|
    # Run each server - server.run creates its own Async block internally
    @servers.each do |name, server|
      server.run
    end
    
    # Wait for all child tasks to complete
    task.children.each(&:wait)
  end
end

#statistics_stringObject

Generates a human-readable string representing statistics for all servers.



64
65
66
67
68
# File 'lib/falcon/composite_server.rb', line 64

def statistics_string
  @servers.map do |name, server|
    "#{name}: #{server.statistics_string}"
  end.join(", ")
end