Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelize Notifications #20

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/Listener/CalendarListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCA\DAV\Events\CardUpdatedEvent;

use Psr\Log\LoggerInterface;
use GuzzleHttp\Promise;

use OCA\DavPush\Service\SubscriptionService;
use OCA\DavPush\Transport\TransportManager;
Expand All @@ -59,13 +60,16 @@ public function handle(Event $event): void {
$collectionName = $event->getCalendarData()['uri'];
$subscriptions = $this->subscriptionService->findAll($collectionName);

foreach($subscriptions as $subscription) {
$transport = $this->transportManager->getTransport($subscription->getTransport());
try {
$transport->notify($subscription->getUserId(), $collectionName, $subscription->getId());
} catch (\Exception $e) {
$this->logger->error("transport " . $subscription->getTransport() . " failed to deliver notification to subscription " . $subscription->getId());
$notificationPromises = (function () use ($collectionName, $subscriptions): Generator {
JonathanTreffler marked this conversation as resolved.
Show resolved Hide resolved
foreach($subscriptions as $subscription) {
$transport = $this->transportManager->getTransport($subscription->getTransport());
yield $transport->notify($subscription->getUserId(), $collectionName, $subscription->getId());
}
}
})();

$responses = Promise\Utils::settle($notificationPromises)->wait();

// TODO: iterate over responses and log errors
// $this->logger->error("transport " . $subscription->getTransport() . " failed to deliver notification to subscription " . $subscription->getId());
}
}
22 changes: 13 additions & 9 deletions lib/PushTransports/WebPushTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
use OCA\DavPush\Service\WebPushSubscriptionService;
use OCA\DavPush\Errors\WebPushSubscriptionNotFound;

use OCP\Http\Client\IClientService;
use OCP\Http\Client\IPromise;

use Sabre\Xml\Service;

class WebPushTransport extends Transport {
protected $id = "web-push";

public function __construct(
private WebPushSubscriptionService $webPushSubscriptionService,
private IClientService $httpClientService
) {}

private function parseOptions(array $options): array {
Expand Down Expand Up @@ -79,7 +83,7 @@ public function registerSubscription($subsciptionId, $options) {
];
}

public function notify(string $userId, string $collectionName, int $subscriptionId) {
public function notify(string $userId, string $collectionName, int $subscriptionId): IPromise {
$xmlService = new Service();

$pushResource = $this->webPushSubscriptionService->findBySubscriptionId($subscriptionId)->getPushResource();
Expand All @@ -88,15 +92,15 @@ public function notify(string $userId, string $collectionName, int $subscription
'{DAV:Push}topic' => $collectionName,
]);

$options = [
'http' => [
'method' => 'POST',
'content' => $content,
],
];
$httpClient = $this->httpClientService->newClient();

$context = stream_context_create($options);
$result = file_get_contents($pushResource, false, $context);
return $httpClient->postAsync($pushResource, [
"body" => $content,
"timeout" => 10,
"headers" => [
"Content-Type" => "application/xml",
],
]);
}

public function getSubscriptionIdFromOptions(string $userId, string $collectionName, $options): ?int {
Expand Down
4 changes: 3 additions & 1 deletion lib/Transport/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace OCA\DavPush\Transport;

use OCP\Http\Client\IPromise;

abstract class Transport {
protected $id;

Expand Down Expand Up @@ -69,5 +71,5 @@ abstract public function getSubscriptionIdFromOptions(string $userId, string $co
// Change mutable options of the subscription (if any exist)
abstract public function updateSubscription($subsciptionId, $options);

abstract public function notify(string $userId, string $collectionName, int $subscriptionId);
abstract public function notify(string $userId, string $collectionName, int $subscriptionId): IPromise;
}
Loading