Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloud Foundry Engineer committed Apr 12, 2011
0 parents commit d66dc25
Show file tree
Hide file tree
Showing 17 changed files with 1,476 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source "http://rubygems.org"

gem 'bundler', '>= 1.0.10'
gem 'rake'
gem 'nats', '>= 0.4.8', :require => 'nats/client'
gem 'eventmachine', '~> 0.12.10'
gem "http_parser.rb", :require => "http/parser"
gem "yajl-ruby", :require => ["yajl", "yajl/json_gem"]

gem 'vcap_common', :path => '../common'

group :test do
gem "rspec"
gem "rcov"
gem "ci_reporter"
end
55 changes: 55 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
PATH
remote: ../common
specs:
vcap_common (0.99)
eventmachine (~> 0.12.10)
nats
thin
yajl-ruby

GEM
remote: http://rubygems.org/
specs:
builder (3.0.0)
ci_reporter (1.6.4)
builder (>= 2.1.2)
daemons (1.1.2)
diff-lcs (1.1.2)
eventmachine (0.12.10)
http_parser.rb (0.5.1)
json_pure (1.5.1)
nats (0.4.8)
daemons (>= 1.1.0)
eventmachine (>= 0.12.10)
json_pure (>= 1.5.1)
rack (1.2.2)
rake (0.8.7)
rcov (0.9.9)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
thin (1.2.11)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
yajl-ruby (0.8.2)

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.10)
ci_reporter
eventmachine (~> 0.12.10)
http_parser.rb
nats (>= 0.4.8)
rake
rcov
rspec
vcap_common!
yajl-ruby
55 changes: 55 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rake'

desc "Run specs"
task "spec" => ["bundler:install:test", "test:spec"]

desc "Run specs in CI mode"
# FIXME - Router specs currently fail on some platforms if they
# share a bundle directory with the rest of the core.
# Some kind of tricky interaction around the --without flag?
task "ci" do
sh("BUNDLE_PATH=$HOME/.vcap_router_gems bundle install --without production")
Dir.chdir("spec") do
sh("BUNDLE_PATH=$HOME/.vcap_router_gems bundle exec rake spec")
end
end

desc "Run specs using RCov"
task "spec:rcov" => ["bundler:install:test", "test:spec:rcov"]

desc "Synonym for spec"
task :test => :spec
desc "Synonym for spec"
task :tests => :spec

namespace "bundler" do
desc "Install gems"
task "install" do
sh("bundle install")
end

desc "Install gems for test"
task "install:test" do
sh("bundle install --without development production")
end

desc "Install gems for production"
task "install:production" do
sh("bundle install --without development test")
end

desc "Install gems for development"
task "install:development" do
sh("bundle install --without test production")
end
end

namespace "test" do
task "spec" do |t|
sh("cd spec && rake spec")
end

task "spec:rcov" do |t|
sh("cd spec && rake spec:rcov")
end
end
6 changes: 6 additions & 0 deletions bin/router
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# Copyright (c) 2009-2011 VMware, Inc.

home = File.join(File.dirname(__FILE__), '/..')
ENV['BUNDLE_GEMFILE'] = "#{home}/Gemfile"
require File.join(home, 'lib/router')
6 changes: 6 additions & 0 deletions config/router.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port: 2222
inet: 0.0.0.0
sock: /tmp/router.sock
mbus: nats://localhost:4222
log_level: INFO
pid: /var/vcap/sys/run/router.pid
6 changes: 6 additions & 0 deletions config/router2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port: 2224
inet: 0.0.0.0
sock: /tmp/router2.sock
mbus: nats://localhost:4222
log_level: INFO
pid: /var/vcap/sys/run/router2.pid
149 changes: 149 additions & 0 deletions lib/router.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Copyright (c) 2009-2011 VMware, Inc.
require 'fileutils'
require 'logger'
require 'optparse'
require 'socket'
require 'yaml'
require 'openssl'

require 'rubygems'
require 'bundler/setup'

require 'nats/client'
require 'http/parser'

require 'vcap/common'
require 'vcap/component'

$:.unshift(File.dirname(__FILE__))

require 'router/const'
require 'router/router'
require 'router/app_connection'
require 'router/client_connection'
require 'router/utils'

config_file = File.join(File.dirname(__FILE__), '../config/router.yml')
port, inet = nil, nil

options = OptionParser.new do |opts|
opts.banner = 'Usage: router [OPTIONS]'
opts.on("-p", "--port [ARG]", "Network port") do |opt|
port = opt.to_i
end
opts.on("-i", "--interface [ARG]", "Network Interface") do |opt|
inet = opt
end
opts.on("-c", "--config [ARG]", "Configuration File") do |opt|
config_file = opt
end
opts.on("-h", "--help", "Help") do
puts opts
exit
end
end
options.parse!(ARGV.dup)

begin
config = File.open(config_file) do |f|
YAML.load(f)
end
rescue => e
puts "Could not read configuration file: #{e}"
exit
end

# Placeholder for Component reporting
config['config_file'] = File.expand_path(config_file)

port = config['port'] unless port
inet = config['inet'] unless inet

EM.epoll

EM.run {

trap("TERM") { stop(config['pid']) }
trap("INT") { stop(config['pid']) }

Router.config(config)
Router.log.info "Starting VCAP Router (#{Router.version})"
Router.log.info "Listening on: #{inet}:#{port}" if inet && port

Router.inet = inet || VCAP.local_ip(config['local_route'])
Router.port = port

# If the sock paramater is set, this will override the inet/port
# for unix domain sockets
if fn = config['sock']
File.unlink(fn) if File.exists?(fn)
Router.log.info "Listening on unix domain socket: '#{fn}'"
end

# Hack for running on BVTs on Macs which default to 256 FDs per process
if RUBY_PLATFORM =~ /darwin/
begin
Process.setrlimit(Process::RLIMIT_NOFILE, 4096)
rescue => e
Router.log.info "Failed to modify the socket limit: #{e}"
end
end

EM.set_descriptor_table_size(32768) # Requires Root privileges
Router.log.info "Socket Limit:#{EM.set_descriptor_table_size}"

create_pid_file(config['pid'])

EM.error_handler { |e|
if e.kind_of? NATS::Error
Router.log.error("NATS problem, #{e}")
else
Router.log.error "Eventmachine problem, #{e}"
Router.log.error("#{e.backtrace.join("\n")}")
end
}

begin
# TCP/IP Socket
Router.server = EM.start_server(inet, port, ClientConnection, false) if inet && port
Router.local_server = EM.start_server(fn, nil, ClientConnection, true) if fn
rescue => e
Router.log.fatal "Problem starting server, #{e}"
exit
end

# Allow nginx to access..
FileUtils.chmod(0777, fn) if fn

NATS.start(:uri => config['mbus'])

# Create the register/unregister listeners.
Router.setup_listeners

# Register ourselves with the system
VCAP::Component.register(:type => 'Router',
:host => VCAP.local_ip(config['local_route']),
:config => config)

# Setup some of our varzs..
VCAP::Component.varz[:requests] = 0
VCAP::Component.varz[:bad_requests] = 0
VCAP::Component.varz[:urls] = 0
VCAP::Component.varz[:droplets] = 0

@router_id = VCAP.fast_uuid
@hello_message = { :id => @router_id, :version => Router::VERSION }.to_json.freeze

Router.log_connection_stats

# This will check on the state of the registered urls, do maintenance, etc..
Router.setup_sweepers

# Setup a start sweeper to make sure we have a consistent view of the world.
EM.next_tick {
# Announce our existence
NATS.publish('router.start', @hello_message)
EM.add_periodic_timer(START_SWEEPER) { NATS.publish('router.start', @hello_message) }
}
}

Loading

0 comments on commit d66dc25

Please sign in to comment.