MinnDevelopment/discord-webhooks

Support for sending messages with timeouts

celandro opened this issue · 1 comments

The following code is being used to send with a 30 second timeout.

    void sendMessagesWithTimeout(Notification change, Optional<DiscordGuild> guild, List<DiscordGuild> helperGuilds) {
        //message created by some method
        WebhookMessage message = createRemoteHelpMessage(change);
        log.info("Sending {} discord raid request for {} to {} servers", language, change.getId(), helperGuilds.size());
        for (DiscordGuild helperGuild: helperGuilds) {
            try {
                getClient(helperGuild.getId()).send(message, helperGuild).orTimeout(30000, TimeUnit.MILLISECONDS).whenCompleteAsync((result,e) -> {
                    if (e != null) {
                        if (e instanceof TimeoutException) {
                            log.warn("Time out after {}ms sending {} to {}", globalNotificationTimeoutMs ,change.getRaidPartyId(), helperGuild);
                        }
                    }
                });
            } catch (ExecutionException | RuntimeException e) {
                log.error("Not retrying " + change.getId() + ": Could not send discord raid request for " + helperGuild, e);
            }
        }
    }

Unfortunately, this does not currently cancel the request

The following change is required to support timeouts

    private boolean executePair(@Async.Execute Request req) {
        if (req.future.isCancelled()) {

should be

    private boolean executePair(@Async.Execute Request req) {
        if (req.future.isCompletedExceptionally()) {

as TimeoutException is not a CancellationException. Per javadocs, isCompletedExceptionally is a superset of isCancelled.

This should work as of 0.5.1. I've also introduced a new setter to configure a default timeout with setTimeout.