Skip to content
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 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Member

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:

public final class CoroutineContextService extends SimpleDecoratingHttpService {

You may want to refer to this code snippet to see how it's implemented:
val userContext = CoroutineContexts.get(ctx) ?: EmptyCoroutineContext

suspendedServe(ctx, req)
})
}

suspend fun suspendedServe(ctx: ServiceRequestContext, req: HttpRequest): HttpResponse
minwoox marked this conversation as resolved.
Show resolved Hide resolved
}
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check if the context is propagated correctly.
You might want to refer to this implementation:

}
}
)
}
}
}

@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")
}
}
Loading