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 method stream type to generated code #172

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This did not make sense to live on the MethodSpec - it should have lived on the HTTPRequest. Now the MethodSpec type only contains fields assigned by the code generator.

val streamType: StreamType = StreamType.UNKNOWN,
Copy link
Member

Choose a reason for hiding this comment

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

This unknown value seems a little scary. How will it impact things? Is it necessary for backwards-compatibility with older generated code? What could go wrong if older code is paired with this newer runtime -- would it mean that the GET HTTP verb is never used and interceptors would see the wrong value for stream type? I think it might be safer to make this a non-optional parameter with no default and remove the UNKNOWN sentinel enum entry. Users must re-generate their code when they update to newer version of the library.

Copy link
Contributor Author

@pkwarren pkwarren Dec 8, 2023

Choose a reason for hiding this comment

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

I went back and forth on this. Having UNKNOWN also matches connect-go and grpc-java. connect-swift doesn't have an unknown type.

It was less about the compatibility with existing generated code as this is still pre-v1 and it should be expected that consumers keep the runtime libraries in sync with the generated code. I'll make the change to make it required.

Copy link
Member

Choose a reason for hiding this comment

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

I know connect-go has this because there's no real support for enums in Go, and it's just good practice for the default/zero value to be a sentinel of this sort. But in kotlin and java, it's much less clear why you'd include one. With grpc-java, it's likely because that supports server-side, too, and if you want to create an "unknown method handler", it's most correct for it to report to handler/interceptor code that the stream type is 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))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There were a few places where we were missing specifying the ConnectException's underlying cause (if set).

}
}
}
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
pkwarren marked this conversation as resolved.
Show resolved Hide resolved
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