itsi-server 0.2.17 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +1 -1
- data/exe/itsi +2 -4
- data/ext/itsi_scheduler/Cargo.toml +1 -1
- data/ext/itsi_server/Cargo.toml +1 -1
- data/ext/itsi_server/extconf.rb +7 -4
- data/ext/itsi_server/src/ruby_types/itsi_server/file_watcher.rs +422 -101
- data/ext/itsi_server/src/ruby_types/itsi_server/itsi_server_config.rs +36 -10
- data/ext/itsi_server/src/server/binds/listener.rs +3 -3
- data/ext/itsi_server/src/server/serve_strategy/cluster_mode.rs +10 -10
- data/ext/itsi_server/src/server/serve_strategy/single_mode.rs +10 -2
- data/ext/itsi_server/src/server/signal.rs +34 -12
- data/lib/itsi/server/config/dsl.rb +6 -5
- data/lib/itsi/server/config/middleware/compression.md +3 -3
- data/lib/itsi/server/config/middleware/endpoint/controller.md +1 -1
- data/lib/itsi/server/config/middleware/proxy.md +2 -2
- data/lib/itsi/server/config/middleware/rate_limit.md +1 -1
- data/lib/itsi/server/config/options/auto_reload_config.rb +7 -6
- data/lib/itsi/server/config/options/include.md +1 -0
- data/lib/itsi/server/config/options/include.rb +13 -11
- data/lib/itsi/server/config/options/reuse_port.rb +2 -4
- data/lib/itsi/server/config.rb +1 -1
- data/lib/itsi/server/default_config/Itsi.rb +9 -5
- data/lib/itsi/server/version.rb +1 -1
- metadata +1 -1
@@ -1,4 +1,4 @@
|
|
1
|
-
use super::file_watcher::{self};
|
1
|
+
use super::file_watcher::{self, WatcherCommand};
|
2
2
|
use crate::{
|
3
3
|
ruby_types::ITSI_SERVER_CONFIG,
|
4
4
|
server::{
|
@@ -9,7 +9,7 @@ use crate::{
|
|
9
9
|
use derive_more::Debug;
|
10
10
|
use itsi_error::ItsiError;
|
11
11
|
use itsi_rb_helpers::{call_with_gvl, print_rb_backtrace, HeapValue};
|
12
|
-
use itsi_tracing::{set_format, set_level, set_target, set_target_filters};
|
12
|
+
use itsi_tracing::{error, set_format, set_level, set_target, set_target_filters};
|
13
13
|
use magnus::{
|
14
14
|
block::Proc,
|
15
15
|
error::Result,
|
@@ -18,12 +18,12 @@ use magnus::{
|
|
18
18
|
};
|
19
19
|
use nix::{
|
20
20
|
fcntl::{fcntl, FcntlArg, FdFlag},
|
21
|
-
unistd::
|
21
|
+
unistd::dup,
|
22
22
|
};
|
23
23
|
use parking_lot::{Mutex, RwLock};
|
24
24
|
use std::{
|
25
25
|
collections::HashMap,
|
26
|
-
os::fd::
|
26
|
+
os::fd::RawFd,
|
27
27
|
path::PathBuf,
|
28
28
|
str::FromStr,
|
29
29
|
sync::{
|
@@ -32,7 +32,7 @@ use std::{
|
|
32
32
|
},
|
33
33
|
time::Duration,
|
34
34
|
};
|
35
|
-
use tracing::
|
35
|
+
use tracing::debug;
|
36
36
|
static DEFAULT_BIND: &str = "http://localhost:3000";
|
37
37
|
static ID_BUILD_CONFIG: LazyId = LazyId::new("build_config");
|
38
38
|
static ID_RELOAD_EXEC: LazyId = LazyId::new("reload_exec");
|
@@ -44,7 +44,7 @@ pub struct ItsiServerConfig {
|
|
44
44
|
pub itsi_config_proc: Arc<Option<HeapValue<Proc>>>,
|
45
45
|
#[debug(skip)]
|
46
46
|
pub server_params: Arc<RwLock<Arc<ServerParams>>>,
|
47
|
-
pub watcher_fd: Arc<Option<
|
47
|
+
pub watcher_fd: Arc<Option<file_watcher::WatcherPipes>>,
|
48
48
|
}
|
49
49
|
|
50
50
|
#[derive(Debug)]
|
@@ -84,7 +84,7 @@ pub struct ServerParams {
|
|
84
84
|
listener_info: Mutex<HashMap<String, i32>>,
|
85
85
|
pub itsi_server_token_preference: ItsiServerTokenPreference,
|
86
86
|
pub preloaded: AtomicBool,
|
87
|
-
socket_opts: SocketOpts,
|
87
|
+
pub socket_opts: SocketOpts,
|
88
88
|
preexisting_listeners: Option<String>,
|
89
89
|
}
|
90
90
|
|
@@ -442,6 +442,10 @@ impl ItsiServerConfig {
|
|
442
442
|
}
|
443
443
|
}
|
444
444
|
|
445
|
+
pub fn use_reuse_port_load_balancing(&self) -> bool {
|
446
|
+
cfg!(target_os = "linux") && self.server_params.read().socket_opts.reuse_port
|
447
|
+
}
|
448
|
+
|
445
449
|
/// Reload
|
446
450
|
pub fn reload(self: Arc<Self>, cluster_worker: bool) -> Result<bool> {
|
447
451
|
let server_params = call_with_gvl(|ruby| {
|
@@ -553,6 +557,9 @@ impl ItsiServerConfig {
|
|
553
557
|
}
|
554
558
|
|
555
559
|
pub fn dup_fds(self: &Arc<Self>) -> Result<()> {
|
560
|
+
// Ensure the watcher is already stopped before duplicating file descriptors
|
561
|
+
// to prevent race conditions between closing the watcher FD and duplicating socket FDs
|
562
|
+
|
556
563
|
let binding = self.server_params.read();
|
557
564
|
let mut listener_info_guard = binding.listener_info.lock();
|
558
565
|
let dupped_fd_map = listener_info_guard
|
@@ -578,8 +585,10 @@ impl ItsiServerConfig {
|
|
578
585
|
}
|
579
586
|
|
580
587
|
pub fn stop_watcher(self: &Arc<Self>) -> Result<()> {
|
581
|
-
if let Some(
|
582
|
-
|
588
|
+
if let Some(pipes) = self.watcher_fd.as_ref() {
|
589
|
+
// Send explicit stop command to the watcher process
|
590
|
+
file_watcher::send_watcher_command(&pipes.write_fd, WatcherCommand::Stop)?;
|
591
|
+
// We don't close the pipes here - they'll be closed when the WatcherPipes is dropped
|
583
592
|
}
|
584
593
|
Ok(())
|
585
594
|
}
|
@@ -594,8 +603,24 @@ impl ItsiServerConfig {
|
|
594
603
|
pub async fn check_config(&self) -> bool {
|
595
604
|
if let Some(errors) = self.get_config_errors().await {
|
596
605
|
Self::print_config_errors(errors);
|
606
|
+
// Notify watcher that config check failed
|
607
|
+
if let Some(pipes) = self.watcher_fd.as_ref() {
|
608
|
+
if let Err(e) =
|
609
|
+
file_watcher::send_watcher_command(&pipes.write_fd, WatcherCommand::ConfigError)
|
610
|
+
{
|
611
|
+
error!("Failed to notify watcher of config error: {}", e);
|
612
|
+
}
|
613
|
+
}
|
597
614
|
return false;
|
598
615
|
}
|
616
|
+
// If we reach here, the config is valid
|
617
|
+
if let Some(pipes) = self.watcher_fd.as_ref() {
|
618
|
+
if let Err(e) =
|
619
|
+
file_watcher::send_watcher_command(&pipes.write_fd, WatcherCommand::Continue)
|
620
|
+
{
|
621
|
+
error!("Failed to notify watcher to continue: {}", e);
|
622
|
+
}
|
623
|
+
}
|
599
624
|
true
|
600
625
|
}
|
601
626
|
|
@@ -609,7 +634,8 @@ impl ItsiServerConfig {
|
|
609
634
|
)
|
610
635
|
})?;
|
611
636
|
|
612
|
-
|
637
|
+
// Make sure we're not calling stop_watcher here to avoid double-stopping
|
638
|
+
// The watcher should be stopped earlier in the restart sequence
|
613
639
|
call_with_gvl(|ruby| -> Result<()> {
|
614
640
|
ruby.get_inner_ref(&ITSI_SERVER_CONFIG)
|
615
641
|
.funcall::<_, _, Value>(*ID_RELOAD_EXEC, (listener_json,))?;
|
@@ -304,16 +304,16 @@ impl Listener {
|
|
304
304
|
connect_tcp_socket(ip, port, &socket_opts).unwrap()
|
305
305
|
}
|
306
306
|
|
307
|
-
pub fn into_tokio_listener(self,
|
307
|
+
pub fn into_tokio_listener(self, should_rebind: bool) -> TokioListener {
|
308
308
|
match self {
|
309
309
|
Listener::Tcp(mut listener) => {
|
310
|
-
if
|
310
|
+
if should_rebind {
|
311
311
|
listener = Listener::rebind_listener(listener);
|
312
312
|
}
|
313
313
|
TokioListener::Tcp(TokioTcpListener::from_std(listener).unwrap())
|
314
314
|
}
|
315
315
|
Listener::TcpTls((mut listener, acceptor)) => {
|
316
|
-
if
|
316
|
+
if should_rebind {
|
317
317
|
listener = Listener::rebind_listener(listener);
|
318
318
|
}
|
319
319
|
TokioListener::TcpTls(
|
@@ -100,9 +100,11 @@ impl ClusterMode {
|
|
100
100
|
LifecycleEvent::Restart => {
|
101
101
|
if self.server_config.check_config().await {
|
102
102
|
self.invoke_hook("before_restart");
|
103
|
+
self.server_config.stop_watcher()?;
|
103
104
|
self.server_config.dup_fds()?;
|
104
105
|
self.shutdown().await.ok();
|
105
106
|
info!("Shutdown complete. Calling reload exec");
|
107
|
+
|
106
108
|
self.server_config.reload_exec()?;
|
107
109
|
}
|
108
110
|
Ok(())
|
@@ -111,8 +113,11 @@ impl ClusterMode {
|
|
111
113
|
if !self.server_config.check_config().await {
|
112
114
|
return Ok(());
|
113
115
|
}
|
116
|
+
|
114
117
|
let should_reexec = self.server_config.clone().reload(true)?;
|
118
|
+
|
115
119
|
if should_reexec {
|
120
|
+
self.server_config.stop_watcher()?;
|
116
121
|
self.server_config.dup_fds()?;
|
117
122
|
self.shutdown().await.ok();
|
118
123
|
self.server_config.reload_exec()?;
|
@@ -321,15 +326,6 @@ impl ClusterMode {
|
|
321
326
|
.iter()
|
322
327
|
.try_for_each(|worker| worker.boot(Arc::clone(&self)))?;
|
323
328
|
|
324
|
-
if cfg!(target_os = "linux") {
|
325
|
-
self.server_config
|
326
|
-
.server_params
|
327
|
-
.write()
|
328
|
-
.listeners
|
329
|
-
.lock()
|
330
|
-
.drain(..);
|
331
|
-
};
|
332
|
-
|
333
329
|
let (sender, mut receiver) = watch::channel(());
|
334
330
|
*CHILD_SIGNAL_SENDER.lock() = Some(sender);
|
335
331
|
|
@@ -381,9 +377,13 @@ impl ClusterMode {
|
|
381
377
|
}
|
382
378
|
lifecycle_event = lifecycle_rx.recv() => match lifecycle_event{
|
383
379
|
Ok(lifecycle_event) => {
|
380
|
+
debug!("Cluster mode received lifecycle event: {:?}", lifecycle_event);
|
384
381
|
if let Err(e) = self_ref.clone().handle_lifecycle_event(lifecycle_event).await{
|
385
382
|
match e {
|
386
|
-
ItsiError::Break =>
|
383
|
+
ItsiError::Break => {
|
384
|
+
debug!("Lifecycle event triggered shutdown, breaking cluster monitor loop");
|
385
|
+
break;
|
386
|
+
},
|
387
387
|
_ => error!("Error in handle_lifecycle_event {:?}", e)
|
388
388
|
}
|
389
389
|
}
|
@@ -262,7 +262,14 @@ impl SingleMode {
|
|
262
262
|
let shutdown_timeout = self.server_config.server_params.read().shutdown_timeout;
|
263
263
|
let (shutdown_sender, _) = watch::channel(RunningPhase::Running);
|
264
264
|
let monitor_thread = self.clone().start_monitors(thread_workers.clone());
|
265
|
+
|
266
|
+
// If we're on Linux with reuse_port enabled, we can use
|
267
|
+
// kernel level load balancing across processes sharing a port.
|
268
|
+
// To take advantage of this, these forks will rebind to the same port upon boot.
|
269
|
+
// Worker 0 is special (this one just inherits the bind from the master process).
|
265
270
|
let is_zero_worker = self.is_zero_worker();
|
271
|
+
let should_rebind = !is_zero_worker && self.server_config.use_reuse_port_load_balancing();
|
272
|
+
|
266
273
|
if monitor_thread.is_none() {
|
267
274
|
error!("Failed to start monitor thread");
|
268
275
|
return Err(ItsiError::new("Failed to start monitor thread"));
|
@@ -283,7 +290,7 @@ impl SingleMode {
|
|
283
290
|
.listeners
|
284
291
|
.lock()
|
285
292
|
.drain(..)
|
286
|
-
.map(|list| Arc::new(list.into_tokio_listener(
|
293
|
+
.map(|list| Arc::new(list.into_tokio_listener(should_rebind)))
|
287
294
|
.collect::<Vec<_>>();
|
288
295
|
|
289
296
|
tokio_listeners.iter().cloned().for_each(|listener| {
|
@@ -311,7 +318,7 @@ impl SingleMode {
|
|
311
318
|
let mut after_accept_wait: Option<Duration> = None::<Duration>;
|
312
319
|
|
313
320
|
if cfg!(target_os = "macos") {
|
314
|
-
after_accept_wait = if server_params.workers > 1 {
|
321
|
+
after_accept_wait = if server_params.workers > 1 && !(server_params.socket_opts.reuse_port && server_params.socket_opts.reuse_address) {
|
315
322
|
Some(Duration::from_nanos(10 * server_params.workers as u64))
|
316
323
|
} else {
|
317
324
|
None
|
@@ -434,6 +441,7 @@ impl SingleMode {
|
|
434
441
|
if self.is_single_mode() {
|
435
442
|
self.invoke_hook("before_restart");
|
436
443
|
}
|
444
|
+
self.server_config.stop_watcher()?;
|
437
445
|
self.server_config.dup_fds()?;
|
438
446
|
self.server_config.reload_exec()?;
|
439
447
|
Ok(())
|
@@ -1,11 +1,12 @@
|
|
1
1
|
use std::{
|
2
2
|
collections::VecDeque,
|
3
|
-
sync::atomic::{AtomicBool, AtomicI8},
|
3
|
+
sync::atomic::{AtomicBool, AtomicI8, Ordering},
|
4
4
|
};
|
5
5
|
|
6
6
|
use nix::libc::{self, sighandler_t};
|
7
7
|
use parking_lot::Mutex;
|
8
8
|
use tokio::sync::broadcast;
|
9
|
+
use tracing::{debug, warn};
|
9
10
|
|
10
11
|
use super::lifecycle_event::LifecycleEvent;
|
11
12
|
|
@@ -21,12 +22,14 @@ pub fn subscribe_runtime_to_signals() -> broadcast::Receiver<LifecycleEvent> {
|
|
21
22
|
if let Some(sender) = guard.as_ref() {
|
22
23
|
return sender.subscribe();
|
23
24
|
}
|
24
|
-
let (sender, receiver) = broadcast::channel(
|
25
|
+
let (sender, receiver) = broadcast::channel(32);
|
25
26
|
let sender_clone = sender.clone();
|
26
27
|
std::thread::spawn(move || {
|
27
|
-
std::thread::sleep(std::time::Duration::from_millis(
|
28
|
+
std::thread::sleep(std::time::Duration::from_millis(10));
|
28
29
|
for event in PENDING_QUEUE.lock().drain(..) {
|
29
|
-
sender_clone.send(event)
|
30
|
+
if let Err(e) = sender_clone.send(event) {
|
31
|
+
eprintln!("Warning: Failed to send pending lifecycle event {:?}", e);
|
32
|
+
}
|
30
33
|
}
|
31
34
|
});
|
32
35
|
|
@@ -41,22 +44,36 @@ pub fn unsubscribe_runtime() {
|
|
41
44
|
|
42
45
|
pub fn send_lifecycle_event(event: LifecycleEvent) {
|
43
46
|
if let Some(sender) = SIGNAL_HANDLER_CHANNEL.lock().as_ref() {
|
44
|
-
sender.send(event)
|
47
|
+
if let Err(e) = sender.send(event) {
|
48
|
+
// Channel full or receivers dropped - this is a critical error for shutdown signals
|
49
|
+
eprintln!("Critical: Failed to send lifecycle event {:?}", e);
|
50
|
+
// For shutdown events, try to force exit if channel delivery fails
|
51
|
+
if matches!(
|
52
|
+
e.0,
|
53
|
+
LifecycleEvent::Shutdown | LifecycleEvent::ForceShutdown
|
54
|
+
) {
|
55
|
+
eprintln!("Emergency shutdown due to signal delivery failure");
|
56
|
+
std::process::exit(1);
|
57
|
+
}
|
58
|
+
}
|
45
59
|
} else {
|
46
60
|
PENDING_QUEUE.lock().push_back(event);
|
47
61
|
}
|
48
62
|
}
|
49
63
|
|
50
64
|
fn receive_signal(signum: i32, _: sighandler_t) {
|
51
|
-
|
65
|
+
debug!("Received signal: {}", signum);
|
66
|
+
SIGINT_COUNT.fetch_add(-1, Ordering::SeqCst);
|
52
67
|
let event = match signum {
|
53
68
|
libc::SIGTERM | libc::SIGINT => {
|
54
|
-
|
55
|
-
|
56
|
-
|
69
|
+
debug!("Received shutdown signal (SIGTERM/SIGINT)");
|
70
|
+
SHUTDOWN_REQUESTED.store(true, Ordering::SeqCst);
|
71
|
+
SIGINT_COUNT.fetch_add(2, Ordering::SeqCst);
|
72
|
+
if SIGINT_COUNT.load(Ordering::SeqCst) < 2 {
|
73
|
+
debug!("First shutdown signal, requesting graceful shutdown");
|
57
74
|
Some(LifecycleEvent::Shutdown)
|
58
75
|
} else {
|
59
|
-
|
76
|
+
warn!("Multiple shutdown signals received, forcing immediate shutdown");
|
60
77
|
Some(LifecycleEvent::ForceShutdown)
|
61
78
|
}
|
62
79
|
}
|
@@ -70,13 +87,17 @@ fn receive_signal(signum: i32, _: sighandler_t) {
|
|
70
87
|
};
|
71
88
|
|
72
89
|
if let Some(event) = event {
|
90
|
+
debug!("Signal {} mapped to lifecycle event: {:?}", signum, event);
|
73
91
|
send_lifecycle_event(event);
|
92
|
+
} else {
|
93
|
+
debug!("Signal {} not mapped to any lifecycle event", signum);
|
74
94
|
}
|
75
95
|
}
|
76
96
|
|
77
97
|
pub fn reset_signal_handlers() -> bool {
|
78
|
-
|
79
|
-
|
98
|
+
debug!("Resetting signal handlers");
|
99
|
+
SIGINT_COUNT.store(0, Ordering::SeqCst);
|
100
|
+
SHUTDOWN_REQUESTED.store(false, Ordering::SeqCst);
|
80
101
|
|
81
102
|
unsafe {
|
82
103
|
libc::signal(libc::SIGTERM, receive_signal as usize);
|
@@ -92,6 +113,7 @@ pub fn reset_signal_handlers() -> bool {
|
|
92
113
|
}
|
93
114
|
|
94
115
|
pub fn clear_signal_handlers() {
|
116
|
+
debug!("Clearing signal handlers");
|
95
117
|
unsafe {
|
96
118
|
libc::signal(libc::SIGTERM, libc::SIG_DFL);
|
97
119
|
libc::signal(libc::SIGINT, libc::SIG_DFL);
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# typed: true
|
3
|
+
|
1
4
|
module Itsi
|
2
5
|
class Server
|
3
6
|
module Config
|
@@ -55,9 +58,7 @@ module Itsi
|
|
55
58
|
nested_locations: [],
|
56
59
|
middleware_loader: lambda do
|
57
60
|
@options[:nested_locations].each(&:call)
|
58
|
-
|
59
|
-
@middleware[:app] = { app_proc: DEFAULT_APP[]}
|
60
|
-
end
|
61
|
+
@middleware[:app] = { app_proc: DEFAULT_APP[] } unless @middleware[:app] || @middleware[:static_assets]
|
61
62
|
[flatten_routes, Config.errors_to_error_lines(errors)]
|
62
63
|
end
|
63
64
|
}
|
@@ -75,7 +76,7 @@ module Itsi
|
|
75
76
|
define_method(option_name) do |*args, **kwargs, &blk|
|
76
77
|
option.new(self, *args, **kwargs, &blk).build!
|
77
78
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
78
|
-
@errors << [e, e.backtrace.find{|r| !(r =~ /
|
79
|
+
@errors << [e, e.backtrace.find { |r| !(r =~ %r{server/config}) }]
|
79
80
|
end
|
80
81
|
end
|
81
82
|
|
@@ -86,7 +87,7 @@ module Itsi
|
|
86
87
|
rescue Config::Endpoint::InvalidHandlerException => e
|
87
88
|
@errors << [e, "#{e.backtrace[0]}:in #{e.message}"]
|
88
89
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
89
|
-
@errors << [e, e.backtrace.find{|r| !(r =~ /
|
90
|
+
@errors << [e, e.backtrace.find { |r| !(r =~ %r{server/config}) }]
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
@@ -13,7 +13,7 @@ You can enable several different compression algorithms, and choose to selective
|
|
13
13
|
min_size: 1024 # 1KiB,
|
14
14
|
algorithms: %w[zstd gzip deflate br],
|
15
15
|
compress_streams: true,
|
16
|
-
mime_types: %[all],
|
16
|
+
mime_types: %w[all],
|
17
17
|
level: "fastest"
|
18
18
|
```
|
19
19
|
|
@@ -24,7 +24,7 @@ You can enable several different compression algorithms, and choose to selective
|
|
24
24
|
compress \
|
25
25
|
min_size: 1024 # 1KiB,
|
26
26
|
algorithms: %w[zstd gzip deflate br],
|
27
|
-
mime_types: %[image],
|
27
|
+
mime_types: %w[image],
|
28
28
|
level: "fastest"
|
29
29
|
|
30
30
|
static_assets: \
|
@@ -47,4 +47,4 @@ You can enable several different compression algorithms, and choose to selective
|
|
47
47
|
# Pre-compressed `static_assets`
|
48
48
|
Itsi also supports serving pre-compressed static assets directly from the file-system.
|
49
49
|
This is configured inside the `static_assets` middleware.
|
50
|
-
Go to the [static_assets](/middleware/static_assets
|
50
|
+
Go to the [static_assets](/middleware/static_assets) middleware for more information.
|
@@ -12,7 +12,7 @@ proxy \
|
|
12
12
|
to: "http://backend.example.com/api{path}{query}",
|
13
13
|
backends: ["127.0.0.1:3001", "127.0.0.1:3002"],
|
14
14
|
backend_priority: "round_robin",
|
15
|
-
headers: { "X-Forwarded-For" =>
|
15
|
+
headers: { "X-Forwarded-For" => "{addr}" },
|
16
16
|
verify_ssl: false,
|
17
17
|
timeout: 30,
|
18
18
|
tls_sni: true,
|
@@ -54,7 +54,7 @@ proxy \
|
|
54
54
|
3. **Header Overrides**
|
55
55
|
The `headers` option lets you specify extra or overriding headers. Each header value may be a literal or a string rewrite. For example, overriding `"X-Forwarded-For"` to carry the client’s IP is done by:
|
56
56
|
```ruby
|
57
|
-
{ "X-Forwarded-For" =>
|
57
|
+
{ "X-Forwarded-For" => "{addr}" }
|
58
58
|
```
|
59
59
|
|
60
60
|
4. **Request Forwarding and Error Handling**
|
@@ -2,9 +2,8 @@ module Itsi
|
|
2
2
|
class Server
|
3
3
|
module Config
|
4
4
|
class AutoReloadConfig < Option
|
5
|
-
|
6
5
|
insert_text <<~SNIPPET
|
7
|
-
|
6
|
+
auto_reload_config! # Auto-reload the server configuration each time it changes.
|
8
7
|
SNIPPET
|
9
8
|
|
10
9
|
detail "Auto-reload the server configuration each time it changes."
|
@@ -15,18 +14,20 @@ module Itsi
|
|
15
14
|
|
16
15
|
def build!
|
17
16
|
return if @auto_reloading
|
18
|
-
|
17
|
+
|
18
|
+
src = caller.find { |l| !(l =~ %r{lib/itsi/server/config}) }.split(":").first
|
19
19
|
|
20
20
|
location.instance_eval do
|
21
21
|
return if @auto_reloading
|
22
22
|
|
23
23
|
if @included
|
24
24
|
@included.each do |file|
|
25
|
-
next if
|
25
|
+
next if "#{file}" == src
|
26
|
+
|
26
27
|
if ENV["BUNDLE_BIN_PATH"]
|
27
|
-
watch "#{file}
|
28
|
+
watch "#{file}", [%w[bundle exec itsi restart]]
|
28
29
|
else
|
29
|
-
watch "#{file}
|
30
|
+
watch "#{file}", [%w[itsi restart]]
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
@@ -7,6 +7,7 @@ Use the `include` option to load additional files to be evaluated within the cur
|
|
7
7
|
You can use this option to split a large configuration file into multiple smaller files.
|
8
8
|
|
9
9
|
Files required using `include` are also subject to auto-reloading, when using the [auto_reload_config](/options/auto_reload_config) option.
|
10
|
+
The path of the included file is evaluated relative to the current configuration file.
|
10
11
|
|
11
12
|
## Examples
|
12
13
|
```ruby {filename="Itsi.rb"}
|
@@ -2,37 +2,39 @@ module Itsi
|
|
2
2
|
class Server
|
3
3
|
module Config
|
4
4
|
class Include < Option
|
5
|
-
|
6
5
|
insert_text "include \"${1|other_file|}\" # Include another file to be loaded within the current configuration"
|
7
6
|
|
8
7
|
detail "Include another file to be loaded within the current configuration"
|
9
8
|
|
10
9
|
schema do
|
11
|
-
Type(String)
|
10
|
+
Type(String) & Required()
|
12
11
|
end
|
13
12
|
|
14
13
|
def build!
|
15
|
-
|
14
|
+
caller_location = caller_locations(2, 1).first.path
|
15
|
+
included_file = \
|
16
|
+
if caller_location =~ %r{lib/itsi/server}
|
17
|
+
File.expand_path("#{@params}.rb")
|
18
|
+
else
|
19
|
+
File.expand_path("#{@params}.rb", File.dirname(caller_location))
|
20
|
+
end
|
21
|
+
|
16
22
|
location.instance_eval do
|
17
23
|
@included ||= []
|
18
24
|
@included << included_file
|
19
25
|
|
20
26
|
if @auto_reloading
|
21
27
|
if ENV["BUNDLE_BIN_PATH"]
|
22
|
-
watch "#{included_file}
|
28
|
+
watch "#{included_file}", [%w[bundle exec itsi restart]]
|
23
29
|
else
|
24
|
-
watch "#{included_file}
|
30
|
+
watch "#{included_file}", [%w[itsi restart]]
|
25
31
|
end
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
code = IO.read(filename)
|
32
|
-
location.instance_eval(code, filename, 1)
|
33
|
-
|
35
|
+
code = IO.read(included_file)
|
36
|
+
location.instance_eval(code, included_file, 1)
|
34
37
|
end
|
35
|
-
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
@@ -2,17 +2,15 @@ module Itsi
|
|
2
2
|
class Server
|
3
3
|
module Config
|
4
4
|
class ReusePort < Option
|
5
|
-
|
6
5
|
insert_text <<~SNIPPET
|
7
|
-
|
6
|
+
reuse_port ${1|true,false|}
|
8
7
|
SNIPPET
|
9
8
|
|
10
9
|
detail "Configures whether the server should set the reuse_port option on the underlying socket."
|
11
10
|
|
12
11
|
schema do
|
13
|
-
(Bool() & Required()).default(
|
12
|
+
(Bool() & Required()).default(true)
|
14
13
|
end
|
15
|
-
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|
data/lib/itsi/server/config.rb
CHANGED
@@ -97,7 +97,7 @@ module Itsi
|
|
97
97
|
errors << [e, e.backtrace[0]]
|
98
98
|
end
|
99
99
|
# If we're just preloading a specific gem group, we'll do that here too
|
100
|
-
when Symbol
|
100
|
+
when Symbol, String
|
101
101
|
Itsi.log_debug("Preloading gem group #{preload}")
|
102
102
|
Bundler.require(preload)
|
103
103
|
end
|
@@ -10,7 +10,7 @@ env = ENV.fetch("APP_ENV") { ENV.fetch("RACK_ENV", "development") }
|
|
10
10
|
|
11
11
|
# Number of worker processes to spawn
|
12
12
|
# If more than 1, Itsi will be booted in Cluster mode
|
13
|
-
workers ENV["ITSI_WORKERS"]&.to_i || env == "development" ? 1 : nil
|
13
|
+
workers ENV["ITSI_WORKERS"]&.to_i || (env == "development" ? 1 : nil)
|
14
14
|
|
15
15
|
# Number of threads to spawn per worker process
|
16
16
|
# For pure CPU bound applicationss, you'll get the best results keeping this number low
|
@@ -27,11 +27,13 @@ threads ENV.fetch("ITSI_THREADS", 3)
|
|
27
27
|
fiber_scheduler nil
|
28
28
|
|
29
29
|
# If you bind to https, without specifying a certificate, Itsi will use a self-signed certificate.
|
30
|
-
# The self-signed certificate will use a CA generated for your
|
30
|
+
# The self-signed certificate will use a CA generated for your
|
31
|
+
# host and stored inside `ITSI_LOCAL_CA_DIR` (Defaults to ~/.itsi)
|
31
32
|
# bind "https://0.0.0.0:3000"
|
32
33
|
# bind "https://0.0.0.0:3000?domains=dev.itsi.fyi"
|
33
34
|
#
|
34
|
-
# If you want to use let's encrypt to generate you a real certificate you
|
35
|
+
# If you want to use let's encrypt to generate you a real certificate you
|
36
|
+
# and pass cert=acme and an acme_email address to generate one.
|
35
37
|
# bind "https://itsi.fyi?cert=acme&acme_email=admin@itsi.fyi"
|
36
38
|
# You can generate certificates for multiple domains at once, by passing a comma-separated list of domains
|
37
39
|
# bind "https://0.0.0.0?domains=foo.itsi.fyi,bar.itsi.fyi&cert=acme&acme_email=admin@itsi.fyi"
|
@@ -68,7 +70,8 @@ preload true
|
|
68
70
|
# all of them at once, if they reach the threshold simultaneously.
|
69
71
|
worker_memory_limit 1024 * 1024 * 1024
|
70
72
|
|
71
|
-
# You can provide an optional block of code to run, when a worker hits its memory threshold
|
73
|
+
# You can provide an optional block of code to run, when a worker hits its memory threshold
|
74
|
+
# (Use this to send yourself an alert,
|
72
75
|
# write metrics to disk etc. etc.)
|
73
76
|
after_memory_limit_reached do |pid|
|
74
77
|
puts "Worker #{pid} has reached its memory threshold and will restart"
|
@@ -85,7 +88,8 @@ after_fork {}
|
|
85
88
|
shutdown_timeout 5
|
86
89
|
|
87
90
|
# Set this to false for application environments that require rack.input to be a rewindable body
|
88
|
-
# (like Rails). For rack applications that can stream inputs, you can set this to true for a more
|
91
|
+
# (like Rails). For rack applications that can stream inputs, you can set this to true for a more
|
92
|
+
# memory-efficient approach.
|
89
93
|
stream_body false
|
90
94
|
|
91
95
|
# OOB GC responses threshold
|
data/lib/itsi/server/version.rb
CHANGED