-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't block the event loop when reading inputstream. #6120
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d69008
Don't block the event loop when reading inputstream. Fixes #6100
graemerocher 6a0be18
Use only publishOn
graemerocher 7144ec2
add issue link
graemerocher 120aa53
synchronize access to input stream
graemerocher addc552
Lazily init processor
graemerocher dcf80f3
Skip offloading to IO
jameskleeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...er-netty/src/test/groovy/io/micronaut/http/server/netty/stream/InputStreamBodySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package io.micronaut.http.server.netty.stream | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.* | ||
import io.micronaut.http.annotation.* | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.http.client.multipart.MultipartBody | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import io.micronaut.scheduling.TaskExecutors | ||
import io.micronaut.scheduling.annotation.ExecuteOn | ||
import jakarta.inject.Inject | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Issue | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import javax.annotation.Nullable | ||
import java.util.concurrent.ConcurrentLinkedQueue | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
class InputStreamBodySpec extends Specification { | ||
|
||
@Shared @AutoCleanup EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer) | ||
|
||
@Issue('https://github.com/micronaut-projects/micronaut-core/issues/6100') | ||
void "test apply load to InputStream read"() { | ||
given: | ||
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURI()) | ||
|
||
when: | ||
int max = 30 | ||
CountDownLatch latch = new CountDownLatch(max) | ||
|
||
ExecutorService pool = Executors.newCachedThreadPool() | ||
ConcurrentLinkedQueue<HttpStatus> responses = new ConcurrentLinkedQueue() | ||
for (int i = 0; i < max; i++) { | ||
pool.submit(() -> { | ||
try { | ||
MultipartBody multipartBody = MultipartBody.builder() | ||
.addPart("myfile", | ||
"largefile" * 1024) | ||
.build() | ||
HttpRequest request = HttpRequest.POST("/input-stream-test/hello", multipartBody) | ||
.contentType(MediaType.MULTIPART_FORM_DATA_TYPE) | ||
HttpResponse response = client.toBlocking() | ||
.exchange(request) | ||
responses.add(response.status()) | ||
System.out.println(response.getStatus()) | ||
System.out.println(response.getHeaders().asMap()) | ||
|
||
} catch (HttpClientResponseException e) { | ||
System.out.println(e.getStatus()) | ||
} catch (URISyntaxException e) { | ||
e.printStackTrace() | ||
} finally { | ||
latch.countDown() | ||
} | ||
|
||
}) | ||
} | ||
latch.await() | ||
|
||
then: | ||
responses.size() == 30 | ||
responses.every({ it == HttpStatus.OK}) | ||
} | ||
} | ||
|
||
@Controller("/input-stream-test") | ||
class PayloadInputStream { | ||
|
||
@Inject | ||
@Client("/") | ||
private HttpClient httpClient | ||
|
||
private String responsePayload = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + | ||
" <soap:Body>\n" + | ||
" <ns2:getHelloWorldAsStringResponse xmlns:ns2=\"http://sample.soap.oracle/\">\n" + | ||
" <return>Hello World %s</return>\n" + | ||
" </ns2:getHelloWorldAsStringResponse>\n" + | ||
" </soap:Body>\n" + | ||
"</soap:Envelope>" | ||
|
||
|
||
@Post("/hello") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.ALL) | ||
@ExecuteOn(TaskExecutors.IO) | ||
MutableHttpResponse<String> stream(@Body @Nullable InputStream payload) throws IOException { | ||
|
||
//blocking read on injected http client | ||
HttpResponse<String> resp = httpClient.toBlocking().exchange(HttpRequest | ||
.GET(URI.create("/input-stream-test/hello/other")), String.class) | ||
System.out.println(resp.getBody(String.class).get()) | ||
|
||
|
||
byte[] body = payload.bytes | ||
String b = new String(body) | ||
int l = body.length | ||
|
||
responsePayload = "{\n" + | ||
"\t\"payload\" : {\n" + | ||
"\t\t\"name\" : \"1542\"\n" + | ||
"\t}\n" + | ||
"}" | ||
return HttpResponse.ok().body(responsePayload).contentType(MediaType.APPLICATION_JSON) | ||
} | ||
|
||
@Get("/hello/other") | ||
String other() { | ||
return "Some body content" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this lead to OOM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InputStream is not thread safe, so I don't see an alternative
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I don't think this is an improvement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide a reproducer for your OOM theory in that case. "I don't think" is not a valid argument opposing a bug fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try to create the scenario. In the meantime, relax with your tone