Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have simultaneous requests coming in and have tested how its handled … #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
hostname = 'localhost'
port = 2000

s = TCPSocket.open(hostname, port)
start_time = Time.now

while line = s.gets
puts line.chop
$stdout.sync = true

threads = []

100.times do
threads << Thread.start() do
s = TCPSocket.open(hostname, port)

while line = s.gets
puts line.chop
end

s.close
end
end

s.close
threads.each do |thr|
thr.join
end

end_time = Time.now

puts end_time - start_time
Empty file added localhost
Empty file.
16 changes: 12 additions & 4 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,35 @@ def single_client_server

loop do
client = server.accept
client.puts Time.now.ctime
sleep 0.1 ##simulates laged db hit
#client.puts Time.now.ctime
client.puts "Closing the connection. Bye!"
client.close
end
end

#single_client_server
#runs in about 10 seconds, as mathematically expected (100 * .1secs)

def multi_client_server
server = TCPServer.open(PORT)

#puts server.methods

loop do
# This differs from the example above in that a new thread is spawned for each client-to-server
# connection so that multiple clients can interact with the serverr - a new process per client.

Thread.start(server.accept) do |client|
client.puts Time.now.ctime
Thread.start(server.accept) do |client|
sleep 0.1 ##simulates laged database hit
#client.puts Time.now.ctime
client.puts "Closing the connection. Bye!"
client.close
end
end
end

#multi_client_server
multi_client_server
#runs in about .5secs, because the requests are handled each on a single thread.
#because they are run on their own thread, the "db hit" allows the MRI to
#move back onto other Threads and fulfilling their requests.