Created
February 10, 2017 09:01
-
-
Save mpneuried/14638968390586a64cd3b712ee4381e3 to your computer and use it in GitHub Desktop.
parse and filter redis clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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