Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
added yaml address book example, wrapped some potentially shared acce…
Browse files Browse the repository at this point in the history
…ss calls with a mutex. Updated csv address book to actually read from a csv
  • Loading branch information
brianmario committed Nov 18, 2008
1 parent 9277e7f commit d1136a3
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 11 deletions.
15 changes: 12 additions & 3 deletions address_books/csv_address_book.rb
@@ -1,10 +1,19 @@
require 'singleton'
require 'thread'
require 'csv'

class AddressBook
include Singleton

def initialize
@servers = [
{:host => 'localhost', :port => 80}
]
@servers = []
@mutex = Mutex.new
end

def configure(config)
CSV.open(config['file']) do |row|
@servers << {:host => row[1], :port => row[2].to_i}
end
end

# this method should return an array of hosts which this request is qualified to connect to.
Expand Down
25 changes: 25 additions & 0 deletions address_books/yaml_address_book.rb
@@ -0,0 +1,25 @@
require 'singleton'
require 'thread'
require 'yaml'

class AddressBook
include Singleton

def initialize
@servers = []
@mutex = Mutex.new
end

def configure(config)
servers = YAML.load_file(config['file'])
servers.each do |server|
@servers << server
end
end

# this method should return an array of hosts which this request is qualified to connect to.
# which is determined based on the contents of the data passed
def lookup_addresses(data)
return @servers
end
end
7 changes: 5 additions & 2 deletions config.yml
@@ -1,10 +1,13 @@
panel:
name: eventmachine
name: rev
max_connections: 1024
options:
addr: 0.0.0.0
port: 8000
operator:
name: random
options:
address_book:
name: csv
name: csv
options:
file: "examples/servers.csv"
1 change: 1 addition & 0 deletions examples/servers.csv
@@ -0,0 +1 @@
4.0,localhost,80
3 changes: 3 additions & 0 deletions examples/servers.yml
@@ -0,0 +1,3 @@
---
- :host: localhost
:port: 80
15 changes: 13 additions & 2 deletions operators/random_operator.rb
@@ -1,12 +1,23 @@
require 'singleton'
require 'thread'

class Operator
include Singleton

def initialize
@address_book = AddressBook.new
@address_book = AddressBook.instance
@mutex = Mutex.new
end

def configure(config)
end

# the purpose of this method is to return a *single* host for the backend connection
# which is determined by this operators method of load-balancing
def lookup_jack(data)
addresses = @address_book.lookup_addresses(data)
@mutex.synchronize {
addresses = @address_book.lookup_addresses(data)
}
return addresses[rand(addresses.size)] unless addresses.nil?
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion panels/eventmachine_panel.rb
Expand Up @@ -63,7 +63,7 @@ class BrowserRequest < EventMachine::Connection
attr_reader :backend

def initialize
@operator = Operator.new
@operator = Operator.instance
super
end

Expand Down
14 changes: 12 additions & 2 deletions panels/rev_panel.rb
Expand Up @@ -51,7 +51,7 @@ class BrowserRequest < Rev::TCPSocket
attr_reader :backend

def initialize(*args)
@operator = Operator.new
@operator = Operator.instance
super
end

Expand Down Expand Up @@ -111,11 +111,21 @@ def self.start(options)

trap ('SIGHUP') {
LOGGER.warn 'Hangup caught, restarting'
# TODO: reload config
# tell AddressBook and Operator to reconfigure themselves based on the "new" config
}

server = Rev::TCPServer.new(options['host'], options['port'], BrowserRequest)
server.attach(@@rev_loop)


# timer = Rev::TimerWatcher.new(5, true)
# timer.instance_eval do
# def on_timer
# LOGGER.info("This will check for failed backends, and attempt to bring them back online")
# end
# end
# timer.attach(@@rev_loop)

LOGGER.info "Rev panel listening on #{options['host']}:#{options['port']}"
@@rev_loop.run
end
Expand Down
2 changes: 1 addition & 1 deletion panels/threaded_panel.rb
Expand Up @@ -39,7 +39,7 @@ class BrowserRequest
attr_reader :backend

def initialize(sock)
@operator = Operator.new
@operator = Operator.instance

@socket = sock
LOGGER.info "#{@socket.peeraddr[3]}:#{@socket.peeraddr[1]} connected"
Expand Down
2 changes: 2 additions & 0 deletions switchboard.rb
Expand Up @@ -18,6 +18,8 @@
LOGGER.info("Max connections to: #{CONFIG['panel']['max_connections'].to_i}")

begin
AddressBook.instance.configure(CONFIG['address_book']['options'])
Operator.instance.configure(CONFIG['operator']['options'])
Panel.start(CONFIG['panel']['options'])
rescue Exception => e
LOGGER.fatal e.inspect
Expand Down

0 comments on commit d1136a3

Please sign in to comment.