Skip to content

Commit

Permalink
Add 'packages/tcp-transport/' from commit '6ff37d9716cd9468104d4eb779…
Browse files Browse the repository at this point in the history
…d59120f4bc61e1'

git-subtree-dir: packages/tcp-transport
git-subtree-mainline: 8e696f1
git-subtree-split: 6ff37d9
  • Loading branch information
esatterwhite committed Mar 18, 2020
2 parents 8e696f1 + 6ff37d9 commit 0c04fbf
Show file tree
Hide file tree
Showing 10 changed files with 4,966 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/tcp-transport/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
coverage/
.nyc_output/
*.save
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

12 changes: 12 additions & 0 deletions packages/tcp-transport/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Dockerfile*
.dockerignore
payload.txt
*.save
compose/
assets
*.tgz
jsdoc.json
docs/
.test
tutorials/
echo.js
21 changes: 21 additions & 0 deletions packages/tcp-transport/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 71 additions & 0 deletions packages/tcp-transport/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# skyring-tcp-transport
A TCP based transport with connection pooling for Skyring.

## Installation

```bash
$ npm install @skyring/tcp-transport --save
```

## Usage

Skyring accepts an array property `transports`. Each entry can be a string or a named function.
If given a string, skyring will pass it to `require` which must resolve to a named function

```js
const Skyring = require('skyring')
const server = new Skyring({
transports: ['@skyring/tcp-transport']
, seeds: ['localhost:3455']
})

server
.load()
.listen(3000)
```


### Example Echo Server

```js
// tcp echo server
'use strict'

let count = 0
const port = process.env.PORT || 5555
const net = require('net')
const server = net.createServer((socket) => {
socket.on('data', (chunk) => {
console.log(`${++count} ` + chunk)
})
})

process.once('SIGINT', onSignal)
process.once('SIGTERM', onSignal)
server.listen(port, (err) => {
if (err) {
console.log(err)
process.exitCode = 1
}
console.log('server listening')
})
function onSignal() {
server.close()
}
```

```bash
$ curl -XPOST http://localhost:3000/timer -H 'Content-Type: application/json' -d '{
"timeout":3000
, "data":"hello world!"
, "callback": {
"transport": "tcp"
, "method":"unused"
, "uri": "tcp://0.0.0.0:5555"
}
}'
```

```
>>> 1 hello world !
```
23 changes: 23 additions & 0 deletions packages/tcp-transport/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

let count = 0
const port = process.env.PORT || 5555
const net = require('net')
const server = net.createServer((socket) => {
socket.on('data', (chunk) => {
console.log(`${++count} ` + chunk)
})
})

process.once('SIGINT', onSignal)
process.once('SIGTERM', onSignal)
server.listen(port, (err) => {
if (err) {
console.log(err)
process.exitCode = 1
}
console.log('server listening')
})
function onSignal() {
server.close()
}
3 changes: 3 additions & 0 deletions packages/tcp-transport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('./lib/tcp')
78 changes: 78 additions & 0 deletions packages/tcp-transport/lib/tcp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'
const Url = require('url')
, net = require('net')
, Pool = require('generic-pool')
, debug = require('debug')('skyring:transports:tcp')
, connections = new Map()
;

module.exports = function tcp( method, url, payload, id, cache ){
const pool = getPool(url)
pool.acquire().then((conn) => {
const out = typeof payload === 'object' ? JSON.stringify(payload) : payload
cache.remove(id)
conn.write(out + '\n', 'utf8',() => {
pool.release(conn)
})
})
.catch((e) => {
debug('error', e)
const err = new Error(`Unable to exeute tcp transport for timer ${id}`)
err.name = 'ETCPERR'
console.error(err)
})
}

module.exports.shutdown = shutdown

function getPool(addr, opts) {
if (connections.has(addr)) return connections.get(addr)
const pool = Pool.createPool({
create: () => {
debug("creating")
const url = Url.parse(addr)
let conn = net.connect(url.port, url.hostname)
conn.setNoDelay(true)
conn.setKeepAlive(true)
conn.once('error', (err) => {
debug("destroying connection", err.message)
pool.destroy(conn).catch(()=>{})
connections.delete(addr)
})
return conn
}
, destroy: (client, cb) => {
client.on('end', () => {
debug('connection closed')
})
client.destroy()
}
, validate: (conn) => {
return Promise.resolve(conn.destroyed)
}
}, {
min: 1
, max: 100
, testOnBorrow: true
})
connections.set(addr, pool)
return pool
}

function shutdown(cb = ()=>{}) {
const entries = connections.entries()
const run = () => {
const next = entries.next()
if (next.done) return cb()
const [key, value] = next.value
debug('disconnecting %s', key)
value.drain().then(() => {
value.clear()
debug(`removing tcp connection to ${key}`)
connections.delete(key)
run()
})
}

run()
}
Loading

0 comments on commit 0c04fbf

Please sign in to comment.