ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
138 stars 56 forks source link

Improve subscriber handling content distribution response #3022

Open shafreenAnfar opened 2 years ago

shafreenAnfar commented 2 years ago

The spec says

Subscribers SHOULD respond to content distribution requests as quickly as possible; their success response code SHOULD only indicate receipt of the message, not acknowledgment that it was successfully processed by the subscriber.

For more information see https://www.w3.org/TR/websub/#content-distribution

I wonder if we should change the default behavior to automatically handle it internally and send 2xx response when the content is received. This can be disabled if the user wants via a configuration.

shafreenAnfar commented 2 years ago

@ayeshLK @chamil321 any thoughts ?

shafreenAnfar commented 2 years ago

I think as per the current behavior, the ideal code should be something like the below,

import ballerina/websub;
import ballerina/io;

@websub:SubscriberServiceConfig {
    target: [
        "https://api.github.com/hub", 
        "https://github.com/<YOUR_ORGANIZATION>/<REPOSITORY>/events/push.json"
    ],
    secret: "<YOUR_SECRET_KEY>",
    httpConfig: {
        auth: {
            token: "<YOUR_AUTH_TOKEN>"
        }
    }
}
service /JuApTOXq19 on new websub:Listener(9090) {
    remote function onEventNotification(
                    websub:ContentDistributionMessage event) {
        worker A returns error? {
            var retrievedContent = event.content;
            if retrievedContent is json {
                // body
            } else {
                io:println("Unrecognized content type, hence ignoring");
            }
        }
    }
}
ayeshLK commented 2 years ago

I wonder if we should change the default behavior to automatically handle it internally and send 2xx response when the content is received. This can be disabled if the user wants via a configuration.

This seems a good improvement to websub:SubscriberService.

chamil321 commented 2 years ago

+1