When a lucky app boots, it's only 1 single process. If you're running on a beast server, then you're missing out on some free performance! This shards lets you boot multiple processes of your lucky app.
NOTE This version requires Lucky 0.15.0
or later.
- Add the dependency to your
shard.yml
:
dependencies:
lucky-cluster:
github: jwoertink/lucky-cluster
- Run
shards install
- Open
src/start_server.cr
- Add your require below the app require
require "./app"
require "lucky-cluster"
- Replace
AppServer.new
withLucky::Cluster.new
app_server = Lucky::Cluster.new
-
Remove the
Signal::INT.trap
block.lucky-cluster
handles that for you. -
You can remove the extra
puts
in there too, if you want. -
Optionally, specify the number of processes to boot with
app_server.threads = 2
Once done, your src/start_server.cr
file should look like this:
require "./app"
require "lucky-cluster"
if Lucky::Env.development?
Avram::Migrator::Runner.new.ensure_migrated!
end
Habitat.raise_if_missing_settings!
app_server = Lucky::Cluster.new
# This boots a new process for each thread.
app_server.threads = ENV.fetch("MAX_THREADS") { "10" }.to_i
# You can also use this:
# app_server.threads = System.cpu_count.to_i32
app_server.listen
Linux will handle load balancing the processes for you, macOS will not. So if you try to load test this on a mac, you're going to have a bad time!
I don't know yet. Just make sure you don't break this thing, and if you know how I can write specs for this, please help!
- Fork it (https://github.com/jwoertink/lucky-cluster/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Jeremy Woertink - creator and maintainer
Thanks to the work done on Spider-Gazelle for showing how they did it.