| 1 | module veb |
| 2 | |
| 3 | import fasthttp |
| 4 | |
| 5 | @[heap] |
| 6 | pub struct Server { |
| 7 | handle fasthttp.ServerHandle |
| 8 | lifecycle_control bool |
| 9 | } |
| 10 | |
| 11 | fn new_server_with_lifecycle(handle fasthttp.ServerHandle) &Server { |
| 12 | return &Server{ |
| 13 | handle: handle |
| 14 | lifecycle_control: true |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn new_server_without_lifecycle() &Server { |
| 19 | return &Server{} |
| 20 | } |
| 21 | |
| 22 | fn (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. |
| 29 | pub 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. |
| 38 | pub 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 | |