Skip to content

Commit

Permalink
Add method stream type to generated code
Browse files Browse the repository at this point in the history
It is useful to have the stream type in the generated code (similar to
other Connect implementations). In the future we may use this
information to optimize stream handling - for example, avoiding the
pipe/duplex/oneshot for server streaming calls to enable Connect over
HTTP/1.1. Update the generator to include the stream type in the
generated code.

Additionally, move the HTTP method to the HTTPRequest class - mutating a
method spec to determine which HTTP method to use seems wrong. We should
keep MethodSpec to contain only fields set from the generator.
  • Loading branch information
pkwarren committed Dec 7, 2023
1 parent de27a21 commit 43230dd
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 40 deletions.
9 changes: 2 additions & 7 deletions library/src/main/kotlin/com/connectrpc/MethodSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ package com.connectrpc

import kotlin.reflect.KClass

internal object Method {
internal const val GET_METHOD = "GET"
internal const val POST_METHOD = "POST"
}

/**
* Represents the minimum set of information to execute an RPC method.
* Primarily used in generated code.
Expand All @@ -29,12 +24,12 @@ internal object Method {
* @param requestClass The Kotlin Class for the request message.
* @param responseClass The Kotlin Class for the response message.
* @param idempotency The declared idempotency of a method.
* @param method The HTTP method of a request.
* @param streamType The method's stream type.
*/
class MethodSpec<Input : Any, Output : Any>(
val path: String,
val requestClass: KClass<Input>,
val responseClass: KClass<Output>,
val idempotency: Idempotency = Idempotency.UNKNOWN,
val method: String = Method.POST_METHOD,
val streamType: StreamType = StreamType.UNKNOWN,
)
35 changes: 35 additions & 0 deletions library/src/main/kotlin/com/connectrpc/StreamType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022-2023 The Connect Authors
//
// Licensed 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
//
// http://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.connectrpc

/**
* Represents the RPC stream type. Set by the code generator on each [MethodSpec].
*/
enum class StreamType {
/** Unknown stream type. */
UNKNOWN,

/** Unary RPC. */
UNARY,

/** Client streaming RPC. */
CLIENT,

/** Server streaming RPC. */
SERVER,

/** Bidirectional streaming RPC. */
BIDI,
}
11 changes: 11 additions & 0 deletions library/src/main/kotlin/com/connectrpc/http/HTTPRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import com.connectrpc.Headers
import com.connectrpc.MethodSpec
import java.net.URL

internal object HTTPMethod {
internal const val GET = "GET"
internal const val POST = "POST"
}

/**
* HTTP request used for sending primitive data to the server.
*/
Expand All @@ -32,6 +37,9 @@ class HTTPRequest internal constructor(
val message: ByteArray? = null,
// The method spec associated with the request.
val methodSpec: MethodSpec<*, *>,
// HTTP method to use with the request.
// Almost always POST, but side effect free unary RPCs may be made with GET.
val httpMethod: String = HTTPMethod.POST,
) {
/**
* Clones the [HTTPRequest] with override values.
Expand All @@ -50,13 +58,16 @@ class HTTPRequest internal constructor(
message: ByteArray? = this.message,
// The method spec associated with the request.
methodSpec: MethodSpec<*, *> = this.methodSpec,
// The HTTP method to use with the request.
httpMethod: String = this.httpMethod,
): HTTPRequest {
return HTTPRequest(
url,
contentType,
headers,
message,
methodSpec,
httpMethod,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class ProtocolClient(
isComplete = true
when (streamResult.code) {
Code.OK -> channel.close()
else -> channel.close(streamResult.connectException() ?: ConnectException(code = streamResult.code))
else -> channel.close(streamResult.connectException() ?: ConnectException(code = streamResult.code, exception = streamResult.cause))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import com.connectrpc.ConnectException
import com.connectrpc.Headers
import com.connectrpc.Idempotency
import com.connectrpc.Interceptor
import com.connectrpc.Method.GET_METHOD
import com.connectrpc.MethodSpec
import com.connectrpc.ProtocolClientConfig
import com.connectrpc.RequestCompression
import com.connectrpc.StreamFunction
import com.connectrpc.StreamResult
import com.connectrpc.StreamType
import com.connectrpc.Trailers
import com.connectrpc.UnaryFunction
import com.connectrpc.compression.CompressionPool
import com.connectrpc.http.HTTPMethod
import com.connectrpc.http.HTTPRequest
import com.connectrpc.http.HTTPResponse
import com.squareup.moshi.Moshi
Expand Down Expand Up @@ -189,7 +189,8 @@ internal class ConnectInterceptor(
}

private fun shouldUseGETRequest(request: HTTPRequest, finalRequestBody: Buffer): Boolean {
return request.methodSpec.idempotency == Idempotency.NO_SIDE_EFFECTS &&
return request.methodSpec.streamType == StreamType.UNARY &&
request.methodSpec.idempotency == Idempotency.NO_SIDE_EFFECTS &&
clientConfig.getConfiguration.useGET(finalRequestBody)
}

Expand All @@ -210,13 +211,8 @@ internal class ConnectInterceptor(
url = url,
contentType = "application/${requestCodec.encodingName()}",
headers = request.headers,
methodSpec = MethodSpec(
path = request.methodSpec.path,
requestClass = request.methodSpec.requestClass,
responseClass = request.methodSpec.responseClass,
idempotency = request.methodSpec.idempotency,
method = GET_METHOD,
),
methodSpec = request.methodSpec,
httpMethod = HTTPMethod.GET,
)
}

Expand Down Expand Up @@ -261,7 +257,7 @@ internal class ConnectInterceptor(
errorJSON,
)
} catch (e: Exception) {
return ConnectException(code, serializationStrategy.errorDetailParser(), errorJSON)
return ConnectException(code, serializationStrategy.errorDetailParser(), errorJSON, e)
}
val errorDetails = parseErrorDetails(errorPayloadJSON)
ConnectException(
Expand Down
18 changes: 13 additions & 5 deletions library/src/test/kotlin/com/connectrpc/InterceptorChainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ import org.junit.Test
import org.mockito.kotlin.mock
import java.net.URL

private val METHOD_SPEC = MethodSpec(
private val UNARY_METHOD_SPEC = MethodSpec(
path = "",
requestClass = Any::class,
responseClass = Any::class,
streamType = StreamType.UNARY,
)

private val STREAM_METHOD_SPEC = MethodSpec(
path = "",
requestClass = Any::class,
responseClass = Any::class,
streamType = StreamType.BIDI,
)

class InterceptorChainTest {
Expand Down Expand Up @@ -64,7 +72,7 @@ class InterceptorChainTest {

@Test
fun fifo_request_unary() {
val response = unaryChain.requestFunction(HTTPRequest(URL("https://connectrpc.com"), "", emptyMap(), null, METHOD_SPEC))
val response = unaryChain.requestFunction(HTTPRequest(URL("https://connectrpc.com"), "", emptyMap(), null, UNARY_METHOD_SPEC))
assertThat(response.headers.get("id")).containsExactly("1", "2", "3", "4")
}

Expand All @@ -76,7 +84,7 @@ class InterceptorChainTest {

@Test
fun fifo_request_stream() {
val request = streamingChain.requestFunction(HTTPRequest(URL("https://connectrpc.com"), "", emptyMap(), null, METHOD_SPEC))
val request = streamingChain.requestFunction(HTTPRequest(URL("https://connectrpc.com"), "", emptyMap(), null, STREAM_METHOD_SPEC))
assertThat(request.headers.get("id")).containsExactly("1", "2", "3", "4")
}

Expand Down Expand Up @@ -112,7 +120,7 @@ class InterceptorChainTest {
it.contentType,
headers,
it.message,
METHOD_SPEC,
UNARY_METHOD_SPEC,
)
},
responseFunction = {
Expand Down Expand Up @@ -144,7 +152,7 @@ class InterceptorChainTest {
it.contentType,
headers,
it.message,
METHOD_SPEC,
STREAM_METHOD_SPEC,
)
},
requestBodyFunction = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.connectrpc.Codec
import com.connectrpc.MethodSpec
import com.connectrpc.ProtocolClientConfig
import com.connectrpc.SerializationStrategy
import com.connectrpc.StreamType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -54,6 +55,7 @@ class BiDirectionalStreamTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
)
stream.sendClose()
Expand Down Expand Up @@ -83,6 +85,7 @@ class BiDirectionalStreamTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
)
val result = stream.send("input")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.connectrpc.Codec
import com.connectrpc.MethodSpec
import com.connectrpc.ProtocolClientConfig
import com.connectrpc.SerializationStrategy
import com.connectrpc.StreamType
import com.connectrpc.http.HTTPClientInterface
import com.connectrpc.http.HTTPRequest
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -57,6 +58,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
) { _ -> }
}
Expand All @@ -81,6 +83,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
) { _ -> }
}
Expand All @@ -105,6 +108,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
)
}
Expand All @@ -130,6 +134,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.BIDI,
),
)
}
Expand All @@ -154,6 +159,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
) {}

Expand Down Expand Up @@ -182,6 +188,7 @@ class ProtocolClientTest {
path = "com.connectrpc.SomeService/Service",
String::class,
String::class,
streamType = StreamType.UNARY,
),
) {}

Expand Down
Loading

0 comments on commit 43230dd

Please sign in to comment.