Skip to content

Commit 7e505d9

Browse files
Expose the ability to invoke grpc services from handler services, and viceversa. (#176)
1 parent 011c802 commit 7e505d9

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

src/restate_context.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,9 @@ export interface RestateBaseContext {
180180
// ----------------------------------------------------------------------------
181181

182182
/**
183-
* The context that gives access to all Restate-backed operations, for example
184-
* - sending reliable messages / rpc through Restate
185-
* - access/update state (for keyed services)
186-
* - side effects
187-
* - sleeps and delayed calls
188-
* - awakeables
189-
* - ...
190-
*
191-
* This context is for use in **gRPC service** implementations.
192-
* For the rpc-handler API, use the {@link RpcContext} instead.
183+
* Interface to interact with **gRPC** based services.
193184
*/
194-
export interface RestateGrpcContext extends RestateBaseContext {
185+
export interface RestateGrpcChannel {
195186
/**
196187
* Unidirectional call to other Restate services ( = in background / async / not waiting on response).
197188
* To do this, wrap the call via the proto-ts client with oneWayCall, as shown in the example.
@@ -256,6 +247,27 @@ export interface RestateGrpcContext extends RestateBaseContext {
256247
): Promise<Uint8Array>;
257248
}
258249

250+
/**
251+
* The context that gives access to all Restate-backed operations, for example
252+
* - sending reliable messages / rpc through Restate
253+
* - access/update state (for keyed services)
254+
* - side effects
255+
* - sleeps and delayed calls
256+
* - awakeables
257+
* - ...
258+
*
259+
* This context is for use in **gRPC service** implementations.
260+
* For the rpc-handler API, use the {@link RpcContext} instead.
261+
*/
262+
export interface RestateGrpcContext
263+
extends RestateBaseContext,
264+
RestateGrpcChannel {
265+
/**
266+
* Get the {@link RpcGateway} to invoke Handler-API based services.
267+
*/
268+
rpcGateway(): RpcGateway;
269+
}
270+
259271
/**
260272
* For compatibility, we make the support 'RestateContext' as the type for the Grpc-based API context.
261273
*/
@@ -323,18 +335,9 @@ export type ServiceApi<_M = unknown> = {
323335
};
324336

325337
/**
326-
* The context that gives access to all Restate-backed operations, for example
327-
* - sending reliable messages / RPC through Restate
328-
* - access/update state (for keyed services)
329-
* - side effects
330-
* - sleeps and delayed calls
331-
* - awakeables
332-
* - ...
333-
*
334-
* This context is for use with the **rpc-handler API**.
335-
* For gRPC-based API, use the {@link RestateContext} instead.
338+
* Interface to interact with **rpc-handler API** based services.
336339
*/
337-
export interface RpcContext extends RestateBaseContext {
340+
export interface RpcGateway {
338341
/**
339342
* Makes a type-safe request/response RPC to the specified target service.
340343
*
@@ -463,3 +466,22 @@ export interface RpcContext extends RestateBaseContext {
463466
*/
464467
sendDelayed<M>(opts: ServiceApi<M>, delay: number): SendClient<M>;
465468
}
469+
470+
/**
471+
* The context that gives access to all Restate-backed operations, for example
472+
* - sending reliable messages / RPC through Restate
473+
* - access/update state (for keyed services)
474+
* - side effects
475+
* - sleeps and delayed calls
476+
* - awakeables
477+
* - ...
478+
*
479+
* This context is for use with the **rpc-handler API**.
480+
* For gRPC-based API, use the {@link RestateContext} instead.
481+
*/
482+
export interface RpcContext extends RestateBaseContext, RpcGateway {
483+
/**
484+
* Get the {@link RestateGrpcChannel} to invoke gRPC based services.
485+
*/
486+
grpcChannel(): RestateGrpcChannel;
487+
}

src/restate_context_impl.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
1010
*/
1111

12-
import { RestateGrpcContext, RpcContext, ServiceApi } from "./restate_context";
12+
import {
13+
RestateGrpcChannel,
14+
RestateGrpcContext,
15+
RpcContext,
16+
RpcGateway,
17+
ServiceApi,
18+
} from "./restate_context";
1319
import { StateMachine } from "./state_machine";
1420
import {
1521
AwakeableEntryMessage,
@@ -397,6 +403,10 @@ export class RestateGrpcContextImpl implements RestateGrpcContext {
397403
);
398404
}
399405
}
406+
407+
rpcGateway(): RpcGateway {
408+
return new RpcContextImpl(this);
409+
}
400410
}
401411

402412
async function executeWithRetries<T>(
@@ -555,4 +565,8 @@ export class RpcContextImpl implements RpcContext {
555565
public sleep(millis: number): Promise<void> {
556566
return this.ctx.sleep(millis);
557567
}
568+
569+
grpcChannel(): RestateGrpcChannel {
570+
return this.ctx;
571+
}
558572
}

0 commit comments

Comments
 (0)