tiny_http_fork 0.12.10

Low level HTTP server library FORK
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
use std::io::ErrorKind;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::Duration;
use std::{fmt, io};
use std::sync::{Arc, Mutex, OnceLock};
use std::thread::{self, JoinHandle};

use crossbeam::channel::{Receiver, Sender, unbounded};
use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction};
use nix::unistd::alarm;

use crate::log;

static COMPLETED_TASKS: AtomicUsize = AtomicUsize::new(0);
static TASKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0);
static SPAWNED_THREADS: AtomicUsize = AtomicUsize::new(0);

static MIN_THREADS_VAL: OnceLock<usize> = OnceLock::new();
static MAX_THREADS_VAL: OnceLock<usize> = OnceLock::new();

/// A struct for the task thread.
#[derive(Debug)]
struct TaskThread
{
    /// Name of the thread.
    thr_id: usize,

    /// A shared access to the thread pool for managment.
    inner: Arc<Mutex<TaskPoolInner>>,

    /// A sender endpoint of channel.
    task_send_ch: Sender<TaskTask>, 

    /// A receiver which is polled.
    recv_ch: Receiver<TaskTask>, 

    /// A exit reason trap.
    tec: TaskExitCatcher, 
}

impl TaskThread
{
    fn thread(mut self)
    {
        loop 
        {
            let task = 
                if self.thr_id >= *MIN_THREADS_VAL.get().unwrap() 
                {
                    let Ok(task) = self.recv_ch.recv_timeout(Duration::from_secs(5))
                        else
                        {
                            break;
                        };

                    task
                }
                else
                {
                    let Ok(task) = self.recv_ch.recv()
                        else
                        {
                            break;
                        };

                    task
                };

            match task
            {
                TaskTask::Exec(mut fn_mut) => 
                {
                    (fn_mut)();

                    COMPLETED_TASKS.fetch_add(1, Ordering::SeqCst);
                },
                TaskTask::ExitReason(TaskExitReason::Ok(thr_no)) => 
                {
                    let cur_thr = thread::current();

                    log::info!("[{}] thread #{} exited", cur_thr.name().unwrap_or("UNKNOWN"), thr_no);
                    let mut task_pool_lock = self.inner.lock().unwrap();

                    let Some(task_handle) = task_pool_lock.thread_pool.get_mut(thr_no).unwrap().take()
                        else
                        {
                            continue;
                        };

                    drop(task_pool_lock);

                    task_handle.take_result();
                },
                TaskTask::ExitReason(TaskExitReason::Panic(thr_no)) =>
                {
                    let cur_thr = thread::current();

                    // restart the thread
                    let mut task_pool_lock = self.inner.lock().unwrap();

                    let task_handle = 
                        match task_pool_lock.add_thread(self.inner.clone(), self.tec.block_panic.clone(), thr_no)
                        {
                            Ok(r) => r,
                            Err(e) =>
                            {
                                log::error!("[{}] thread no: {} creation error: {}. Stalled thread.", 
                                    cur_thr.name().unwrap_or("UNKNOWN"), thr_no, e);
                                continue;
                            }
                        };

                    let Some(prev_task_hndl) = 
                        task_pool_lock.thread_pool.get_mut(thr_no).unwrap().replace(task_handle)
                    else
                    {
                        continue;
                    };

                    drop(task_pool_lock);

                    prev_task_hndl.take_result();
                }
                TaskTask::ExitAll => 
                {
                    let _ = self.task_send_ch.send(TaskTask::ExitAll);
                    
                    break;
                }
            }
        }

        self.tec.exit_ok();
    }
}

/// An inner struct which is shared across threads.
#[derive(Debug)]
struct TaskPoolInner
{
    /// A pool of the threads.
    thread_pool: Vec<Option<TaskHandle>>,

    /// A sender endpoint.
    task_send_ch: Sender<TaskTask>,

    /// A receiver endpoint of the above.
    task_recv_ch: Receiver<TaskTask>,
}

impl TaskPoolInner
{
    fn add_thread(&self, task_pool_inner: Arc<Mutex<TaskPoolInner>>, block_panic: Arc<AtomicBool>, thr_no: usize) -> io::Result<TaskHandle>
    {
        let recv_ch = self.task_recv_ch.clone();
        let tec = TaskExitCatcher::new(self.task_send_ch.clone(), thr_no, block_panic);
        
        let thread_ctx = 
            TaskThread
            {
                thr_id:
                    thr_no,
                inner: 
                    task_pool_inner,
                task_send_ch: 
                    self.task_send_ch.clone(),
                recv_ch,
                tec,                    
            };

        let thb = thread::Builder::new().name(format!("task-{}s", thr_no));

        let handler = thb.spawn(move || TaskThread::thread(thread_ctx))?;

        SPAWNED_THREADS.fetch_add(1, Ordering::SeqCst);

        return Ok(TaskHandle{ handler });
    }

    /// Counts the threads which [TaskHandle] is `some` and thread is `not finished`.
    fn count_live_threads(&self) -> usize
    {
        self.thread_pool.iter().filter(|v| v.is_some() == true && v.as_ref().unwrap().handler.is_finished() == false).count()
    }

    /// Counts the threads which [TaskHandle] is `none` or thread is `finished`.
    fn count_completed_threads(&self) -> usize
    {
        self.thread_pool.iter().filter(|v| v.is_none() == true || v.as_ref().unwrap().handler.is_finished() == true).count()
    }

    fn find_completed_thread_or_unalloc(&self) -> Option<usize>
    {
        self
            .thread_pool
            .iter()
            .enumerate()
            .find(|(_pos, c)| c.is_none()).map(|(pos, _)| pos)
            .map_or_else(
                ||
                {
                    let pos = self.thread_pool.len();
                    if pos == self.thread_pool.capacity()
                    {
                        return None;
                    }
                    
                    return Some(pos);
                }, 
                |f| Some(f)
            )
    }
}


/// A protocol which is used for thread control.
enum TaskTask
{
    /// Exec task.
    Exec(Box<dyn FnMut() + Send + 'static>),

    /// Thread exited.
    ExitReason(TaskExitReason),

    /// All threads should end.
    ExitAll,
}

impl fmt::Debug for TaskTask
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
    {
        match self 
        {
            Self::Exec(_arg0) => 
                f.debug_tuple("Exec").finish(),
            Self::ExitReason(arg0) => 
                f.debug_tuple("ExitReason").field(arg0).finish(),
            Self::ExitAll => 
                f.debug_tuple("ExitAll").finish(),
        }
    }
}

/// The reason task has been completed.
#[derive(Debug)]
enum TaskExitReason
{
    /// Task ended normally.
    /// 
    /// * `0` - task number (in [TaskPoolInner])
    Ok(usize),

    /// Exit reason is panic.
    /// 
    /// * `0` - task number (in [TaskPoolInner])
    Panic(usize),
}

/// Catches the reason the thread exited.
#[derive(Debug)]
struct TaskExitCatcher
{
    /// Is set to true when task pool is destructed.
    block_panic: Arc<AtomicBool>,

    /// A sender endpoint for a reason transmission.
    send: Sender<TaskTask>,

    /// A thread numer which is assigend in [TaskPoolInner]
    thread_no: usize,

    /// A flag which indicates that thread exited normally.
    exit_ok: bool,
}

impl Drop for TaskExitCatcher
{
    fn drop(&mut self) 
    {
        SPAWNED_THREADS.fetch_sub(1, Ordering::SeqCst);

        if self.exit_ok == true || self.block_panic.load(Ordering::SeqCst) == true
        {
            let _ = self.send.send(TaskTask::ExitReason(TaskExitReason::Ok(self.thread_no)));
        }
        else
        {
            let _ = self.send.send(TaskTask::ExitReason(TaskExitReason::Panic(self.thread_no)));
        }
    }
}

impl TaskExitCatcher
{
    fn new(send: Sender<TaskTask>, thread_no: usize, block_panic: Arc<AtomicBool>) -> Self
    {
        Self{ send, thread_no, exit_ok: false, block_panic }
    }

    fn exit_ok(&mut self) 
    {
        self.exit_ok = true;
    }
}

#[derive(Debug)]
struct TaskHandle
{
    handler: JoinHandle<()>,
}

impl TaskHandle
{
    fn take_result(self)
    {
        let thread_name = 
            self.handler.thread().name().map_or("unknown".to_string(), |n| n.to_string());

        match self.handler.join()
        {
            Ok(_) => 
            {
                log::debug!("thread '{}' exited", thread_name);
            },
            Err(e) => 
            {
                log::error!("thread '{}' exit with error: {:?}", thread_name, e);
            }
        }
        
    }
}

/// SIGNAL_ALARM handler. Rings every second.
extern "C" fn signal_alarm(_: nix::libc::c_int) 
{    
    let tasks_cnt = COMPLETED_TASKS.swap(0, Ordering::SeqCst);

    TASKS_PER_SECOND.store(tasks_cnt, Ordering::Release);
    
    // restart alarm
    alarm::set(1);
}

/// Manages a collection of threads.
///
/// A new thread is created every time all the existing threads are full.
/// Any idle thread will automatically die after a few seconds.
#[derive(Debug)]
pub struct TaskPool 
{
    /// The shared inner which threads' handles and channel endpoints.
    inner: Arc<Mutex<TaskPoolInner>>,

    /// A task sender.
    task_submit: Sender<TaskTask>,

    /// When `taskpool` is destruc
    block_panic: Arc<AtomicBool>,
}

impl TaskPool 
{
    /// Minimum number of active threads.
    pub const MIN_THREADS: usize = 4;

    /// Maximum number of active threads.
    pub const MAX_THREADS: usize = 128;

    pub 
    fn new(min_threads_opt: Option<usize>, max_threads_opt: Option<usize>) -> io::Result<TaskPool>
    {
        let sa = 
            SigAction::new(
                SigHandler::Handler(signal_alarm),
                SaFlags::SA_RESTART,
                SigSet::empty()
            );

        unsafe 
        {
            sigaction(Signal::SIGALRM, &sa).unwrap();
        }

        let min_threads = *MIN_THREADS_VAL.get_or_init(|| min_threads_opt.unwrap_or(Self::MIN_THREADS));
        let max_threads = *MAX_THREADS_VAL.get_or_init(|| max_threads_opt.unwrap_or(Self::MAX_THREADS));

        

        let (send, recv) = unbounded::<TaskTask>();
        let task_submit = send.clone();

        let block_panic: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));

        

        let pool_inner = 
            Arc::new(
                Mutex::new(
                    TaskPoolInner 
                    {
                        thread_pool: 
                            Vec::with_capacity(max_threads),
                        task_send_ch:
                            send,
                        task_recv_ch:
                            recv,
                    }
                )
            );

        let pool = 
            Self
            { 
                inner: 
                    pool_inner, 
                task_submit: 
                    task_submit, 
                block_panic: 
                    block_panic.clone(), 
            };

        let mut pool_lock = pool.inner.lock().unwrap();
        

        for i in 0..min_threads 
        {
            let handle = pool_lock.add_thread(pool.inner.clone(), block_panic.clone(), i)?;

            pool_lock.thread_pool.push(Some(handle));
        }

        drop(pool_lock);

        // start alarm
        alarm::set(1);

        return Ok(pool);
    }

    /// Executes a function in a thread.
    /// If no thread is available, spawns a new one.
    pub 
    fn spawn(&self, code: Box<dyn FnMut() + Send>) -> io::Result<()>
    {
        if let Err(_) = self.task_submit.send(TaskTask::Exec(code))
        {
            return Err(
                io::Error::new(ErrorKind::BrokenPipe, format!("submit_task disconnected"))
            );
        }

        // check pressure (todo)
        // i.e check how many messages are in the queue. 
        // and what is the thoughput i.e how many tasks were completed 

        let thr_cnt = SPAWNED_THREADS.load(Ordering::SeqCst);

       //log::debug!("debug: spawned threads: {}, tps: {}", thr_cnt, TASKS_PER_SECOND.load(Ordering::Acquire));

        let max_threads = *MAX_THREADS_VAL.get().unwrap();

        if thr_cnt < max_threads
        {
            let tsk_cnt = self.task_submit.len();
            let tsk_ps = TASKS_PER_SECOND.load(Ordering::Acquire);

            if tsk_ps < tsk_cnt
            {
                let spawn_more = (tsk_cnt - tsk_ps) / thr_cnt / (max_threads - thr_cnt);

                if spawn_more == 0
                {
                    return Ok(());
                }

                log::info!("more {} threads are required to clear the queue len: {}, tasks_per_sec: {}", 
                    spawn_more, tsk_cnt, tsk_ps);

                let thr_max = 
                    if thr_cnt+spawn_more > max_threads
                    {
                        thr_cnt+spawn_more - max_threads
                    }
                    else
                    {
                        spawn_more
                    };

                log::info!("spawning {} threads count: {}, max threads {}", thr_max, thr_cnt, max_threads);

                let mut pool_lock = self.inner.lock().unwrap();
                
                for _ in 0..thr_max
                {
                    let Some(thr_idx) = pool_lock.find_completed_thread_or_unalloc()
                        else
                        {
                            log::error!("cannot spawn thread, cannot find unallocated or finished, spawned_thrs: {}, list len: {}", 
                                thr_cnt, pool_lock.thread_pool.len());

                            return Ok(());
                        };

                    let handle = pool_lock.add_thread(self.inner.clone(), self.block_panic.clone(), thr_idx)?;

                    pool_lock.thread_pool.push(Some(handle));
                }
            }
        }

        return Ok(())
    }

    fn stop_all_internal(&mut self) -> io::Result<()>
    {
        self.block_panic.store(true, Ordering::SeqCst);

        return 
            self
                .task_submit
                .send(TaskTask::ExitAll)
                .map_err(|_e|
                    io::Error::new(ErrorKind::BrokenPipe, "TaskPool::drop() task_bumit send error, channel closed")
                );

    }

    pub 
    fn stop_wait_all(mut self) -> io::Result<()>
    {
        self.stop_wait_all_timeout_internal(60)
    }

    pub 
    fn stop_wait_all_timeout(mut self, timeout_sec: u32) -> io::Result<()>
    {
        self.stop_wait_all_timeout_internal(timeout_sec)
    }

    pub 
    fn stop_wait_all_timeout_internal(&mut self, timeout_sec: u32) -> io::Result<()>
    {
        self.stop_all_internal()?;

        // wait for tasks
        for i in 0..timeout_sec
        {
            std::thread::sleep(Duration::from_millis(999));

            let task_pool_lock = self.inner.lock().unwrap();

            log::debug!("drop() itration: {}, count: {}, len: {}", i, task_pool_lock.count_completed_threads(), task_pool_lock.thread_pool.len());
            if task_pool_lock.count_completed_threads() == task_pool_lock.thread_pool.len()
            {
                break;
            }

            drop(task_pool_lock);

           
        }


        let mut task_pool_lock = self.inner.lock().unwrap();
        for task 
        in 
            task_pool_lock.thread_pool
                .iter_mut().filter(|p| p.is_some() == true).map(|p| p.take().unwrap())
        {
            if task.handler.is_finished() == true
            {
                task.take_result();
            }
            else
            {
                log::error!("stalled thread: {}", task.handler.thread().name().unwrap_or("UNKNOWN"));
            }
        }

        return Ok(());
    }
}

impl Drop for TaskPool 
{
    fn drop(&mut self) 
    {
        if let Err(e) = self.stop_wait_all_timeout_internal(10)
        {
            log::error!("TaskPool::drop() error: {}", e);

            return;
        }
    }
}

#[cfg(test)]
mod test_task_pool
{
    use std::time::{Duration, Instant};

    use super::*;

    fn test_function()
    {
        println!("[{}] function started", std::thread::current().name().unwrap_or("UNKNOWN"));

        let timeout = rand::random_range(1..2000);
        std::thread::sleep(Duration::from_millis(timeout));

        println!("[{}] function completed", std::thread::current().name().unwrap_or("UNKNOWN"));
    }

    fn test_function_panic()
    {
        println!("[{}] function started", std::thread::current().name().unwrap_or("UNKNOWN"));

        panic!("paniking");
    }

    fn test_function_long()
    {
        println!("[{}] function started", std::thread::current().name().unwrap_or("UNKNOWN"));

        let timeout = rand::random_range(5..1000);
        std::thread::sleep(Duration::from_millis(timeout));

        println!("[{}] function completed", std::thread::current().name().unwrap_or("UNKNOWN"));
    }

    #[test]
    fn test_simple()
    {
        let taskpool = TaskPool::new(Some(5), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        taskpool.spawn(Box::new(test_function)).unwrap();
        taskpool.spawn(Box::new(test_function)).unwrap();

        drop(taskpool);
    }

    #[test]
    fn test_simple_thread_destruct()
    {
        let taskpool = TaskPool::new(Some(1), Some(2)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..50
        {
            let timeout = rand::random_range(1..200);
            std::thread::sleep(Duration::from_millis(timeout));

            taskpool.spawn(Box::new(test_function_long)).unwrap();
        }

        std::thread::sleep(Duration::from_secs(15));

        taskpool.stop_wait_all().unwrap();
    }

    #[test]
    fn test_simple_thread_panic()
    {
        let taskpool = TaskPool::new(Some(5), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        taskpool.spawn(Box::new(test_function)).unwrap();
        taskpool.spawn(Box::new(test_function_panic)).unwrap();

        taskpool.stop_wait_all().unwrap();
    }

    #[test]
    fn test_simple_thread_panic2()
    {
        let taskpool = TaskPool::new(Some(2), Some(2)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        taskpool.spawn(Box::new(test_function)).unwrap();
        taskpool.spawn(Box::new(test_function_panic)).unwrap();

        std::thread::sleep(Duration::from_secs(1));
        taskpool.spawn(Box::new(test_function)).unwrap();

        taskpool.stop_wait_all().unwrap();
    }

    #[test]
    fn test_low_load()
    {
        let taskpool = TaskPool::new(Some(5), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..10
        {
            taskpool.spawn(Box::new(test_function)).unwrap();
        }

        drop(taskpool);
    }

    #[test]
    fn test_low_load_single_thread()
    {
        let taskpool = TaskPool::new(Some(1), Some(1)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..10
        {
            taskpool.spawn(Box::new(test_function)).unwrap();
        }

        drop(taskpool);
    }

    #[test]
    fn test_low_load_wait_all()
    {
        let taskpool = TaskPool::new(Some(5), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..10
        {
            taskpool.spawn(Box::new(test_function)).unwrap();
        }

        taskpool.stop_wait_all().unwrap();
    }

    #[test]
    fn test_low_load_low_threads()
    {
        let taskpool = TaskPool::new(Some(1), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..50
        {
            let timeout = rand::random_range(1..200);
            std::thread::sleep(Duration::from_millis(timeout));

            taskpool.spawn(Box::new(test_function_long)).unwrap();
        }

        taskpool.stop_wait_all().unwrap();
    }

    #[test]
    fn test_med_load()
    {
        let taskpool = TaskPool::new(Some(5), Some(8)).unwrap();

        assert_eq!(taskpool.block_panic.load(Ordering::Relaxed), false);

        for _ in 0..40
        {
            let s = Instant::now();
            taskpool.spawn(Box::new(test_function)).unwrap();
            let e = s.elapsed();
            println!("elaspsed: {:?}", e);

            let timeout = rand::random_range(500..1100);
            std::thread::sleep(Duration::from_micros(timeout));
        }

        drop(taskpool);
    }

    
}