AbortError: SCAN can't be processed. The connection is already closed.
I have just two properties: A = anything AA = anything
Result: cursor 0 cursor 1 cursor 3 AbortError: SCAN can't be processed. The connection is already closed.
function scanAsync( redis, cursor, pattern, set ) {
return new Promise( ( resolve, reject ) => {
scan( redis, cursor, pattern, set, resolve, reject );
} );
}
function scan( redis, cursor, pattern, set, resolve, reject ) {
console.log( "cursor", cursor );
// Same result if using redis.scan( cursor, 'MATCH', pattern, 'COUNT', '1', function( err, res ) {
redis.send_command( 'scan', [ cursor, 'MATCH', pattern, 'COUNT', '1' ], function( err, res ) {
if ( err ) {
throw err;
}
// Update the cursor position for the next scan
cursor = res[ 0 ];
// get the SCAN result for this iteration
var keys = res[ 1 ];
// Result is more or less than COUNT, or no keys may be returned.
// SCAN may return the same key multiple times.
if ( keys.length > 0 ) {
keys.forEach( function( key, i ) {
set.add( key );
} );
}
// An iteration starts when the cursor is set to 0
// and terminates when the cursor returned by the server is 0.
if ( cursor === '0' ) {
resolve( true );
}
else {
scan( redis, cursor, pattern, set, resolve, reject );
}
} );
}
var myResults = new Set();
var redis = require( "redis" );
scanAsync( this.redis, '0', "A*", myResults ).then( function( value ) {
console.log( value, "myResults", myResults, Array.from( myResults ) );
}, function( err ) {
console.log( err );
} );
The server works ok:
mine:0>scan 0 'A*' 1 "ERR syntax error" mine:0>scan 0 MATCH A* COUNT 1
- "1"
-
- "AA"
mine:0>scan 1 MATCH A* COUNT 1
- "3"
-
- "A"
mine:0>scan 3 MATCH A* COUNT 1
- "0"
@cague can you please format your code? You can for example use:
```js // Your code that is formatted as JavaScript. ```
In general: this does not seem to be an issue with node_redis. The connection could be closed for multiple reasons.
@BridgeAR Thanks for the reply. I think because the error is reproducible every time with the code I sent, there is a bug. If there was anything random (if the code worked sometimes), I might suspect other reasons. But there isn't.
@cague It seems that is caused by the connection and just like what @BridgeAR said
Also, I tried your code and it worked well.
version
- redis: "^2.8.0"
- node.js: "v10.15.1"
- redis-server: "5.0.5"
key/value
- sadd A 1 2 3
- sadd AA 1 2 3 4
code
function scanAsync( redis, cursor, pattern, set ) {
return new Promise( ( resolve, reject ) => {
scan( redis, cursor, pattern, set, resolve, reject );
} );
}
function scan( redis, cursor, pattern, set, resolve, reject ) {
console.log( "cursor", cursor );
redis.send_command( 'scan', [ cursor, 'MATCH', pattern, 'COUNT', '1' ], function( err, res ) {
if ( err ) {
throw err;
}
cursor = res[ 0 ];
var keys = res[ 1 ];
if ( keys.length > 0 ) {
keys.forEach( function( key, i ) {
set.add( key );
} );
}
if ( cursor === '0' ) {
resolve( true );
}
else {
scan( redis, cursor, pattern, set, resolve, reject );
}
} );
}
const myResults = new Set();
const redis = require("redis");
const client = new redis.createClient({port: 6378});
scanAsync(client, '0', "A*", myResults ).then( function( value ) {
console.log( value, "myResults", myResults, Array.from( myResults ) );
process.exit(0);
}, function( err ) {
console.log( err );
process.exit(1);
} );
output
cursor 0
cursor 1
true 'myResults' Set { 'AA', 'A' } [ 'AA', 'A' ]
In my case, whenever they find a data it quits. But when I set COUNT it works fine. I tried this for 150k data. I want to solve this without using the COUNT. As I might not know about the total keys in Redis.
Calling redis
async function (param) {
let cursor = '0'
while(true){
const getAsync = promisify(this.client.scan).bind(this.client)
const reply = await getAsync(cursor, 'MATCH', pattern)
cursor = reply[0];
console.log('test', reply[1])
if(reply[1] != false){
return reply[1]
}
if(cursor == 0){
break
}
}
}