v2 / vlib / veb / server_new_veb.v
44 lines · 37 sloc · 1.1 KB · 307a670c1fdd44c017d91cb91517658594cefbd0
Raw
1module veb
2
3import fasthttp
4
5@[heap]
6pub struct Server {
7 handle fasthttp.ServerHandle
8 lifecycle_control bool
9}
10
11fn new_server_with_lifecycle(handle fasthttp.ServerHandle) &Server {
12 return &Server{
13 handle: handle
14 lifecycle_control: true
15 }
16}
17
18fn new_server_without_lifecycle() &Server {
19 return &Server{}
20}
21
22fn (s &Server) ensure_lifecycle_control() ! {
23 if !s.lifecycle_control {
24 return error('veb server lifecycle control is not available with SSL')
25 }
26}
27
28// wait_till_running waits until the server starts accepting requests.
29pub fn (s &Server) wait_till_running(params WaitTillRunningParams) !int {
30 s.ensure_lifecycle_control()!
31 return s.handle.wait_till_running(fasthttp.WaitTillRunningParams{
32 max_retries: params.max_retries
33 retry_period_ms: params.retry_period_ms
34 })!
35}
36
37// shutdown gracefully stops accepting new requests and waits for in-flight requests to finish.
38pub fn (s &Server) shutdown(params ShutdownParams) ! {
39 s.ensure_lifecycle_control()!
40 s.handle.shutdown(fasthttp.ShutdownParams{
41 timeout: params.timeout
42 retry_period_ms: params.retry_period_ms
43 })!
44}
45