Skip to content

Instantly share code, notes, and snippets.

@mpneuried
Created February 10, 2017 09:01
Show Gist options
  • Save mpneuried/14638968390586a64cd3b712ee4381e3 to your computer and use it in GitHub Desktop.
Save mpneuried/14638968390586a64cd3b712ee4381e3 to your computer and use it in GitHub Desktop.
parse and filter redis clients
querystring = require('querystring')
crypto = require('crypto')
parseRedisClients = ( raw, filter={} )->
clients = []
for line in raw.split( "\n" ) when line.length > 1
cl = querystring.parse( line, " ", "=" )
if Object.keys( filter ).length
_match = true
for _k, _v of filter when cl[ _k ] isnt _v
_match = false
clients.push cl if _match
else
clients.push cl
return clients
getSelfRedisClient = ( redis, cb )->
rCliName = "client-" + crypto.randomBytes( 5 ).toString( "hex" )
redis.client "SETNAME", rCliName, ( err, info )->
if err
cb( err )
return
redis.client "list", ( err, clients )->
if err
cb( err )
return
[ selfCli ] = parseRedisClients( clients, { name: rCliName } )
if not selfCli?
cb( new Error( "client-not-found" ) )
return
cb( null, selfCli )
return
return
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment