Class: Yarn::Server

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants, Logging
Defined in:
lib/yarn/server.rb

Constant Summary collapse

TCP_OPTS =

TCP optimizations

[ 
  # delays accepting connections until clients send data
  [Socket::SOL_TCP, TCP_DEFER_ACCEPT, 1],
  # send ACK flags in their own packets (faster)
  [Socket::SOL_TCP, TCP_QUICKACK, 1],
  # set maximum number of
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #log, #output, #timestamp

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yarn/server.rb', line 20

def initialize(options={})
  # merge given options with default values
  opts = { 
    output: $stdout, 
    host: '127.0.0.1', 
    port: 3000,
    workers: 4,
    log: true,
    rack: "off" 
  }.merge(options)

  @app = nil
  @app = load_rack_app(opts[:rack]) unless opts[:rack] == "off"
  @opts = opts

  @host, @port, @num_workers = opts[:host], opts[:port], opts[:workers]
  @workers = []
  $output, $debug = opts[:output], opts[:debug]
  $log = opts[:log] || opts[:debug]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/yarn/server.rb', line 18

def host
  @host
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/yarn/server.rb', line 18

def port
  @port
end

#socketObject

Returns the value of attribute socket.



18
19
20
# File 'lib/yarn/server.rb', line 18

def socket
  @socket
end

#workersObject

Returns the value of attribute workers.



18
19
20
# File 'lib/yarn/server.rb', line 18

def workers
  @workers
end

Instance Method Details

#configure_socketObject



64
65
66
# File 'lib/yarn/server.rb', line 64

def configure_socket
  TCP_OPTS.each { |opt| @session.setsockopt(*opt) }
end

#fork_workerObject



72
73
74
# File 'lib/yarn/server.rb', line 72

def fork_worker
  fork { worker }
end

#get_handlerObject



86
87
88
# File 'lib/yarn/server.rb', line 86

def get_handler
  @app ? RackHandler.new(@app,@opts) : RequestHandler.new
end

#init_workersObject



68
69
70
# File 'lib/yarn/server.rb', line 68

def init_workers
  @num_workers.times { @workers << fork_worker }
end

#load_rack_app(app_path) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/yarn/server.rb', line 41

def load_rack_app(app_path)
  if File.exists?(app_path)
    config_file = File.read(app_path)
    rack_application = eval("Rack::Builder.new { #{config_file} }")
  else
    log "#{app_path} does not exist. Exiting."
    Kernel::exit
  end
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yarn/server.rb', line 51

def start
  trap("INT") { stop }
  @socket = TCPServer.new(@host, @port)
  @socket.listen(1024)
  ::BasicSocket.do_not_reverse_lookup=true
  log "Yarn started #{@num_workers} workers and is listening on #{@host}:#{@port}"

  init_workers

  # Waits here for all processes to exit
  Process.waitall
end

#stopObject



90
91
92
93
94
# File 'lib/yarn/server.rb', line 90

def stop
  @socket.close if (@socket && !@socket.closed?)

  log "Server stopped. Have a nice day!"
end

#workerObject



76
77
78
79
80
81
82
83
84
# File 'lib/yarn/server.rb', line 76

def worker
  trap("INT") { exit }
  handler = get_handler
  loop do
    @session = @socket.accept
    configure_socket
    handler.run @session
  end
end