patternrubyMinor
Balancing the server load
Viewed 0 times
serverthebalancingload
Problem
I've created a program that will take, a bunch of numbers from a
Once that max capacity has been hit, the numbers dump the remaining into a server called
I'm looking for better ways to write this, that are cleaner, and will make this, if possible, more readable.
Example of usage:
The main objective of this program is to have a script that will equally load three different servers, and if all servers have hit a max capacity, will load into a fail safe server.
range of 1 - 255, and then put them into 3 different strings(servers) equally. It does this by having a count that starts at 1 and loops to 3, once the count has hit 3 it restarts until three servers have hit a max capacity of 100 numbers within it.Once that max capacity has been hit, the numbers dump the remaining into a server called
failsafe.I'm looking for better ways to write this, that are cleaner, and will make this, if possible, more readable.
#!/local/usr/bin/ruby
users = (1..255) #<= Amount of users
calculon = " " #<= Random servers to be append into
darth = " "
invader_zim = " "
failsafe = " " #<= Failsafe
count = 1
users.each do |i|
target_string = case count
when 1 then calculon #<= Append into server
when 2 then darth #<= Append into server
when 3 then invader_zim #<= Append into server
end
target_string = failsafe if target_string.length == 100
target_string << i #<= When amount is reached hits fail safe
if count == 3
count = 1
else
count += 1
end
end
puts calculon.length == 1 ? 'No users on server' : calculon.length
puts darth.length == 1 ? 'No users on server' : darth.length
puts invader_zim.length == 1 ? 'No users on server' : invader_zim.length
puts failsafe.length == 1 ? 'No users on server' : failsafe.lengthExample of usage:
86
86
86
No users on server
The main objective of this program is to have a script that will equally load three different servers, and if all servers have hit a max capacity, will load into a fail safe server.
Solution
This is perfect use case for
We also get the added benefit of having the user ids as arrays instead of strings. This makes more sense, and if for some reason you do want to get the string representation back, you can simply call
Finally, if you add more servers to your cluster, you just need to edit the single line defining the server names at the top of the file, and everything will still work.
group_by, which basically does all the work for you in a single line:We also get the added benefit of having the user ids as arrays instead of strings. This makes more sense, and if for some reason you do want to get the string representation back, you can simply call
join(' ') on the array of user ids.Finally, if you add more servers to your cluster, you just need to edit the single line defining the server names at the top of the file, and everything will still work.
users = (1..255) #= servers.size * server_capacity)
all_servers_full ? 'failsafe' : servers[i % servers.size]
end
assigned_users.each do |server, users|
puts "#{server} server has #{users.size} users"
endCode Snippets
users = (1..255) #<= Amount of users
servers = %w(calculon darth invader_zim)
server_capacity = 100
assigned_users = users.group_by.with_index do |u, i|
all_servers_full = (i >= servers.size * server_capacity)
all_servers_full ? 'failsafe' : servers[i % servers.size]
end
assigned_users.each do |server, users|
puts "#{server} server has #{users.size} users"
endContext
StackExchange Code Review Q#115446, answer score: 4
Revisions (0)
No revisions yet.