Skip to content

Commit 13a2c27

Browse files
committed
APISIX-38 Document the threading model and async plugins
1 parent 4bf59e7 commit 13a2c27

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

docs/en/latest/the-internal-of-apisix-java-plugin-runner.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,39 @@ The current type takes the following values
7878
* 2 means http_req_call
7979

8080
The binary data generated by the flatbuffer serialization is placed in the data segment.
81+
82+
## Threading model
83+
84+
Apisix plugin runner will run your plugins directly onto the event loop.
85+
86+
While this empower the best performance possible, as a plugin developer you will have the responsibility
87+
never to block threads on the event loop. Doing so would result in catastrophic performance drop.
88+
89+
Hopefully one can write asynchronous plugins easily: just call the `PluginFilterChain` as a callback once you
90+
are done.
91+
92+
For instance:
93+
94+
```java
95+
@Component
96+
public class AsyncResponseFilter implements PluginFilter {
97+
@Override
98+
public String name() {
99+
return "AyncResponseFilter";
100+
}
101+
102+
@Override
103+
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
104+
callExternalService()
105+
.thenAccept(body -> {
106+
response.setBody(body);
107+
chain.postFilter(request, response);
108+
});
109+
}
110+
111+
// This simulates calls to an external service
112+
CompletableFuture<String> callExternalService() {
113+
return CompletableFuture.completedFuture("response_body");
114+
}
115+
}
116+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.apisix.plugin.runner.filter;
19+
20+
import java.util.concurrent.CompletableFuture;
21+
22+
import org.apache.apisix.plugin.runner.PostRequest;
23+
import org.apache.apisix.plugin.runner.PostResponse;
24+
import org.springframework.stereotype.Component;
25+
26+
@Component
27+
public class AsyncResponseFilter implements PluginFilter {
28+
@Override
29+
public String name() {
30+
return "AsyncResponseFilter";
31+
}
32+
33+
@Override
34+
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
35+
callExternalService()
36+
.thenAccept(body -> {
37+
response.setBody(body);
38+
chain.postFilter(request, response);
39+
});
40+
}
41+
42+
// This simulates calls to an external service
43+
CompletableFuture<String> callExternalService() {
44+
return CompletableFuture.completedFuture("response_body");
45+
}
46+
}

0 commit comments

Comments
 (0)