pthrift

A Thrift client utilising a pool of service connections and improved error handling/recovery

Usage no npm install needed!

<script type="module">
  import pthrift from 'https://cdn.skypack.dev/pthrift';
</script>

README

pthrift

npm version Build Status

Pooled thrift client, a node Thrift client utilising a pool of service connections and improved error handling/recovery

npm install pthrift

Features

  • proper Promise support in pool instance con/destruction
  • correct behaviour on "Internal Error", aka, socket misalignment on unhandled server errors
  • better/existent timeout support for: connections, pool checkout, execution
  • builtin retry support using upstream Thrift code
  • faster detection and pruning of dead connections
  • async/await compatibility and other niceties
  • enabled oneway function

Example usage

Given a thrift file calculator_service.thrift with contents:

exception OutOfRange {
  1: string message
}
service CalculatorService {
  i32 add(1:i32 num1, 2:i32 num2) throws (1:OutOfRange out_of_range)
}

Compile with the thrift command (installable via your package manager):

thrift --gen js:node calculator_service.thrift

This will produce JS definitions for your service, which can then be used to create a client:

const CalculatorService = require("./CalculatorService");
const Pool = require("pthrift");

// host and port are mandatory see other config options in comments
const host = "127.0.0.1",
  port = 9000;
const client = Pool(
  CalculatorService,
  { host, port },
  {
    poolOptions: { max: 5 },
    onAcqTimeout: (err) => {},
    onConnTimeout: (err) => {},
    onClosedError: (err) => {},
  }
);

// use the client as you would a regular client, get pooling for free
client.add(1, 2).then((sum) => console.log(sum));

Testing

Tests use the calculator_service.thrift example service located in spec/support/thrift. A compiled version is committed to this repository; to recompile it, install thrift using your package manager (e.g., brew install thrift), then follow the instructions in the file.

Tests can be run using

npm test

License

Copyright (2018-2021) 北京华夏春松科技有限公司

Apache License Version 2.0

chatoper banner