Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use a specific execution program in the server to block operations

Shulou Source: shulou.com Published: 2022-06-01 04:51:05 09月12日 Update

This article will explain in detail how to use a specific execution program to prevent operations on the server. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use a specific execution program to block the operation

The entire server becomes unresponsive because of a slow operation, which is usually not what developers want. Unfortunately, for RPC, the response time is usually unpredictable.

Suppose the server has 100 worker threads and an endpoint called 100 RPS. Internally, it makes a RPC call, which usually takes 10 milliseconds. At some point, the response time of this RPC becomes 2 seconds, and the only thing the server can do during peak periods is to wait for these calls while other endpoints cannot access them.

@ GET@Path ("/ genre/ {name}") @ Produces (MediaType.APPLICATION_JSON) public Response getGenre (@ PathParam ("name") String genreName) {Genre genre = potentiallyVerySlowSynchronousCall (genreName); return Response.ok (genre). Build ();}

The easiest way to solve this problem is to submit the code, which turns the blocking call into a thread pool:

@ GET@Path ("/ genre/ {name}") @ Produces (MediaType.APPLICATION_JSON) public void getGenre (@ PathParam ("name") String genreName, @ Suspended AsyncResponse response) {response.setTimeout (1L, TimeUnit.SECONDS); executorService.submit (()-> {Genre genre = potentiallyVerySlowSynchronousCall (genreName); return response.resume (Response.ok (genre). Build ());}) } this is the end of the article on "how to use specific execution programs in the server to prevent operations". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Tags: Servers services programs time articles more endpoints threads good practical slow unfortunate people code content that is peaks articles methods knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info NVidia OPPO Reno macOS