-
Notifications
You must be signed in to change notification settings - Fork 902
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
Add CoroutineHttpService for handle request within Coroutine suspend #5603
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c76b219
Add CoroutineHttpService for handle request within Coroutine suspend
pinest94 79d2f0f
Fix lint
pinest94 b41a22f
Apply ktlint format
pinest94 c34d203
Support CoroutineContextService newDecorate in CoroutineHttpService
pinest94 326b773
Remove comment
pinest94 e6682de
Revise KDoc
minwoox 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
42 changes: 42 additions & 0 deletions
42
kotlin/src/main/kotlin/com/linecorp/armeria/server/kotlin/CoroutineHttpService.kt
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,42 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server.kotlin | ||
|
||
import com.linecorp.armeria.common.HttpRequest | ||
import com.linecorp.armeria.common.HttpResponse | ||
import com.linecorp.armeria.common.kotlin.asCoroutineContext | ||
import com.linecorp.armeria.server.HttpService | ||
import com.linecorp.armeria.server.ServiceRequestContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.future.future | ||
|
||
/** | ||
* An Coroutine-based [HttpService] | ||
*/ | ||
fun interface CoroutineHttpService : HttpService { | ||
|
||
/** | ||
* invoke the suspendedServe method within the CoroutineScope | ||
*/ | ||
override fun serve(ctx: ServiceRequestContext, req: HttpRequest): HttpResponse { | ||
return HttpResponse.of(CoroutineScope(ctx.asCoroutineContext()).future { // Do we also need to add user context? | ||
suspendedServe(ctx, req) | ||
}) | ||
} | ||
|
||
suspend fun suspendedServe(ctx: ServiceRequestContext, req: HttpRequest): HttpResponse | ||
minwoox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
57 changes: 57 additions & 0 deletions
57
kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/CoroutineHttpServiceTest.kt
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,57 @@ | ||||
/* | ||||
* Copyright 2024 LINE Corporation | ||||
* | ||||
* LINE Corporation licenses this file to you under the Apache License, | ||||
* version 2.0 (the "License"); you may not use this file except in compliance | ||||
* with the License. You may obtain a copy of the License at: | ||||
* | ||||
* https://www.apache.org/licenses/LICENSE-2.0 | ||||
* | ||||
* Unless required by applicable law or agreed to in writing, software | ||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||
* License for the specific language governing permissions and limitations | ||||
* under the License. | ||||
*/ | ||||
|
||||
package com.linecorp.armeria.server.kotlin | ||||
|
||||
import com.linecorp.armeria.common.HttpRequest | ||||
import com.linecorp.armeria.common.HttpResponse | ||||
import com.linecorp.armeria.common.HttpStatus | ||||
import com.linecorp.armeria.server.ServerBuilder | ||||
import com.linecorp.armeria.server.ServiceRequestContext | ||||
import com.linecorp.armeria.testing.junit5.server.ServerExtension | ||||
import kotlinx.coroutines.test.runTest | ||||
import org.assertj.core.api.Assertions | ||||
import org.junit.jupiter.api.Test | ||||
import org.junit.jupiter.api.extension.RegisterExtension | ||||
|
||||
class CoroutineHttpServiceTest { | ||||
|
||||
companion object { | ||||
@JvmField | ||||
@RegisterExtension | ||||
val server = | ||||
object : ServerExtension() { | ||||
override fun configure(sb: ServerBuilder) { | ||||
sb.service( | ||||
"/hello", | ||||
object : CoroutineHttpService { | ||||
override suspend fun suspendedServe(ctx: ServiceRequestContext, req: HttpRequest): HttpResponse { | ||||
return HttpResponse.of("hello world") | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's check if the context is propagated correctly. Line 380 in df826c0
|
||||
} | ||||
} | ||||
) | ||||
} | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
fun `Should return hello world when call hello coroutine service`() = runTest { | ||||
val response = server.blockingWebClient().get("/hello") | ||||
|
||||
Assertions.assertThat(response.status()).isEqualTo(HttpStatus.OK) | ||||
Assertions.assertThat(response.contentUtf8()).isEqualTo("hello world") | ||||
} | ||||
} |
Oops, something went wrong.
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.
Let's also support the userContext that maybe set by a
CoroutineContextService
:armeria/kotlin/src/main/java/com/linecorp/armeria/server/kotlin/CoroutineContextService.java
Line 54 in d9a50e8
You may want to refer to this code snippet to see how it's implemented:
armeria/kotlin/src/main/kotlin/com/linecorp/armeria/internal/common/kotlin/ArmeriaCoroutineUtil.kt
Line 152 in 655d48c