Skip to content

Commit fd198fb

Browse files
authored
[taucorder] service #283
1 parent d954c97 commit fd198fb

File tree

14 files changed

+540
-2
lines changed

14 files changed

+540
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @generated by protoc-gen-connect-es v1.4.0 with parameter "target=ts"
2+
// @generated from file taucorder/v1/health.proto (package taucorder.v1, syntax proto3)
3+
/* eslint-disable */
4+
// @ts-nocheck
5+
6+
import { Empty } from "./common_pb.js";
7+
import { MethodKind } from "@bufbuild/protobuf";
8+
9+
/**
10+
* Service
11+
*
12+
* @generated from service taucorder.v1.HealthService
13+
*/
14+
export const HealthService = {
15+
typeName: "taucorder.v1.HealthService",
16+
methods: {
17+
/**
18+
* @generated from rpc taucorder.v1.HealthService.Ping
19+
*/
20+
ping: {
21+
name: "Ping",
22+
I: Empty,
23+
O: Empty,
24+
kind: MethodKind.Unary,
25+
},
26+
}
27+
} as const;
28+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Transport } from "@connectrpc/connect";
2+
import { RPCClient } from "./HealthClient";
3+
import { Empty, Node } from "../gen/taucorder/v1/common_pb";
4+
5+
export class Health {
6+
private client: RPCClient;
7+
8+
constructor(transport: Transport) {
9+
this.client = new RPCClient(transport);
10+
}
11+
12+
/**
13+
* Ping the health service
14+
* @returns Empty response
15+
*/
16+
async ping() {
17+
await this.client.ping(new Empty());
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PromiseClient, createPromiseClient, Transport } from "@connectrpc/connect";
2+
import { HealthService } from "../gen/taucorder/v1/health_connect";
3+
import { Empty } from "../gen/taucorder/v1/common_pb";
4+
5+
export class RPCClient {
6+
private client: PromiseClient<typeof HealthService>;
7+
8+
constructor(transport: Transport) {
9+
this.client = createPromiseClient(HealthService, transport);
10+
}
11+
12+
async ping(req: Empty): Promise<Empty> {
13+
return this.client.ping(req);
14+
}
15+
}

pkg/taucorder/clients/js/src/Taucorder.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exec, ChildProcess } from "child_process";
2-
import { Taucorder } from "./Taucorder";
2+
import { Taucorder, TaucorderService } from "./Taucorder";
33
import { Config } from "../gen/taucorder/v1/node_pb";
44
import { Peer } from "../gen/taucorder/v1/common_pb";
55

@@ -26,7 +26,7 @@ describe("Taucorder test", () => {
2626
}
2727
});
2828
mockServerProcess.stderr?.on("data", (data: string) => {
29-
console.error("Mock server error:", data);
29+
console.log("Mock server error:", data);
3030
reject(data);
3131
});
3232
});
@@ -35,6 +35,9 @@ describe("Taucorder test", () => {
3535
throw error;
3636
}
3737

38+
let tch = new TaucorderService(universeConfig.url);
39+
await tch.wait(10);
40+
3841
taucorder = new Taucorder(
3942
universeConfig.url,
4043
new Config({

pkg/taucorder/clients/js/src/Taucorder.ts

+40
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Seer } from "./Seer";
1414
import { Swarm } from "./Swarm";
1515
import { TNS } from "./TNS";
1616
import { X509 } from "./X509";
17+
import { Health } from "./Health";
1718

1819
class ExtendedAuth extends Auth {
1920
private wrappers: {
@@ -73,6 +74,7 @@ export class Taucorder {
7374
seer?: Seer;
7475
swarm?: Swarm;
7576
tns?: TNS;
77+
health?: Health;
7678
} = {};
7779

7880
constructor(rpcUrl: string, config: Config) {
@@ -151,4 +153,42 @@ export class Taucorder {
151153
}
152154
return this.wrappers.tns;
153155
}
156+
157+
Health() {
158+
if (!this.wrappers.health) {
159+
this.wrappers.health = new Health(this.transport);
160+
}
161+
return this.wrappers.health;
162+
}
163+
}
164+
165+
export class TaucorderService extends Health {
166+
constructor(rpcUrl: string) {
167+
const transport = createConnectTransport({
168+
baseUrl: rpcUrl,
169+
httpVersion: "1.1",
170+
});
171+
super(transport);
172+
}
173+
174+
/**
175+
* Wait for the service to become available by pinging until success or timeout
176+
* @param timeoutSeconds Maximum time to wait in seconds
177+
* @throws Error if service does not become available within timeout
178+
*/
179+
async wait(timeoutSeconds: number): Promise<void> {
180+
const start = Date.now();
181+
const timeoutMs = timeoutSeconds * 1000;
182+
183+
while (Date.now() - start < timeoutMs) {
184+
try {
185+
await this.ping();
186+
return;
187+
} catch (err) {
188+
// Wait 100ms before retrying
189+
await new Promise(resolve => setTimeout(resolve, 100));
190+
}
191+
}
192+
throw new Error(`Service did not become available within ${timeoutSeconds} seconds`);
193+
}
154194
}

pkg/taucorder/proto/gen/taucorder/v1/health.pb.go

+84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/taucorder/proto/gen/taucorder/v1/taucorderv1connect/health.connect.go

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
syntax = "proto3";
2+
3+
package taucorder.v1;
4+
5+
option go_package = ".";
6+
7+
import "taucorder/v1/common.proto";
8+
9+
// Data Structures
10+
11+
// Service
12+
service HealthService {
13+
rpc Ping(Empty) returns (Empty);
14+
}

pkg/taucorder/service/health.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package service
2+
3+
import (
4+
"context"
5+
6+
"connectrpc.com/connect"
7+
pb "github.com/taubyte/tau/pkg/taucorder/proto/gen/taucorder/v1"
8+
)
9+
10+
func (ts *healthService) Ping(ctx context.Context, req *connect.Request[pb.Empty]) (*connect.Response[pb.Empty], error) {
11+
return connect.NewResponse(&pb.Empty{}), nil
12+
}

0 commit comments

Comments
 (0)