在实施中出现并发问题

huangapple go评论133阅读模式
英文:

Getting Concurrency Issue in Implementation

问题

抱歉,您提到的代码部分不需要翻译。要解决"concurrent calls will not be made to this method since the service is not an 'isolated' service"警告,您需要将服务标记为“isolated”。

在您的服务定义中,例如,在service /fr on new http:Listener(9090)之前,您可以添加isolated修饰符,如下所示:

isolated service /fr on new http:Listener(9090) {
    // ...
}

这样,您的服务将被标记为“isolated”,并且可以处理并发调用,从而解决了该警告。

英文:

Package Level

PM.bal

public type PM object {

    public isolated function methodA(json config) returns error|http:Response;
    public isolated function methodB(json config) returns error|http:Response;
    public isolated function methodC(json config) returns sql:Client|error;
};

DefaultImplPM.bal

public class DefaultImplPM {
    *PM;
   
    public isolated function methodC(json config) returns sql:Client|error {
        return  new mysql:Client("dbConfig.host", "dbConfig.username", "dbConfig.password", "dbConfig.database");
    }

    public isolated function methodA(json config) returns error|http:Response {
        http:Response response = new;
        response.setJsonPayload("Default methodA is not implemented yet");
        return response;
    }

    public isolated function methodB( json config) returns error|http:Response {
        http:Response response = new;
        response.setJsonPayload("Default methodB is not implemented yet");
        return response;
    }
}

Service level

newImpl.bal

import package as pkg;
public class NewImplPM {
    *pkg:PM;
   
    public isolated function methodC(json config) returns sql:Client|error {
        return  new mysql:Client("dbConfig.host", "dbConfig.username", "dbConfig.password", "dbConfig.database");
    }

    public isolated function methodA(json config) returns error|http:Response {
        http:Response response = new;
        response.setJsonPayload("new methodA is not implemented yet");
        return response;
    }

    public isolated function methodB(json config) returns error|http:Response {
        http:Response response = new;
        response.setJsonPayload("new methodB is not implemented yet");
        return response;
    }
}

in service.bal

import package as pkg;

service /fr on new http:Listener(9090) {

    pkg:PM pmVariable;
    public function init() {
        self.pmVariable = new DefaultImplPM();
    }


    resource isolated function post methodD(string c) returns error|http:Response {
        return self.pmVariable.methodA(c);   
    }

    resource isolated function post methodE(string c) returns error|http:Response {
        return self.pmVariable.methodB(c);
    }
}

I have some abstract methods for a use case which may have multiple different types of implementation for those methods. I want to given an basic implementation in a ballerina package level. That will be my default algorithm. When some one wants to give some additional support with a new algorithm implementation in service level. He/She should be able to do it by including my interface and implement those methods.

With this implementation, I got concurrent calls will not be made to this method since the service is not an 'isolated' service warning. How to resolve this.

答案1

得分: 1

根据警告显示的信息,这是因为服务对象不是“隔离的”(也不符合将“隔离”对象推断为隔离对象的要求)。只有在对象和方法都是“隔离的”时,才会对资源方法进行并发调用。如果对象不是“隔离的”,仍可能存在对可变状态的不安全访问。

请参阅并发安全性,特别是隔离对象

此服务未被推断为“隔离的”,因为它具有非私有的可变字段(该字段也未在锁中访问,不满足“隔离”对象的传递条件等)。

您可以显式标记服务声明以确定并修复导致其不隔离的原因。

隔离服务 /fr 使用新的 http:Listener(9090) {
    ...
}

通常,您需要要么

  1. PM标记为隔离对象并将pmVariable标记为final字段,或者
  2. pmVariable标记为私有字段,并在访问时使用锁,并引入相关更改(以确保该对象是隔离根)。
英文:

As indicated by the warning, this is because the service object is not isolated (nor does it meet the requirements for an isolated object to be inferred as one). Concurrent calls will be made to a resource method only if both the object and the method are isolated. If the object is not isolated, there can still be unsafe access of mutable state.

See concurrency safety, specifically isolated objects.

This service is not inferred as isolated since it has a non-private mutable field (which is also not accessed within locks, does not meet transfer in/out conditions for an isolated object, etc.).

You can explicitly mark the service declaration to identify and fix what's causing it to be non-isolated.

isolated service /fr on new http:Listener(9090) {
    ...
}

Generally, you would either have to

  1. make PM an isolated object and pmVariable a final field or
  2. make pmVariable a private field and use locks when accessing it and introduce related changes (to ensure that the object is an isolated root)

huangapple
  • 本文由 发表于 2023年6月13日 14:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定