-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench-server.rb
More file actions
executable file
·56 lines (44 loc) · 1.22 KB
/
bench-server.rb
File metadata and controls
executable file
·56 lines (44 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
puts "Loading hyper_ruby"
require "hyper_ruby"
require "json"
# Create and configure the server
server = HyperRuby::Server.new
config = {
bind_address: ENV.fetch("BIND_ADDRESS", "127.0.0.1:3000"),
tokio_threads: ENV.fetch("TOKIO_THREADS", "1").to_i,
debug: ENV.fetch("DEBUG", "false") == "true",
recv_timeout: ENV.fetch("RECV_TIMEOUT", "30000").to_i,
max_connection_age: ENV.fetch("MAX_CONNECTION_AGE", "30000").to_i
}
server.configure(config)
puts "Starting server with config: #{config}"
accept_response = HyperRuby::Response.new(
202,
{ "Content-Type" => "application/json" },
{ "message" => "Accepted" }.to_json
)
# Start the server
server.start
puts "Server started"
# Create a worker thread to handle requests
worker = Thread.new do
server.run_worker do |request|
# read the body into a buffer, to simulate some work
buffer = String.new(capacity: 1024)
request.fill_body(buffer)
accept_response
end
end
puts "Server running at #{config[:bind_address]}"
puts "Press Ctrl+C to stop"
# Wait for Ctrl+C
begin
sleep
rescue Interrupt
puts "\nShutting down..."
server.stop
worker.join
end