marcoferrer / kroto-plus

gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Apache License 2.0
493 stars 28 forks source link

Add common interface to outter 'Grpc' object for extension support. #22

Closed marcoferrer closed 5 years ago

marcoferrer commented 5 years ago

Consider providing a common interface for the outter 'Grpc' object which encloses the generated stub and service base impl

Reasoning behind this is to allow users to write extensions for building stubs with common configurations specific to their application.

A couple have cases have come up where this would be useful but pending feedback will really drive the inclusion of this feature.

fun <W : GrpcWrapper<Stub>, Stub> W.newStubWithMyDefaultsAndParent(channel: Channel, parent: Job): Stub =
    newStub(channel)
        .withCoroutineContext(parent + Dispatchers.Default)
        .withWaitForReady()
        .withDeadline(...)
            .withInterceptors(...)

// Usage: GreeterCoroutineGrpc.newStubWithMyDefaultsAndParent(channel, job)

fun <W : GrpcWrapper<Stub>, Stub> Channel.newStub(wrapper: W): Stub =
    wrapper.newStub(channel)

// Usage: channel.newStub(GreeterCoroutineGrpc)
marcoferrer commented 5 years ago

A variation of this feature was implemented in v0.3.0. Key difference being the interface was implemented on the stubs companion instead of the outer object. It was needed for adding better support of structured concurrency in coroutines scopes, specifically the new stub builder option CoroutineScope.newGrpcStub