question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent grpc service method to be called by multiple clients at the same time

See original GitHub issue

I have a @GRpcService annotated class which overrides my service rpc methods. Is there a configuration that I can made in order to prevent concurrent access on that method?

My code look something like this:

@GRpcService
public class MyClass extends MyServiceImplBase {

    @Override
    @Transactional
    public void myServiceMethod(request, responseObserver) {
        // run a select query that retrieves a row from db
        // set retrieved to true in database for the retrieved row
        // save updated row to database
    }
}

The problem is that If I have 2 grpc clients calling this method at the same time the method will return the same row.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jvmletcommented, Sep 22, 2022

You can use traditional Java synchronization mechanisms to protect critical sections, including DB transactions, nothing grpc-special. Also I recommend to read this section carefully https://github.com/LogNet/grpc-spring-boot-starter#grpc-response-observer-and-spring-transactional-caveats

0reactions
TaridaGeorgecommented, Sep 23, 2022

What is strange is that before using the lock mechanism If I was calling the GRpcService method concurrently 5 times, 5 out of 5 times I would get the same row returned. After I’ve added the lock mechanism and moved the update row method into it’s own service I’m getting the same row 2 out of 5 times.

My code in order to test this looks something like this:

    @Scheduled(fixedDelay = 1000)
    public void runTest() throws InterruptedException {
        log.info("Running tests");
        ExecutorService service = Executors.newFixedThreadPool(5);
        IntStream.range(0, 5)
                .forEach(count ->
                        service.submit(grpcClient::callMyServiceMethod));
        service.awaitTermination(100, TimeUnit.MILLISECONDS);
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple clients connected to a single server gRPC-java
The gRPC server is multithreaded and will simultaneously accept connections from multiple clients. You can quickly try this out with the hello ...
Read more >
Performance best practices with gRPC | Microsoft Learn
Highly concurrent apps generally perform better with server GC. If a gRPC client app is sending and receiving a high number of gRPC...
Read more >
When having multiple clients with their own stream request ...
I have a code where there are multiple threads , executing the same rpc method ( server streaming) , within which a new...
Read more >
Performance Best Practices - gRPC
A user guide of both general and language-specific best practices to improve performance.
Read more >
Consuming a gRPC Service - Quarkus
The service name, method and type can be found in the tags. Disabling metrics collection. To disable the gRPC client metrics when quarkus-micrometer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found