RESTinio
http_server_run.hpp
Go to the documentation of this file.
1 /*
2  * restinio
3  */
4 
5 /*!
6  * \file
7  * \brief Helper function for simple run of HTTP server.
8  */
9 
10 #pragma once
11 
12 #include <restinio/impl/ioctx_on_thread_pool.hpp>
13 
14 #include <restinio/http_server.hpp>
15 
16 namespace restinio
17 {
18 
19 //
20 // break_signal_handling_t
21 //
22 /*!
23  * @brief Indication of usage of break signal handlers for some forms
24  * of run functions.
25  *
26  * @since v.0.5.1
27  */
29 {
30  //! Signal handler should be used by run() function.
31  used,
32  //! Signal handler should not be used by run() function.
33  skipped
34 };
35 
36 /*!
37  * @brief Make the indicator for usage of break signal handler.
38  *
39  * Usage example:
40  * @code
41  * restinio::run( restinio::on_thread_pool(
42  * std::thread::hardware_concurrency(),
43  * restinio::use_break_signal_handling(),
44  * my_server) );
45  * @endcode
46  *
47  * @since v.0.5.1
48  */
49 inline constexpr break_signal_handling_t
51 {
53 }
54 
55 /*!
56  * @brief Make the indicator for absence of break signal handler.
57  *
58  * Usage example:
59  * @code
60  * restinio::run( restinio::on_thread_pool(
61  * std::thread::hardware_concurrency(),
62  * restinio::skip_break_signal_handling(),
63  * my_server) );
64  * @endcode
65  *
66  * @since v.0.5.1
67  */
68 inline constexpr break_signal_handling_t
70 {
72 }
73 
74 //
75 // run_on_this_thread_settings_t
76 //
77 /*!
78  * \brief Settings for the case when http_server must be run
79  * on the context of the current thread.
80  *
81  * \note
82  * Shouldn't be used directly. Only as result of on_this_thread()
83  * function as parameter for run().
84  */
85 template<typename Traits>
87  : public basic_server_settings_t<
89  Traits>
90 {
93 public:
94  // Inherit constructors from base class.
95  using base_type_t::base_type_t;
96 };
97 
98 //
99 // on_this_thread
100 //
101 /*!
102  * \brief A special marker for the case when http_server must be
103  * run on the context of the current thread.
104  *
105  * Usage example:
106  * \code
107  * // Run with the default traits.
108  * run( restinio::on_this_thread()
109  * .port(8080)
110  * .address("localhost")
111  * .request_handler(...) );
112  * \endcode
113  * For a case when some custom traits must be used:
114  * \code
115  * run( restinio::on_this_thread<my_server_traits_t>()
116  * .port(8080)
117  * .address("localhost")
118  * .request_handler(...) );
119  * \endcode
120  */
121 template<typename Traits = default_single_thread_traits_t>
124 
125 //
126 // run_on_thread_pool_settings_t
127 //
128 /*!
129  * \brief Settings for the case when http_server must be run
130  * on the context of the current thread.
131  *
132  * \note
133  * Shouldn't be used directly. Only as result of on_thread_pool()
134  * function as parameter for run().
135  */
136 template<typename Traits>
138  : public basic_server_settings_t<
140  Traits>
141 {
142  //! Size of the pool.
144 
145 public:
146  //! Constructor.
148  //! Size of the pool.
149  std::size_t pool_size )
151  {}
152 
153  //! Get the pool size.
154  std::size_t
155  pool_size() const { return m_pool_size; }
156 };
157 
158 //
159 // on_thread_pool
160 //
161 /*!
162  * \brief A special marker for the case when http_server must be
163  * run on an thread pool.
164  *
165  * Usage example:
166  * \code
167  * // Run with the default traits.
168  * run( restinio::on_thread_pool(16) // 16 -- is the pool size.
169  * .port(8080)
170  * .address("localhost")
171  * .request_handler(...) );
172  * \endcode
173  * For a case when some custom traits must be used:
174  * \code
175  * run( restinio::on_thread_pool<my_server_traits_t>(16)
176  * .port(8080)
177  * .address("localhost")
178  * .request_handler(...) );
179  * \endcode
180  */
181 template<typename Traits = default_traits_t>
184  //! Size of the pool.
185  std::size_t pool_size )
186 {
188 }
189 
190 //
191 // run()
192 //
193 
194 
195 //! Helper function for running http server until ctrl+c is hit.
196 /*!
197  * Can be useful when RESTinio server should be run on the user's
198  * own io_context instance.
199  *
200  * For example:
201  * \code
202  * asio::io_context iosvc;
203  * ... // iosvc used by user.
204  * restinio::run(iosvc,
205  * restinio::on_this_thread<my_traits>()
206  * .port(8080)
207  * .address("localhost")
208  * .request_handler([](auto req) {...}));
209  * \endcode
210  *
211  * \since
212  * v.0.4.2
213  */
214 template<typename Traits>
215 inline void
217  //! Asio's io_context to be used.
218  //! Note: this reference should remain valid until RESTinio server finished.
219  asio_ns::io_context & ioctx,
220  //! Settings for that server instance.
221  run_on_this_thread_settings_t<Traits> && settings )
222 {
224  using server_t = http_server_t<Traits>;
225 
229 
231 
234  [&]( const asio_ns::error_code & ec, int ){
235  if( !ec )
236  {
238  [&]{
239  // Stop running io_service.
240  ioctx.stop();
241  },
243  // We can't throw an exception here!
244  // Store it to rethrow later.
246  } );
247  }
248  } );
249 
251  []{ /* Ok. */},
253  // Stop running io_service.
254  // We can't throw an exception here!
255  // Store it to rethrow later.
256  ioctx.stop();
258  } );
259 
260  ioctx.run();
261 
262  // If an error was detected it should be propagated.
263  if( exception_caught )
265 }
266 
267 //! Helper function for running http server until ctrl+c is hit.
268 /*!
269  * This function creates its own instance of Asio's io_context and
270  * uses it exclusively.
271  *
272  * Usage example:
273  * \code
274  * restinio::run(
275  * restinio::on_this_thread<my_traits>()
276  * .port(8080)
277  * .address("localhost")
278  * .request_handler([](auto req) {...}));
279  * \endcode
280  */
281 template<typename Traits>
282 inline void
284  run_on_this_thread_settings_t<Traits> && settings )
285 {
288 }
289 
290 namespace impl {
291 
292 /*!
293  * \brief An implementation of run-function for thread pool case.
294  *
295  * This function receives an already created thread pool object and
296  * creates and runs http-server on this thread pool.
297  *
298  * \since
299  * v.0.4.2
300  */
301 template<typename Io_Context_Holder, typename Traits>
302 void
306 {
308  using server_t = http_server_t<Traits>;
309 
313 
315 
318  [&]( const asio_ns::error_code & ec, int ){
319  if( !ec )
320  {
322  [&]{
323  // Stop running io_service.
324  pool.stop();
325  },
327  // We can't throw an exception here!
328  // Store it to rethrow later.
330  } );
331  }
332  } );
333 
335  []{ /* Ok. */},
337  // Stop running io_service.
338  // We can't throw an exception here!
339  // Store it to rethrow later.
340  pool.stop();
342  } );
343 
344  pool.start();
345  pool.wait();
346 
347  // If an error was detected it should be propagated.
348  if( exception_caught )
350 }
351 
352 } /* namespace impl */
353 
354 //! Helper function for running http server until ctrl+c is hit.
355 /*!
356  * This function creates its own instance of Asio's io_context and
357  * uses it exclusively.
358  *
359  * Usage example:
360  * \code
361  * restinio::run(
362  * restinio::on_thread_pool<my_traits>(4)
363  * .port(8080)
364  * .address("localhost")
365  * .request_handler([](auto req) {...}));
366  * \endcode
367  */
368 template<typename Traits>
369 inline void
370 run( run_on_thread_pool_settings_t<Traits> && settings )
371 {
374 
376 
377  impl::run( pool, std::move(settings) );
378 }
379 
380 //! Helper function for running http server until ctrl+c is hit.
381 /*!
382  * Can be useful when RESTinio server should be run on the user's
383  * own io_context instance.
384  *
385  * For example:
386  * \code
387  * asio::io_context iosvc;
388  * ... // iosvc used by user.
389  * restinio::run(iosvc,
390  * restinio::on_thread_pool<my_traits>(4)
391  * .port(8080)
392  * .address("localhost")
393  * .request_handler([](auto req) {...}));
394  * \endcode
395  *
396  * \since
397  * v.0.4.2
398  */
399 template<typename Traits>
400 inline void
402  //! Asio's io_context to be used.
403  //! Note: this reference should remain valid until RESTinio server finished.
404  asio_ns::io_context & ioctx,
405  //! Settings for that server instance.
406  run_on_thread_pool_settings_t<Traits> && settings )
407 {
410 
412 
413  impl::run( pool, std::move(settings) );
414 }
415 
416 //
417 // run_existing_server_on_thread_pool_t
418 //
419 /*!
420  * @brief Helper type for holding parameters necessary for running
421  * HTTP-server on a thread pool.
422  *
423  * @note This class is not intended for direct use. It is used by
424  * RESTinio itself.
425  *
426  * @since v.0.5.1
427  */
428 template<typename Traits>
430 {
431  //! Size of thread pool.
433  //! Should break signal handler be used?
435  //! HTTP-server to be used on a thread pool.
436  /*!
437  * We assume that this pointer will be valid pointer.
438  */
440 
441 public:
442  //! Initializing constructor.
444  //! Size of the pool.
445  std::size_t pool_size,
446  //! Should break signal handler be used?
447  break_signal_handling_t break_handling,
448  //! A reference to HTTP-server to be run on a thread pool.
449  //! This reference should outlive an instance of
450  //! run_existing_server_on_thread_pool_t.
451  http_server_t<Traits> & server )
454  , m_server{ &server }
455  {}
456 
457  std::size_t
458  pool_size() const noexcept { return m_pool_size; }
459 
461  break_handling() const noexcept { return m_break_handling; }
462 
464  server() const noexcept { return *m_server; }
465 };
466 
467 /*!
468  * @brief Helper function for running an existing HTTP-server on
469  * a thread pool.
470  *
471  * Usage example:
472  * @code
473  * using my_server_t = restinio::http_server_t< my_server_traits_t >;
474  * my_server_t server{
475  * restinio::own_io_context(),
476  * [](auto & settings) {
477  * settings.port(...);
478  * settings.address(...);
479  * settings.request_handler(...);
480  * ...
481  * }
482  * };
483  * ...
484  * restinio::run( restinio::on_thread_pool(
485  * std::thread::hardware_concurrency(),
486  * restinio::use_break_signal_handling(),
487  * server) );
488  * @endcode
489  *
490  * @since v.0.5.1
491  */
492 template<typename Traits>
495  std::size_t pool_size,
496  break_signal_handling_t break_handling,
497  http_server_t<Traits> & server )
498 {
499  return { pool_size, break_handling, server };
500 }
501 
502 namespace impl {
503 
504 /*!
505  * \brief An implementation of run-function for thread pool case
506  * with existing http_server instance.
507  *
508  * This function receives an already created thread pool object and
509  * already created http-server and run it on this thread pool.
510  *
511  * \attention
512  * This function installs break signal handler and stops server when
513  * break signal is raised.
514  *
515  * \since
516  * v.0.5.1
517  */
518 template<typename Io_Context_Holder, typename Traits>
519 void
523 {
525 
528  [&]( const asio_ns::error_code & ec, int ){
529  if( !ec )
530  {
532  [&]{
533  // Stop running io_service.
534  pool.stop();
535  },
537  // We can't throw an exception here!
538  // Store it to rethrow later.
540  } );
541  }
542  } );
543 
545  []{ /* Ok. */},
547  // Stop running io_service.
548  // We can't throw an exception here!
549  // Store it to rethrow later.
550  pool.stop();
552  } );
553 
554  pool.start();
555  pool.wait();
556 
557  // If an error was detected it should be propagated.
558  if( exception_caught )
560 }
561 
562 /*!
563  * \brief An implementation of run-function for thread pool case
564  * with existing http_server instance.
565  *
566  * This function receives an already created thread pool object and
567  * already created http-server and run it on this thread pool.
568  *
569  * \note
570  * This function doesn't install break signal handlers.
571  *
572  * \since
573  * v.0.5.1
574  */
575 template<typename Io_Context_Holder, typename Traits>
576 void
580 {
582 
584  []{ /* Ok. */},
586  // Stop running io_service.
587  // We can't throw an exception here!
588  // Store it to rethrow later.
589  pool.stop();
591  } );
592 
593  pool.start();
594  pool.wait();
595 
596  // If an error was detected it should be propagated.
597  if( exception_caught )
599 }
600 
601 } /* namespace impl */
602 
603 /*!
604  * @brief Helper function for running an existing HTTP-server on
605  * a thread pool.
606  *
607  * Usage example:
608  * @code
609  * using my_server_t = restinio::http_server_t< my_server_traits_t >;
610  * my_server_t server{
611  * restinio::own_io_context(),
612  * [](auto & settings) {
613  * settings.port(...);
614  * settings.address(...);
615  * settings.request_handler(...);
616  * ...
617  * }
618  * };
619  * ...
620  * // run() returns if Ctrl+C is pressed or if HTTP-server will
621  * // be shut down from elsewhere.
622  * restinio::run( restinio::on_thread_pool(
623  * std::thread::hardware_concurrency(),
624  * restinio::use_break_signal_handling(),
625  * server) );
626  * @endcode
627  *
628  * @since v.0.5.1
629  */
630 template<typename Traits>
631 inline void
633 {
636 
638 
641  else
643 }
644 
645 //
646 // initiate_shutdown
647 //
648 /*!
649  * @brief Helper function for initiation of server shutdown.
650  *
651  * Can be useful if an existing HTTP-server is run via run() function.
652  * For example:
653  * @code
654  * restinio::http_server_t< my_traits > server{ ... };
655  * // Launch another thread that will perform some application logic.
656  * std::thread app_logic_thread{ [&server] {
657  * while(some_condition) {
658  * ...
659  * if(exit_case) {
660  * // HTTP-server should be shut down.
661  * restinio::initiate_shutdown( server );
662  * // Our work should be finished.
663  * return;
664  * }
665  * }
666  * } };
667  * // Start HTTP-server. The current thread will be blocked until
668  * // run() returns.
669  * restinio::run( restinio::on_thread_pool(
670  * 4,
671  * restinio::skip_break_signal_handling(),
672  * server) );
673  * // Now app_logic_thread can be joined.
674  * app_logic_thread.join();
675  * @endcode
676  *
677  * @since v.0.5.1
678  */
679 template<typename Traits>
680 inline void
682 {
683  server.io_context().post( [&server] {
684  server.close_sync();
685  server.io_context().stop();
686  } );
687 }
688 
689 //
690 // on_pool_runner_t
691 //
692 /*!
693  * @brief Helper class for running an existing HTTP-server on a thread pool
694  * without blocking the current thread.
695  *
696  * Usage of run() functions has some drawbacks. For example, the current thread
697  * on that run() is called, will be blocked until run() returns.
698  *
699  * Sometimes it is not appropriate and leads to tricks like that:
700  * @code
701  * // HTTP-server to be run on a thread pool.
702  * restinio::http_server_t< my_traits > server{...};
703  *
704  * // Separate worker thread for calling restinio::run().
705  * std::thread run_thread{ [&server] {
706  * restinio::run( restinio::on_thread_pool(
707  * 16,
708  * restinio::skip_break_signal_handling(),
709  * server) );
710  * // Now this thread is blocked until HTTP-server will be finished.
711  * } };
712  *
713  * ... // Some application specific code here.
714  *
715  * // Now the server can be stopped.
716  * restinio::initiate_shutdown( server );
717  * run_thread.join();
718  * @endcode
719  *
720  * Writing such code is a boring and error-prone task. The class
721  * on_pool_runner_t can be used instead:
722  * @code
723  * // HTTP-server to be run on a thread pool.
724  * restinio::http_server_t< my_traits > server{...};
725  *
726  * // Launch HTTP-server on a thread pool.
727  * restinio::on_pool_runner_t< restinio::http_server_t<my_traits> > runner{
728  * 16,
729  * server
730  * };
731  * runner.start();
732  *
733  * ... // Some application specific code here.
734  *
735  * // Now the server can be stopped.
736  * runner.stop(); // (1)
737  * runner.wait();
738  * @endcode
739  *
740  * Moreover the code at point (1) in the example above it not necessary
741  * because on_pool_runner_t automatically stops the server in the destructor.
742  *
743  * @since v.0.5.1
744  */
745 template<typename Http_Server>
747 {
748  //! HTTP-server to be run.
749  Http_Server & m_server;
750 
751  //! Thread pool for running the server.
754 
755 public :
756  on_pool_runner_t( const on_pool_runner_t & ) = delete;
757  on_pool_runner_t( on_pool_runner_t && ) = delete;
758 
759  //! Initializing constructor.
761  //! Size of thread pool.
762  std::size_t pool_size,
763  //! Server instance to be run.
764  //! NOTE. This reference must be valid for all life-time
765  //! of on_pool_runner instance.
766  Http_Server & server )
767  : m_server{ server }
769  {}
770 
771  /*!
772  * @brief Start the server with callbacks that will be called on
773  * success or failure.
774  *
775  * The @a on_ok should be a function/functor with the format:
776  * @code
777  * void () noexcept;
778  * @endcode
779  *
780  * The @a on_error should be a function/functor with the format:
781  * @code
782  * void (std::exception_ptr) noexcept;
783  * @endcode
784  *
785  * @note
786  * Both callbacks will be passed to http_server_t::open_async method.
787  * It means that @a on_error callback will be called for errors detected
788  * by open_async() methods.
789  *
790  * @attention
791  * Both callbacks should be noexcept functions/functors.
792  *
793  * Usage example:
794  * @code
795  * using my_http_server = restinio::http_server_t<some_traits>;
796  *
797  * my_http_server server{...};
798  * restinio::on_pool_runner_t<my_http_server> runner{16, server};
799  *
800  * std::promise<void> run_promise;
801  * auto run_future = run_promise.get_future();
802  * runner.start(
803  * // Ok callback.
804  * [&run_promise]() noexcept {
805  * run_promise.set_value();
806  * },
807  * // Error callback.
808  * [&run_promise](std::exception_ptr ex) noexcept {
809  * run_promise.set_exception(std::move(ex));
810  * });
811  * // Wait while HTTP-server started (or start failed).
812  * run_future.get();
813  * @endcode
814  *
815  * @since v.0.6.7
816  */
817  template<
818  typename On_Ok_Callback,
819  typename On_Error_Callback >
820  void
822  //! A callback to be called if HTTP-server started successfully.
823  On_Ok_Callback && on_ok,
824  //! A callback to be called if HTTP-server is not started by
825  //! some reasons. Please note that this callback is passed
826  //! to http_server_t::open_async() and will be called only
827  //! for errors detected by open_async() methods.
828  //! If some error is detected outside of open_async() (for
829  //! example a failure to start a thread pool) then on_error
830  //! callback won't be called.
831  On_Error_Callback && on_error )
832  {
833  static_assert( noexcept(on_ok()), "On_Ok_Callback should be noexcept" );
834  static_assert( noexcept(on_error(std::declval<std::exception_ptr>())),
835  "On_Error_Callback should be noexcept" );
836 
838  [callback = std::move(on_ok)]{ callback(); },
839  [this, callback = std::move(on_error)]( std::exception_ptr ex ){
840  // There is no sense to run pool.
841  m_pool.stop();
842 
843  callback( std::move(ex) );
844  } );
845 
846  m_pool.start();
847  }
848 
849  //! Start the server.
850  /*!
851  * It just a shorthand for a version of `start` method with callbacks
852  * where all callbacks to nothing.
853  */
854  void
856  {
857  this->start(
858  []() noexcept { /* nothing to do */ },
859  []( std::exception_ptr ) noexcept { /* nothing to do */ } );
860  }
861 
862  //! Is server started.
863  bool
864  started() const noexcept { return m_pool.started(); }
865 
866  //FIXME: there should be a version of stop() with callbacks like
867  //for start() method above.
868  //! Stop the server.
869  /*!
870  * @note
871  * This method is noexcept since v.0.6.7
872  */
873  void
874  stop() noexcept
875  {
877  [this]{
878  // Stop running io_service.
879  m_pool.stop();
880  },
881  []( std::exception_ptr /*ex*/ ){
882  //FIXME: the exception should be stored to be handled
883  //later in wait() method.
884  //NOTE: this fix is planned for v.0.7.0.
885  //std::rethrow_exception( ex );
886  } );
887  }
888 
889  //FIXME this method should be replaced by two new method in v.0.7.0:
890  //
891  // enum class action_on_exception_t { drop, rethrow };
892  // wait(action_on_exception_t action);
893  //
894  // template<typename Exception_Handler>
895  // wait(Exception_Handler && on_exception);
896  //
897  //! Wait for full stop of the server.
898  /*!
899  * @note
900  * This method is noexcept since v.0.6.7
901  */
902  void
903  wait() noexcept { m_pool.wait(); }
904 };
905 
906 // Forward declaration.
907 // It's necessary for running_server_handle_t.
908 template< typename Http_Server >
909 class running_server_instance_t;
910 
911 //
912 // running_server_handle_t
913 //
914 /*!
915  * @brief The type to be used as a handle for running server instance.
916  *
917  * The handle should be seen as a Moveable and not Copyable type.
918  *
919  * @since v.0.6.7
920  */
921 template< typename Traits >
924 
925 //
926 // running_server_instance_t
927 //
928 /*!
929  * @brief A helper class used in an implementation of #run_async function.
930  *
931  * An instance of that class holds an HTTP-server and thread pool on that
932  * this HTTP-server is launched.
933  *
934  * The HTTP-server will automatically be stopped in the destructor.
935  * However, a user can stop the HTTP-server manually by using
936  * stop() and wait() methods.
937  *
938  * @since v.0.6.7
939  */
940 template< typename Http_Server >
941 class running_server_instance_t
942 {
943  template< typename Traits >
945  run_async(
949 
950  //! Actual server instance.
951  Http_Server m_server;
952 
953  //! The runner of the server.
954  on_pool_runner_t< Http_Server > m_runner;
955 
956  //! Initializing constructor.
958  io_context_holder_t io_context,
959  server_settings_t< typename Http_Server::traits_t > && settings,
960  std::size_t thread_pool_size )
961  : m_server{ std::move(io_context), std::move(settings) }
962  , m_runner{ thread_pool_size, m_server }
963  {}
964 
965 
966  //! Start the HTTP-server.
967  /*!
968  * Returns when HTTP-server started or some startup failure detected.
969  * It means that the caller thread will be blocked until HTTP-server
970  * calls on_ok or on_error callback.
971  *
972  * Throws an exception on an error.
973  */
974  void
976  {
977  std::promise<void> p;
978  auto f = p.get_future();
979  m_runner.start(
980  [&p]() noexcept { p.set_value(); },
981  [&p]( std::exception_ptr ex ) noexcept {
982  p.set_exception( std::move(ex) );
983  } );
984  f.get();
985  }
986 
987 public :
988  /*!
989  * Stop the HTTP-server.
990  *
991  * This method initiates shutdown procedure that can take some
992  * time. But stop() returns without the waiting for the completeness
993  * of the shutdown. To wait for the completeness use wait() method:
994  *
995  * @code
996  * auto server = restinio::run_async(...);
997  * ...
998  * server->stop(); // Returns without the waiting.
999  * ... // Some other actions.
1000  * server->wait(); // Returns only when HTTP-server stopped.
1001  * @endcode
1002  *
1003  * @attention
1004  * The current version doesn't guarantee that stop() can be called
1005  * safely several times. Please take care of that and call stop()
1006  * only once.
1007  */
1008  void
1009  stop() noexcept
1010  {
1011  m_runner.stop();
1012  }
1013 
1014  /*!
1015  * @brief Wait for the shutdown of HTTP-server.
1016  *
1017  * @note
1018  * Method stop() should be called before the call to wait():
1019  * @code
1020  * auto server = restinio::run_async(...);
1021  * ...
1022  * server->stop(); // Initiates the shutdown and returns without the waiting.
1023  * server->wait(); // Returns only when HTTP-server stopped.
1024  * @endcode
1025  *
1026  * @attention
1027  * The current version doesn't guarantee that wait() can be called
1028  * safely several times. Please take care of that and call wait()
1029  * only once.
1030  */
1031  void
1032  wait() noexcept
1033  {
1034  m_runner.wait();
1035  }
1036 };
1037 
1038 //
1039 // run_async
1040 //
1041 /*!
1042  * @brief Creates an instance of HTTP-server and launches it on a
1043  * separate thread or thread pool.
1044  *
1045  * Usage example:
1046  * @code
1047  * int main() {
1048  * auto server = restinio::run_async(
1049  * // Asio's io_context to be used.
1050  * // HTTP-server will use own Asio's io_context object.
1051  * restinio::own_io_context(),
1052  * // The settings for the HTTP-server.
1053  * restinio::server_settings_t{}
1054  * .address("127.0.0.1")
1055  * .port(8080)
1056  * .request_handler(...),
1057  * // The size of thread-pool for the HTTP-server.
1058  * 16);
1059  * // If we are here and run_async doesn't throw then HTTP-server
1060  * // is started.
1061  *
1062  * ... // Some other actions.
1063  *
1064  * // No need to stop HTTP-server manually. It will be automatically
1065  * // stopped in the destructor of `server` object.
1066  * }
1067  * @endcode
1068  * Or, if user-defined traits should be used:
1069  * @code
1070  * int main() {
1071  * struct my_traits : public restinio::default_traits_t {
1072  * ...
1073  * };
1074  *
1075  * auto server = restinio::run_async<my_traits>(
1076  * restinio::own_io_context(),
1077  * restinio::server_settings_t<my_traits>{}
1078  * .address(...)
1079  * .port(...)
1080  * .request_handler(...),
1081  * // Use just one thread for the HTTP-server.
1082  * 1u);
1083  *
1084  * ... // Some other actions.
1085  * }
1086  * @endcode
1087  *
1088  * run_async() returns control when HTTP-server is started or some
1089  * startup failure is detected. But if a failure is detected then an
1090  * exception is thrown. So if run_async() returns successfuly then
1091  * HTTP-server is working.
1092  *
1093  * The started HTTP-server will be automatically stopped at the
1094  * destruction of the returned value. Because of that the returned
1095  * value should be stored for the time while HTTP-server is needed.
1096  *
1097  * The started HTTP-server can be stopped manually by calling
1098  * stop() and wait() methods:
1099  * @code
1100  * auto server = restinio::run_async(...);
1101  * ...
1102  * server->stop(); // Returns without the waiting.
1103  * ... // Some other actions.
1104  * server->wait(); // Returns only when HTTP-server stopped.
1105  * @endcode
1106  *
1107  * @since v.0.6.7
1108  */
1109 template< typename Traits = default_traits_t >
1110 RESTINIO_NODISCARD
1111 running_server_handle_t< Traits >
1116 {
1119  std::move(io_context),
1120  std::move(settings),
1122  };
1123 
1124  handle->start();
1125 
1126  return handle;
1127 }
1128 
1129 } /* namespace restinio */
void initiate_shutdown(http_server_t< Traits > &server)
Helper function for initiation of server shutdown.
void run(run_on_thread_pool_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
void run_without_break_signal_handling(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, http_server_t< Traits > &server)
An implementation of run-function for thread pool case with existing http_server instance.
void wait() noexcept
Wait for the shutdown of HTTP-server.
std::size_t m_pool_size
Size of the pool.
Helper type for holding parameters necessary for running HTTP-server on a thread pool.
void run(run_existing_server_on_thread_pool_t< Traits > &&params)
Helper function for running an existing HTTP-server on a thread pool.
run_existing_server_on_thread_pool_t(std::size_t pool_size, break_signal_handling_t break_handling, http_server_t< Traits > &server)
Initializing constructor.
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread...
constexpr break_signal_handling_t skip_break_signal_handling() noexcept
Make the indicator for absence of break signal handler.
void run(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, run_on_thread_pool_settings_t< Traits > &&settings)
An implementation of run-function for thread pool case.
friend running_server_handle_t< Traits > run_async(io_context_holder_t, server_settings_t< Traits > &&, std::size_t thread_pool_size)
Creates an instance of HTTP-server and launches it on a separate thread or thread pool...
Helper class for running an existing HTTP-server on a thread pool without blocking the current thread...
void stop() noexcept
Stop the server.
Http_Server m_server
Actual server instance.
std::size_t m_pool_size
Size of thread pool.
void run(run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
on_pool_runner_t(std::size_t pool_size, Http_Server &server)
Initializing constructor.
http_server_t< Traits > * m_server
HTTP-server to be used on a thread pool.
Http_Server & m_server
HTTP-server to be run.
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
running_server_instance_t(io_context_holder_t io_context, server_settings_t< typename Http_Server::traits_t > &&settings, std::size_t thread_pool_size)
Initializing constructor.
run_existing_server_on_thread_pool_t< Traits > on_thread_pool(std::size_t pool_size, break_signal_handling_t break_handling, http_server_t< Traits > &server)
Helper function for running an existing HTTP-server on a thread pool.
break_signal_handling_t
Indication of usage of break signal handlers for some forms of run functions.
void wait() noexcept
Wait for full stop of the server.
void start()
Start the server.
on_pool_runner_t(on_pool_runner_t &&)=delete
break_signal_handling_t m_break_handling
Should break signal handler be used?
constexpr break_signal_handling_t use_break_signal_handling() noexcept
Make the indicator for usage of break signal handler.
on_pool_runner_t(const on_pool_runner_t &)=delete
void run_with_break_signal_handling(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, http_server_t< Traits > &server)
An implementation of run-function for thread pool case with existing http_server instance.
Settings for the case when http_server must be run on the context of the current thread.
std::size_t pool_size() const
Get the pool size.
RESTINIO_NODISCARD char to_lower_case(unsigned char ch)
bool started() const noexcept
Is server started.
run_on_thread_pool_settings_t(std::size_t pool_size)
Constructor.
Settings for the case when http_server must be run on the context of the current thread.
run_on_thread_pool_settings_t< Traits > on_thread_pool(std::size_t pool_size)
A special marker for the case when http_server must be run on an thread pool.
Signal handler should not be used by run() function.
void start(On_Ok_Callback &&on_ok, On_Error_Callback &&on_error)
Start the server with callbacks that will be called on success or failure.
void start()
Start the HTTP-server.
void run(asio_ns::io_context &ioctx, run_on_thread_pool_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
Signal handler should be used by run() function.
break_signal_handling_t break_handling() const noexcept
on_pool_runner_t< Http_Server > m_runner
The runner of the server.
http_server_t< Traits > & server() const noexcept
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64
impl::ioctx_on_thread_pool_t< impl::external_io_context_for_thread_pool_t > m_pool
Thread pool for running the server.