Skip to content

Commit

Permalink
add Ping to RpcMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jan 30, 2025
1 parent 5e076b5 commit 58a9436
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/rpc/src/RpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type { Mutable } from "effect/Types"
import type * as Rpc from "./Rpc.js"
import type * as RpcGroup from "./RpcGroup.js"
import type { FromClient, FromClientEncoded, FromServer, FromServerEncoded } from "./RpcMessage.js"
import { RequestId } from "./RpcMessage.js"
import { constPing, RequestId } from "./RpcMessage.js"
import * as RpcSchema from "./RpcSchema.js"
import * as RpcSerialization from "./RpcSerialization.js"

Expand Down Expand Up @@ -529,6 +529,13 @@ export const makeProtocolSocket = Effect.gen(function*() {
Effect.forkScoped
)

yield* Effect.suspend(() => write(parser.encode(constPing))).pipe(
Effect.delay("30 seconds"),
Effect.ignore,
Effect.forever,
Effect.forkScoped
)

return Protocol.of({
responses,
send(request) {
Expand Down Expand Up @@ -582,7 +589,7 @@ const decodeDefectExit = Schema.decodeSync(Schema.Exit({
const transformBigInt = (request: FromClientEncoded) => {
if (request._tag === "Request") {
;(request as any).id = request.id.toString()
} else {
} else if (request._tag !== "Ping") {
;(request as any).requestId = request.requestId.toString()
}
}
32 changes: 30 additions & 2 deletions packages/rpc/src/RpcMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type FromClient<A extends Rpc.Any> = Request<A> | Ack | Interrupt | Eof
* @since 1.0.0
* @category request
*/
export type FromClientEncoded = RequestEncoded | Ack | Interrupt
export type FromClientEncoded = RequestEncoded | Ack | Interrupt | Ping

/**
* @since 1.0.0
Expand Down Expand Up @@ -99,12 +99,26 @@ export interface Eof {
readonly _tag: "Eof"
}

/**
* @since 1.0.0
* @category request
*/
export interface Ping {
readonly _tag: "Ping"
}

/**
* @since 1.0.0
* @category request
*/
export const constEof: Eof = { _tag: "Eof" }

/**
* @since 1.0.0
* @category request
*/
export const constPing: Ping = { _tag: "Ping" }

/**
* @since 1.0.0
* @category response
Expand All @@ -119,7 +133,7 @@ export type FromServer<A extends Rpc.Any> =
* @since 1.0.0
* @category response
*/
export type FromServerEncoded = ResponseChunkEncoded | ResponseExitEncoded | ResponseDefectEncoded
export type FromServerEncoded = ResponseChunkEncoded | ResponseExitEncoded | ResponseDefectEncoded | Pong

/**
* @since 1.0.0
Expand Down Expand Up @@ -208,3 +222,17 @@ export interface ClientEnd {
readonly _tag: "ClientEnd"
readonly clientId: number
}

/**
* @since 1.0.0
* @category response
*/
export interface Pong {
readonly _tag: "Pong"
}

/**
* @since 1.0.0
* @category response
*/
export const constPong: Pong = { _tag: "Pong" }
5 changes: 5 additions & 0 deletions packages/rpc/src/RpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type * as Rpc from "./Rpc.js"
import type * as RpcGroup from "./RpcGroup.js"
import {
constEof,
constPong,
type FromClient,
type FromClientEncoded,
type FromServer,
Expand Down Expand Up @@ -566,6 +567,10 @@ export const makeEncoded: <Rpcs extends Rpc.Any>(
)
break
}
case "Ping": {
yield* send(client.id, client.parser.encode(constPong))
break
}
default: {
if (!serialization.supportsBigInt) {
;(request as any).requestId = BigInt(request.requestId) as any
Expand Down

0 comments on commit 58a9436

Please sign in to comment.