Skip to content

Commit

Permalink
Group pushing the notifications
Browse files Browse the repository at this point in the history
For notify() this doesn't make any difference for now,
but for markProcessed() it reduces the number of connections drastically.

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jun 4, 2020
1 parent ceb6e35 commit f6bc58c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public function setOutput(OutputInterface $output): void {
public function notify(INotification $notification): void {
$notificationId = $this->handler->add($notification);

$this->push->deferPayloads();
try {
$notificationToPush = $this->handler->getById($notificationId, $notification->getUser());
$this->push->pushToDevice($notificationId, $notificationToPush);
} catch (NotificationNotFoundException $e) {
throw new \InvalidArgumentException('Error while preparing push notification');
}
$this->push->flushPayloads();
}

/**
Expand All @@ -74,10 +76,12 @@ public function getCount(INotification $notification): int {
public function markProcessed(INotification $notification): void {
$deleted = $this->handler->delete($notification);

$this->push->deferPayloads();
foreach ($deleted as $user => $notifications) {
foreach ($notifications as $notificationId) {
$this->push->pushDeleteToDevice($user, $notificationId);
}
}
$this->push->flushPayloads();
}
}
40 changes: 29 additions & 11 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class Push {
/** @var OutputInterface */
protected $output;

/** @var array */
protected $payloadsToSend = [];
/** @var bool */
protected $deferPayloads = false;

public function __construct(IDBConnection $connection, INotificationManager $notificationManager, IConfig $config, IProvider $tokenProvider, Manager $keyManager, IUserManager $userManager, IClientService $clientService, ILogger $log) {
$this->db = $connection;
$this->notificationManager = $notificationManager;
Expand All @@ -79,6 +84,15 @@ protected function printInfo(string $message): void {
}
}

public function deferPayloads(): void {
$this->deferPayloads = true;
}

public function flushPayloads(): void {
$this->deferPayloads = false;
$this->sendNotificationsToProxies();
}

public function pushToDevice(int $id, INotification $notification, ?OutputInterface $output = null): void {
$user = $this->userManager->get($notification->getUser());
if (!($user instanceof IUser)) {
Expand Down Expand Up @@ -120,7 +134,6 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
});
$hasTalkApps = !empty($talkApps);

$pushNotifications = [];
foreach ($devices as $device) {
$this->printInfo('');
$this->printInfo('Device token:' . $device['token']);
Expand All @@ -146,10 +159,10 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
$payload = json_encode($this->encryptAndSign($userKey, $device, $id, $notification, $isTalkNotification));

$proxyServer = rtrim($device['proxyserver'], '/');
if (!isset($pushNotifications[$proxyServer])) {
$pushNotifications[$proxyServer] = [];
if (!isset($this->payloadsToSend[$proxyServer])) {
$this->payloadsToSend[$proxyServer] = [];
}
$pushNotifications[$proxyServer][] = $payload;
$this->payloadsToSend[$proxyServer][] = $payload;
} catch (InvalidTokenException $e) {
// Token does not exist anymore, should drop the push device entry
$this->printInfo('InvalidTokenException is thrown');
Expand All @@ -160,7 +173,9 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
}
}

$this->sendNotificationsToProxies($pushNotifications);
if (!$this->deferPayloads) {
$this->sendNotificationsToProxies();
}
}

public function pushDeleteToDevice(string $userId, int $notificationId): void {
Expand All @@ -175,16 +190,15 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}

$userKey = $this->keyManager->getKey($user);
$pushNotifications = [];
foreach ($devices as $device) {
try {
$payload = json_encode($this->encryptAndSignDelete($userKey, $device, $notificationId));

$proxyServer = rtrim($device['proxyserver'], '/');
if (!isset($pushNotifications[$proxyServer])) {
$pushNotifications[$proxyServer] = [];
if (!isset($this->payloadsToSend[$proxyServer])) {
$this->payloadsToSend[$proxyServer] = [];
}
$pushNotifications[$proxyServer][] = $payload;
$this->payloadsToSend[$proxyServer][] = $payload;
} catch (InvalidTokenException $e) {
// Token does not exist anymore, should drop the push device entry
$this->deletePushToken($device['token']);
Expand All @@ -194,10 +208,14 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}
}

$this->sendNotificationsToProxies($pushNotifications);
if (!$this->deferPayloads) {
$this->sendNotificationsToProxies();
}
}

protected function sendNotificationsToProxies(array $pushNotifications): void {
protected function sendNotificationsToProxies(): void {
$pushNotifications = $this->payloadsToSend;
$this->payloadsToSend = [];
if (empty($pushNotifications)) {
return;
}
Expand Down

0 comments on commit f6bc58c

Please sign in to comment.