Class: Ernicorn::Server

Inherits:
Unicorn::HttpServer
  • Object
show all
Defined in:
lib/ernicorn/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Server

Private HttpServer methods we’re overriding to implement BertRPC instead of HTTP.



13
14
15
# File 'lib/ernicorn/server.rb', line 13

def initialize(app, options={})
  super app, options
end

Instance Method Details

#build_app!Object



17
18
# File 'lib/ernicorn/server.rb', line 17

def build_app!
end

#process_client(client) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ernicorn/server.rb', line 39

def process_client(client)
  Stats.decr_workers_idle
  Stats.incr_connections_total

  @client = client
  # bail out if client only sent EOF
  return if @client.kgio_trypeek(1).nil?

  iruby, oruby = Ernicorn.process(self, self)
rescue EOFError
  logger.error("EOF from #{@client.kgio_addr rescue nil}")
rescue Object => e
  logger.error(e)

  begin
    error = t[:error, t[:server, 0, e.class.to_s, e.message, e.backtrace]]
    Ernicorn.write_berp(self, error)
  rescue Object => ex
    logger.error(ex)
  end
ensure
  @client.close rescue nil
  @client = nil

  Stats.incr_connections_completed
  Stats.incr_workers_idle
end

#read(len) ⇒ Object

We pass ourselves as both input and output to Ernicorn.process, which calls the following blocking read/write methods.



70
71
72
73
74
75
76
# File 'lib/ernicorn/server.rb', line 70

def read(len)
  data = ''
  while data.bytesize < len
    data << @client.kgio_read!(len - data.bytesize)
  end
  data
end

#worker_loop(worker) ⇒ Object



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

def worker_loop(worker)
  Stats.incr_workers_idle

  if worker.nr == (self.worker_processes - 1)
    old_pid = "#{self.pid}.oldbin"
    if File.exists?(old_pid) && self.pid != old_pid
      begin
        Process.kill("QUIT", File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
        # someone else did our job for us
      end
    end
  end

  super
ensure
  Stats.decr_workers_idle
end

#write(data) ⇒ Object



78
79
80
# File 'lib/ernicorn/server.rb', line 78

def write(data)
  @client.kgio_write(data)
end