lundi 27 avril 2020

PHP Singleton as CallBack for HTTP Request in HTTP Server

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
  }

}

Aucun commentaire:

Enregistrer un commentaire