worker-tokens
v0.0.0-development
Published
A library for creating and consuming tokens via services using Redis
Downloads
1
Readme
Worker Tokens
Like proof-of-work but without all the hassle
Overview
This project gives an interface for creating, using, and checking tokens
stored in a redis
instance. The overall goal of this is to create a way to for workers/instances to have a limit on the amount of work they are allowed to perform inside of a given system.
Usage
- Add it via
yarn
ornpm
$ yarn add worker-tokens
// Or
$ npm i -s worker-tokens
- Create token interface
// via es modules
import { createTokenInterface } from 'worker-tokens'
// or via cjs modules
const { createTokenInterface } = require('worker-tokens')
- Call
createTokenInterface
with an object that has none or all of the below values:
const tokenInterface = createTokenInterface({
cooldown: 1000, // Create a token every x milliseconds
redisPort: 9876, // Connect to Redis port
redisHost: 'localhost', // Connect to Redis host
tokenLimit: 60, // The max life of a token
prefix: 'DEMO_TOKEN_' // The prefix for all tokens created by this interface
})
Interface
tokenGenerator: number -> Observable
- Takes in a
cooldown
value and returns an observable of tokens tokenInterface.tokenGenerator(1000)
will emit a new token every 1s
- Takes in a
checkToken: string -> Observable
- Takes in a
token
and returns an observable of if that token is valid checkToken('token_123')
will emittrue/false
or throw error
- Takes in a
useToken: string -> Observable
- Takes in a
token
and returns an observable of the work to be performed useToken('token_123')
will emit1
if successful
- Takes in a