Skip to content

Instantly share code, notes, and snippets.

View chilts's full-sized avatar

Andrew Chilton chilts

View GitHub Profile
@chilts
chilts / lock.js
Last active December 17, 2015 09:39
Lock API using Redis
var redis = require('redis');
var redlock = require('redlock');
var getLock2 = redlock({
client : client,
prefix : 'lock:', // optional
});
var getLock2 = redlock({
// same credentials as redis.createClient()
// this needs to be read from the DB on startup
var userId = 1;
function putUser(user, cb) {
// am making this up for the purposes of demonstration - don't know what your use-case is
userId++;
user.id = userId;
var ops = [
{ type: 'put', key: 'sequence:userId', value : userId },
@chilts
chilts / README.md
Created April 2, 2013 19:15
Node.js: Hints, Tips and Maybes
@chilts
chilts / different-pg-connect.js
Created March 5, 2013 09:06
An example of how pg.connect() behaves differently, when it probably should behave the same. :)
var async = require('async');
var pg = require('pg');
var connString = "postgres://postgres@localhost/template1";
// attempt 1 - all ok
pg.connect(connString, function(err, client, done) {
// this function has a .length of 3 ... therefore done is defined
console.log('1) Connected ok');
done();
// pg.end();
@chilts
chilts / sdf
Last active December 11, 2015 23:38
models = require './'
sql = require 'sql'
crypto = require 'crypto'
Downstairs = require('downstairs').Downstairs
Table = require('downstairs').Table
env = require './../config/env'
uuid = require 'node-uuid'
logging = require './../lib/logging'
moment = require 'moment'
$ npm test
> sound@0.1.0 test /home/chilts/src/appsattic-sound
> tap test/*.js
not ok test/basic.js .................................... 6/9
Command: "node" "basic.js"
TAP version 13
ok 1 is an object
ok 2 string passes
@chilts
chilts / README.md
Created October 4, 2012 00:40
A short and sharp explanation of streams and events (in upcoming Node.js)

Streams

Readable Streams

Events:

  • end - once all data has been read and ...

Writable Streams

@chilts
chilts / test-callbacks.js
Created August 23, 2012 21:03
Example Node.js + Postgres tests
var fmt = require('fmt');
var pg = require('pg');
var conString = "pg://scroober@localhost/scroober";
pg.connect(conString, function(err, cl) {
if (err) {
console.log(err);
return;
}
@chilts
chilts / server-listening.js
Created May 14, 2012 03:35
Modified Node.js example from the front page : http://nodejs.org/
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1', function() {
console.log('listening');
});
console.log('Server running at http://127.0.0.1:1337/');
@chilts
chilts / Output
Created May 6, 2012 08:17
Default value for 'c' doesn't proprogate to argv.concurrency
$ ./test.js -b my-bucket
Bucket=my-bucket
Concurrency=3
Bucket=my-bucket
Concurrency=undefined