Skip to content

Commit

Permalink
[taucorder] service #283
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Dec 11, 2024
1 parent d954c97 commit fd198fb
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 2 deletions.
28 changes: 28 additions & 0 deletions pkg/taucorder/clients/js/gen/taucorder/v1/health_connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @generated by protoc-gen-connect-es v1.4.0 with parameter "target=ts"
// @generated from file taucorder/v1/health.proto (package taucorder.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck

import { Empty } from "./common_pb.js";
import { MethodKind } from "@bufbuild/protobuf";

/**
* Service
*
* @generated from service taucorder.v1.HealthService
*/
export const HealthService = {
typeName: "taucorder.v1.HealthService",
methods: {
/**
* @generated from rpc taucorder.v1.HealthService.Ping
*/
ping: {
name: "Ping",
I: Empty,
O: Empty,
kind: MethodKind.Unary,
},
}
} as const;

19 changes: 19 additions & 0 deletions pkg/taucorder/clients/js/src/Health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Transport } from "@connectrpc/connect";
import { RPCClient } from "./HealthClient";
import { Empty, Node } from "../gen/taucorder/v1/common_pb";

export class Health {
private client: RPCClient;

constructor(transport: Transport) {
this.client = new RPCClient(transport);
}

/**
* Ping the health service
* @returns Empty response
*/
async ping() {
await this.client.ping(new Empty());
}
}
15 changes: 15 additions & 0 deletions pkg/taucorder/clients/js/src/HealthClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PromiseClient, createPromiseClient, Transport } from "@connectrpc/connect";
import { HealthService } from "../gen/taucorder/v1/health_connect";
import { Empty } from "../gen/taucorder/v1/common_pb";

export class RPCClient {
private client: PromiseClient<typeof HealthService>;

constructor(transport: Transport) {
this.client = createPromiseClient(HealthService, transport);
}

async ping(req: Empty): Promise<Empty> {
return this.client.ping(req);
}
}
7 changes: 5 additions & 2 deletions pkg/taucorder/clients/js/src/Taucorder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { exec, ChildProcess } from "child_process";
import { Taucorder } from "./Taucorder";
import { Taucorder, TaucorderService } from "./Taucorder";
import { Config } from "../gen/taucorder/v1/node_pb";
import { Peer } from "../gen/taucorder/v1/common_pb";

Expand All @@ -26,7 +26,7 @@ describe("Taucorder test", () => {
}
});
mockServerProcess.stderr?.on("data", (data: string) => {
console.error("Mock server error:", data);
console.log("Mock server error:", data);
reject(data);
});
});
Expand All @@ -35,6 +35,9 @@ describe("Taucorder test", () => {
throw error;
}

let tch = new TaucorderService(universeConfig.url);
await tch.wait(10);

taucorder = new Taucorder(
universeConfig.url,
new Config({
Expand Down
40 changes: 40 additions & 0 deletions pkg/taucorder/clients/js/src/Taucorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Seer } from "./Seer";
import { Swarm } from "./Swarm";
import { TNS } from "./TNS";
import { X509 } from "./X509";
import { Health } from "./Health";

class ExtendedAuth extends Auth {
private wrappers: {
Expand Down Expand Up @@ -73,6 +74,7 @@ export class Taucorder {
seer?: Seer;
swarm?: Swarm;
tns?: TNS;
health?: Health;
} = {};

constructor(rpcUrl: string, config: Config) {
Expand Down Expand Up @@ -151,4 +153,42 @@ export class Taucorder {
}
return this.wrappers.tns;
}

Health() {
if (!this.wrappers.health) {
this.wrappers.health = new Health(this.transport);
}
return this.wrappers.health;
}
}

export class TaucorderService extends Health {
constructor(rpcUrl: string) {
const transport = createConnectTransport({
baseUrl: rpcUrl,
httpVersion: "1.1",
});
super(transport);
}

/**
* Wait for the service to become available by pinging until success or timeout
* @param timeoutSeconds Maximum time to wait in seconds
* @throws Error if service does not become available within timeout
*/
async wait(timeoutSeconds: number): Promise<void> {
const start = Date.now();
const timeoutMs = timeoutSeconds * 1000;

while (Date.now() - start < timeoutMs) {
try {
await this.ping();
return;
} catch (err) {
// Wait 100ms before retrying
await new Promise(resolve => setTimeout(resolve, 100));
}
}
throw new Error(`Service did not become available within ${timeoutSeconds} seconds`);
}
}
84 changes: 84 additions & 0 deletions pkg/taucorder/proto/gen/taucorder/v1/health.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/taucorder/proto/taucorder/v1/health.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package taucorder.v1;

option go_package = ".";

import "taucorder/v1/common.proto";

// Data Structures

// Service
service HealthService {
rpc Ping(Empty) returns (Empty);
}
12 changes: 12 additions & 0 deletions pkg/taucorder/service/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package service

import (
"context"

"connectrpc.com/connect"
pb "github.com/taubyte/tau/pkg/taucorder/proto/gen/taucorder/v1"
)

func (ts *healthService) Ping(ctx context.Context, req *connect.Request[pb.Empty]) (*connect.Response[pb.Empty], error) {
return connect.NewResponse(&pb.Empty{}), nil
}
Loading

0 comments on commit fd198fb

Please sign in to comment.