0

I want to know if there will be any synchronization / concurrency related issues ? if we serve a "request" event (in HTTP Server like swoole) using a "Singleton" as "callback to the request event". In other words the callback that serves the request first create a singleton object (if it does not already exists.

I am creating singleton on "request" event of HTTP Server (Swoole). The static member which is used to created single instance also calls a non-static member of the (same) singleton object, in addition. This non-static member actually serves the request.

$http->on('request', 'Bootstrap::getInstance');

//Singleton

class Bootstrap{

  protected static $instance;

  private function __constructor(){  
         //initialize Phalcon Micro Routers and Events to fuse with Swoole
  } 

  public static getInstance($htpRequest, $httpResponse) {
       if (!$instance) { 
         self::$instance = new Bootstrap();
       }
       //Also call the non-static member to serve "request"
       self::instance->run($htpRequest, $httpResponse);
    }

  //non-static function to serve request
  public function run(httpRequest, httpResponse) {
      // Serve Request
  }

}
Fakhar Anwar
  • 295
  • 1
  • 3
  • 20
  • I see no problem, as each request is stateful for itself. – Markus Zeller Apr 27 '20 at 18:01
  • @MarkusZeller what if requests arrive concurrently (in parallel), for example, say, 50 Requests at the same time. How will system deal with those parallel requests? each request needs to pass through a static member of the class. Will each new request wait for the previous request/s to be finished ? before it (the new request) gain control of this static member. – Fakhar Anwar Apr 27 '20 at 21:03
  • As I said, every request is for its own. The requests are distributed. In case of apache2 it takes them and queue them to the worker threads. Swoole can be used to take more requests than apache can do. – Markus Zeller Apr 28 '20 at 06:23

0 Answers0