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

Commit

Permalink
moved mutex handlers to be class-variables for more global use. updat…
Browse files Browse the repository at this point in the history
…ed rev panel so ctrl+c capture properly exits the process as well. updated logging in a couple of places
  • Loading branch information
brianmario committed Nov 18, 2008
1 parent f2e90a5 commit b2613b0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
8 changes: 5 additions & 3 deletions address_books/csv_address_book.rb
Expand Up @@ -5,17 +5,19 @@
class AddressBook
include Singleton

@@mutex = Mutex.new

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

def configure(config)
@mutex.synchronize {
CSV.open(config['file']) do |row|
@@mutex.synchronize {
CSV.open(config['file'], 'r') do |row|
@servers << {:host => row[1], :port => row[2].to_i}
end
}
LOGGER.debug("AddressBook configured: #{@servers.size} server(s)")
end

# this method should return an array of hosts which this request is qualified to connect to.
Expand Down
6 changes: 2 additions & 4 deletions address_books/sqlite3_address_book.rb
Expand Up @@ -5,9 +5,7 @@
class AddressBook
include Singleton

def initialize
@mutex = Mutex.new
end
@@mutex = Mutex.new

def configure(config)
@db = SQLite3::Database.new(config['database'])
Expand All @@ -22,7 +20,7 @@ def lookup_addresses(data)
# req_host = data['host']
# @db.execute(@query, req_host)
servers = []
@mutex.synchronize {
@@mutex.synchronize {
@db.execute(@query) do |row|
servers << {:host => row[0], :port => row[1]}
end
Expand Down
3 changes: 2 additions & 1 deletion address_books/yaml_address_book.rb
Expand Up @@ -5,9 +5,10 @@
class AddressBook
include Singleton

@@mutex = Mutex.new

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

def configure(config)
Expand Down
9 changes: 6 additions & 3 deletions operators/random_operator.rb
Expand Up @@ -4,21 +4,24 @@
class Operator
include Singleton

@@mutex = Mutex.new

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

def configure(config)
LOGGER.debug("Operator configured")
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)
@mutex.synchronize {
addresses = []
@@mutex.synchronize {
addresses = @address_book.lookup_addresses(data)
}
return addresses[rand(addresses.size)] unless addresses.nil?
return addresses[rand(addresses.size)] if addresses.any?
return nil
end
end
4 changes: 3 additions & 1 deletion panels/rev_panel.rb
Expand Up @@ -106,7 +106,9 @@ def self.start(options)

trap('INT') {
LOGGER.warn "ctrl+c caught, stopping server"
@rev_loop.stop
@@rev_loop.stop unless @@rev_loop.nil? # in case ctrl+c happens twice before we're exited
@@rev_loop = nil
return;
}

trap ('SIGHUP') {
Expand Down
1 change: 1 addition & 0 deletions switchboard.rb
Expand Up @@ -23,4 +23,5 @@
Panel.start(CONFIG['panel']['options'])
rescue Exception => e
LOGGER.fatal e.inspect
LOGGER.fatal e.backtrace.join("\r\n")
end

0 comments on commit b2613b0

Please sign in to comment.