-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from LOKE/feature/ts-and-codegenable
New client, typescript
- Loading branch information
Showing
13 changed files
with
2,559 additions
and
13,869 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# RPC Client | ||
|
||
Use [loke-cli](https://github.com/LOKE/loke-cli) to generate client code | ||
|
||
``` | ||
loke rpc install http://localhost:9999/rpc/yourservice | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { RPCClient } from "./lib/client"; | ||
|
||
import { load as _load } from "./lib/legacy"; | ||
|
||
export const load = _load; | ||
|
||
export default { load }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { AbortController } from "node-abort-controller"; | ||
import fetch from "node-fetch"; | ||
|
||
import { requestDuration, requestCount, failureCount } from "./metrics"; | ||
|
||
interface RequestOptions { | ||
timeout?: number; | ||
} | ||
|
||
export class RPCClient { | ||
private baseURL: URL; | ||
private serviceName: string; | ||
private service: string; | ||
|
||
constructor(baseURL: string, serviceName: string) { | ||
if (!baseURL.endsWith("/")) { | ||
baseURL += "/"; | ||
} | ||
this.serviceName = serviceName; | ||
this.baseURL = new URL(baseURL); | ||
this.service = this.baseURL.toString().replace(/\/^/, ""); | ||
} | ||
async request( | ||
methodName: string, | ||
params: Record<string, any>, | ||
options: RequestOptions = {} | ||
) { | ||
const url = new URL(methodName, this.baseURL); | ||
|
||
const requestMeta = { service: this.service, method: methodName }; | ||
const end = requestDuration.startTimer(requestMeta); | ||
requestCount.inc(requestMeta); | ||
|
||
const { timeout = 60000 } = options; | ||
|
||
const controller = new AbortController(); | ||
const tid = setTimeout(() => controller.abort(), timeout); | ||
|
||
let status = -1; | ||
try { | ||
const res = await fetch(url.toString(), { | ||
method: "POST", | ||
body: JSON.stringify(params), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
signal: controller.signal, | ||
}); | ||
|
||
clearTimeout(tid); | ||
|
||
if (res.ok) { | ||
return res.json(); | ||
} | ||
|
||
status = res.status; | ||
|
||
let errResult; | ||
if (res.headers.get("content-type") === "application/json") { | ||
errResult = await res.json(); | ||
} else { | ||
errResult = { | ||
expose: false, | ||
message: await res.text(), | ||
}; | ||
} | ||
mapError(this.serviceName, methodName, errResult); | ||
} catch (err: any) { | ||
failureCount.inc({ ...requestMeta, status_code: status, type: err.type }); | ||
throw err; | ||
} finally { | ||
end(); | ||
} | ||
} | ||
} | ||
|
||
function mapError(serviceName: string, methodName: string, errResult: any) { | ||
const source = `${serviceName}/${methodName}`; | ||
|
||
if (!errResult.type) { | ||
const newErr: any = new Error(errResult.message); | ||
newErr.code = errResult.code; | ||
newErr.expose = errResult.expose; | ||
newErr.source = [source]; | ||
throw newErr; | ||
} | ||
|
||
throw new RpcResponseError(source, errResult); | ||
} | ||
|
||
class RpcResponseError { | ||
source?: string[]; | ||
|
||
constructor(source: string, responseBody: any) { | ||
Object.defineProperty(this, "name", { | ||
configurable: true, | ||
enumerable: false, | ||
value: this.constructor.name, | ||
writable: true, | ||
}); | ||
|
||
Object.assign(this, responseBody); | ||
if (!this.source) this.source = []; | ||
this.source = [source, ...(this.source || [])]; | ||
|
||
Object.defineProperty(this, "stack", { | ||
configurable: true, | ||
enumerable: false, | ||
value: | ||
this.toString() + | ||
"\n" + | ||
[...this.source] | ||
.reverse() | ||
.map((s) => " via " + s) | ||
.join("\n"), | ||
writable: true, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Histogram, Counter } from "prom-client"; | ||
|
||
export const requestDuration = new Histogram({ | ||
name: "http_rpc_client_request_duration_seconds", | ||
help: "Duration of rpc requests from the client", | ||
labelNames: ["service", "method"], | ||
}); | ||
|
||
export const requestCount = new Counter({ | ||
name: "http_rpc_client_requests_total", | ||
help: "The total number of rpc requests from the client", | ||
labelNames: ["service", "method"], | ||
}); | ||
|
||
export const failureCount = new Counter({ | ||
name: "http_rpc_client_failures_total", | ||
help: "The total number of rpc failures from the client", | ||
labelNames: ["service", "method", "type", "status_code"], | ||
}); |
Oops, something went wrong.