Way*_*her 3 curl service-provider laravel
具体来说,我正在做的是在我的 AppServiceProvider->boot() 方法中,我正在创建一个如下所示的单例类:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton('App\Support\PushNotificationHelper', function ($app) {
return new PushNotificationHelper();
});
}
}
Run Code Online (Sandbox Code Playgroud)
我用于将通知推送到移动应用程序的队列工作器作业需要 helper 类。当移动设备是 Apple 设备时,我需要建立 curl 连接并使连接在队列工作器作业的生命周期后持续存在。这就是为什么我使用单例来保持连接,如:
class PushNotificationHelper {
protected $http2Connection;
protected $http2Expire ;
public function getConnection($options) {
$this->http2Connection = curl_init();
curl_setopt_array($this->http2Connection, $options);
return $this->http2Connection;
}
Run Code Online (Sandbox Code Playgroud)
Apple 声称如果我反复连接和断开连接,那么他们将发出拒绝服务 (DOS)。我的应用程序每小时发送 1000 条通知。每当我使用连接时,我都会检查错误并在需要时关闭/重新打开连接,例如:
curl_close($http2Connection);
Run Code Online (Sandbox Code Playgroud)
但是,我想知道如何检测应用程序何时会永久关闭,以便我可以正常关闭连接。如果没有办法做到这一点,随着时间的推移,它会因为打开连接挂起而损害我的服务器,如果应用程序每天启动/停止几次,可以说运行数月吗?
另一种选择可能是有一个 curl 选项来告诉连接在很长时间后自动断开连接。(我每 4 小时强制关闭并重新打开一次)所以如果我可以告诉连接在至少 5 小时后自动关闭,那么它可能会自我清洁?
恕我直言,您可以尝试向您的应用程序实例添加终止回调,例如在AppServiceProvider,即:
public function boot()
{
$this->app->terminating(function () {
// your terminating code here
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |