What does the BCE20402 warning mean?

Viewed 158

I have a use case where I, in a http service endpoint, need to dispatch several tasks and then combine the responses. In the sample code below, I get the warning (provided as a comment) on the service resource.

service / on new http:Listener(9090) {

// Warning: concurrent calls will not be made to this method since the method is not an 'isolated' method(BCE20402)
    resource function get names() returns string {
      worker w returns string {
        return ["John", "Ada", "Harry"].toString();
      }
      return wait w;
    }

}

With simple code like this, I have managed to avoid the warning by using start hidden inside a foreach lambda, but then it turns up again when I try to include a http client call to another resource in the concurrent tasks.

So what does the warning mean in practice? How can I do what I need to do?

1 Answers

In the context of http:Listener, this warning means that network calls to this resource method will be serialized by the http:Listener.

Ballerina incorporates the concept of isolated to guarantee concurrency safety, since you have used a worker in your resource method, the compiler cannot infer this resource function as an isolated function. And since this is not an isolated function, the http:Listener will forcefully serialize the incoming requests to this resource to make sure that there are no parallel executions of this method.

When you see this warning, making the resource/function an isolated function by pretending the isolated keyword will prompt you with a compiler error saying why this function is not isolated.

isolated resource function get names() {
Related