Comments (2)
It is indeed possible to launch a multithreaded RPC server. Here is a demo launcher that does just that.
However, looking at your project, I can see that the issue is that your code is blocking the eventloop thread.
The code in RpcRequestHandler
is executed inside the eventloop thread. So any blocking operations will make eventloop unavailable until that operation is completed. This is why it appears that requests are processed sequentially. You should avoid blocking code, such as I/O or Thread.sleep(), inside the eventloop.
For example, letโs rewrite the RpcErasureServer#downloadErasureChunks
method. It has blocking code inside the while
loop (Thread.sleep()
). The same logic could be achieved by using Promises. For example, the Promises#loop
method, which can be used to write asynchronous loops. Your code would look like this:
private RpcRequestHandler<ErasureRequest, ErasureResponse> downloadErasureChunks(IChunksService<T> service) {
return request -> Promises.loop(0,
rc -> rc < ConsensusConfiguration.CYCLES &&
(serializable_length == 0 ||
CachedSerializableErasureObject.getInstance().getSerializableErasureObject() == null),
rc -> Promises.delay(ConsensusConfiguration.HEARTBEAT_INTERVAL, rc + 1)
)
.map(rc -> {
if (rc == ConsensusConfiguration.CYCLES)
return new ErasureResponse(null);
T result = service.downloadErasureChunks();
if (result == null)
return new ErasureResponse(null);
return new ErasureResponse(this.valueMapper.encode(result, serializable_length));
});
}
Look at the Promises#loop
call in place of the while
cycle. Promises#loop
takes an initial seed value 0 (rc=0
). It also takes a loop break condition as its second argument. Finally, we pass a function that converts the current rc
value to the next rc
value (rc + 1
), but it also returns the next value after a delay of HEARTBEAT_INTERVAL milliseconds. A delay does not cause a thread to block (like with Thread.sleep()
), but rather schedules a task to the eventloop. So, other requests/tasks can be executed while the eventloop is waiting for the delay to pass.
Notice that we call service.downloadErasureChunks()
inside the eventloop. I looked at this method and did not find blocking operations, so itโs fine to call it from the eventloop thread. However, if downloadErasure()
would access DB, file system, or perform any other I/O operations, you would need to execute it using an external Executor. I noticed there is some blocking code in RPC handlers located in the RpcAdrestusServer
class. You might consider rewriting them, and wrapping blocking code using Promise#ofBlocking
methods.
You can look at this example for reference. Here we wrap blocking calls to JDBC API using Promise#ofBlocking
with an external Executor.
from activej.
@eduard-vasinskyi I have no words to say excellent as always you gave solutions to the point. Thank you very much. I appreciate it. God bless you
from activej.
Related Issues (20)
- Happen error while uploading file HOT 2
- Release plan for v5.5 and v6 HOT 3
- Add Query Parameters? HOT 6
- RPCClient lock port on local HOT 1
- Version 6 does not support ipv6, version 5.5 supported ipv6 HOT 2
- Make classes Public to facilitate mocking. HOT 1
- RpcClient receive unexpected EOS HOT 2
- RpcClient cannot setup connection
- RpcServer serializer is wrong HOT 4
- Happen error with Serializer in native app HOT 2
- sorry a local problem
- How to hook the end of the HttpResponse so I can clean up some buffers? HOT 4
- Unknown characters when read messages from the websocket HOT 1
- HttpRequest with custom Host header HOT 1
- HttpServer under PrimaryServer can't handle SSL requests HOT 2
- Is it possible to use @Eager with @Inject on constructor? HOT 1
- Current DNS Client is Incompatible with Kubernetes Deployment HOT 5
- Support self define ThreadFactory for eventloop threads HOT 1
- Getting post parameters does not work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from activej.