tarantool-transport v0.2.7
#Transport - low-level Tarantool driver
Transport incapsulates socket, manages callbacks, composes request headers, parses response headers, and composes response from several data packets.
Use Connector as a high-level driver or create your own.
NPM
npm install tarantool-transportAPI and usage
Call Transport.connect port, host, callback or new Transport socket to instantiate transport.
First way is common and preferrable while second allows to prepare socket, mock it or hack it.
Call transport.request type, body, callback to send request.
typemust be Number, any valid request type: 0x0D, 0x11, 0x13, 0x15, 0x16 or 0xFF00.bodymust be Buffer (preferrable) or String (empty string is usable, see example below).callbackwill receive response body as Buffer, maybe empty, nevernullorundefined.
All arguments are obligatory.
Example
Transport = require 'tarantool-transport'
PING = 0xFF00 # ping request type
transport = Transport.connect port, host, -> # on connection
transport.request PING, '', -> # on response
console.log 'got ping response'
console.log 'sent ping request'
# the other way, if you want to prepare socket somehow
# net = require 'net'
# socket = net.connect port, host, ->
# # on connection
# transport = new Transport socketHacking
Implementation notes
Before reading source please note that:
- In Tarantool, request and response headers are sequences of unsigned little-endian 32-bit integers.
- Tarantool allows to set
request_id. Server will just white this value intoresponse, it won't check or compare it with anything. Intransportwe call this fieldcallback_id— we pass callbacks and one response calls means one callback here.
Interaction with Socket
Constructed transport sets up socket in this way:
socket.unref()to letnode.jsexit if we're not awaiting responsessocket.setNoDelay()to reduce latency (added in 0.2.3)socket.on('data', cb)to parse and process responses
transport does socket.ref() on request and socket.unref() on last awaited response. Thus, socket prevents node.js from shutting down until it receives all responses.
This is the most common use case, but you can play with socket in any way, at your own risk.
Inner variables
For those who want to hack Transport — list of inner variables:
socket—netsocket or Object you passed to constructorremainder— Buffer, will prepend next data chunk in order to compose responses from several data packetscallbacks— Hash (Object), keys are numeric response ids, values are passed callbacksnextCallbackId— non-negative Number, incremented on request, when reaches 4294967296 overflows to 0, you can use it to describe request frequencyresponsesAwaiting— non-negative Number, incremented on request, decremented on response, stored to know whenref()andunref()thesocket
Bugs and issues
Bug reports and pull requests are welcome.
LICENSE
Tarantool Transport for node.js is published under MIT license.
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago