Class: Mongrel::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/never_block/servers/mongrel.rb

Constant Summary collapse

DEFAULT_FIBER_POOL_SIZE =
20

Instance Method Summary collapse

Constructor Details

#initialize(host, port, num_processors = 950, x = 0, y = nil) ⇒ HttpServer

Deal with Mongrel 1.0.1 or earlier, as well as later.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/never_block/servers/mongrel.rb', line 90

def initialize(host, port, num_processors=950, x=0, y=nil) # Deal with Mongrel 1.0.1 or earlier, as well as later.
	@socket = nil
	@classifier = URIClassifier.new
	@host = host
	@port = port
	@workers = ThreadGroup.new
	if y
		@throttle = x
		@timeout = y || 60
	else
		@timeout = x
	end
	@num_processors = num_processors #num_processors is pointless for evented....
	@death_time = 60
	self.class.const_set(:Instance,self)
end

Instance Method Details

#dispatch_to_handlers(handlers, request, response) ⇒ Object



161
162
163
164
165
166
# File 'lib/never_block/servers/mongrel.rb', line 161

def dispatch_to_handlers(handlers,request,response)
	handlers.each do |handler|
		handler.process(request, response)
		break if response.done
	end
end

#fiber_poolObject



107
108
109
# File 'lib/never_block/servers/mongrel.rb', line 107

def fiber_pool
  @fiber_pool ||= NB::Pool::FiberPool.new(DEFAULT_FIBER_POOL_SIZE)
end

#process_http_request(params, linebuffer, client) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/never_block/servers/mongrel.rb', line 127

def process_http_request(params,linebuffer,client)
	if not params[Const::REQUEST_PATH]
		uri = URI.parse(params[Const::REQUEST_URI])
		params[Const::REQUEST_PATH] = uri.request_uri
	end

    raise "No REQUEST PATH" if not params[Const::REQUEST_PATH]

	script_name, path_info, handlers = @classifier.resolve(params[Const::REQUEST_PATH])

	if handlers
		notifiers = handlers.select { |h| h.request_notify }
		request = HttpRequest.new(params, linebuffer, notifiers)

		# request is good so far, continue processing the response
		response = HttpResponse.new(client)

		# Process each handler in registered order until we run out or one finalizes the response.
		fiber_pool.spawn do
		  dispatch_to_handlers(handlers,request,response)
      end
		# And finally, if nobody closed the response off, we finalize it.
		unless response.done
			response.finished
		else
			response.close_connection_after_writing
		end
	else
		# Didn't find it, return a stock 404 response.
		client.send_data(Const::ERROR_404_RESPONSE)
		client.close_connection_after_writing
	end
end

#runObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/never_block/servers/mongrel.rb', line 111

def run
	trap('INT') { raise StopServer }
	trap('TERM') { raise StopServer }
	@acceptor = Thread.new do
		EventMachine.epoll
		EventMachine.set_descriptor_table_size(4096)
		EventMachine.run do
			begin
				EventMachine.start_server(@host,@port,MongrelProtocol)
			rescue StopServer
				EventMachine.stop_event_loop
			end
		end
	end
end