Skip to content

NodeJs - Pause/Resume stream request #471

Closed

Description

@hosseinGanjyar

I fetch n millions records from SqlServer (streaming) and process over every record. I want to call nested functions into request.on('row', row => { }) and pause streaming until end of latest nested functions.

#NodeJs
#SqlServer

I want this:

const sql = require('mssql')
 
sql.connect(config, err => {
    // ... error checks 
 
    const request = new sql.Request()
    request.stream = true // You can set streaming differently for each request 
    request.query('select * from verylargetable') // or request.execute(procedure) 
 
    request.on('recordset', columns => {
        // Emitted once for each recordset in a query 
    })
 
    request.on('row', row => {
        // Emitted for each row in a recordset 
        //call process_1(row)
    })
 
    request.on('error', err => {
        // May be emitted multiple times 
    })
 
    request.on('done', result => {
        // Always emitted as the last one 
    })
})
 
sql.on('error', err => {
    // ... error handler 
})

function process_1(data){
    //process done after n minutes
   //if end process, then call process_2(newData)
}
function process_2(newData){
    //process done after n minutes
   //if end process, then resume streaming request
}

Notice: I road tediousjs/tedious#181 and other issue about pause/pesume stream request but I didn't get any idea!
thanks

Activity

  1. simonbuchan commented on Feb 2, 2018

    @simonbuchan

    Use-case: backpressure from async-iterable usage:

    type Deferred<T> = {
      promise: Promise<T>,
      resolve: (result: T) => void,
      reject: (reason: any) => void,
    };
    function createDeferred<T>(): Deferred<T> {
      const result = {} as Deferred<T>;
      result.promise = new Promise<T>((resolve, reject) => {
        result.resolve = resolve;
        result.reject = reject;
      });
      return result;
    }
    
    function streamQuery<T>(query: TemplateStringsArray, ...interpolations: any[]): AsyncIterable<T> {
      return { [Symbol.asyncIterator]: asyncIterator };
    
      function asyncIterator(): AsyncIterator<any> {
        const request = pool.request();
        request.stream = true;
    
        // Reproduce pool.query but for request.
        // This is equivalent to request._template('query', query, interpolations);
        const command = [query[0]];
        for (let i = 0; i !== interpolations.length; i++) {
          request.input(`p${i}`, interpolations[i]);
          command.push(`@p${i}`, query[i + 1]);
        }
        request.query(command.join('')); // ignoring query result.
        request.on('row', row);
        request.on('error', error);
        request.on('done', done);
    
        // Note this will buffer without bound, mssql doesn't let us apply backpressure (suspend streaming).
    
        // Results received, but not requested, in the order received
        const buffer: Array<Promise<IteratorResult<T>>> = [];
        let bufferDone = false;
        // Results requested, but not received, in the order requested
        const pending: Array<Deferred<IteratorResult<T>>> = [];
        // At least one of the above should always be empty.
    
        return { next };
    
        function next(): Promise<IteratorResult<T>> {
          // if (buffer.length === LOW) request.resume();
          if (buffer.length) {
            return buffer.shift()!;
          }
          if (bufferDone) {
            return Promise.resolve({ value: undefined as any as T, done: true });
          }
          const deferred = createDeferred<IteratorResult<T>>();
          pending.push(deferred);
          return deferred.promise;
        }
    
        function row(row: T) {
          const result = { value: row, done: false };
          if (!pending.length) {
            buffer.push(Promise.resolve(result));
            // if (buffer.length === FULL) request.pause();
          } else {
            pending.shift()!.resolve(result);
          }
        }
    
        function error(reason: any) {
          if (!pending.length) {
            buffer.push(Promise.reject(reason));
            // if (buffer.length === FULL) request.pause();
          } else {
            pending.shift()!.reject(reason);
          }
        }
    
        function done() {
          bufferDone = true;
          while (pending.length) {
            pending.shift()!.resolve({ value: undefined as any as T, done: true });
          }
        }
      }
    }

    (note that this code is not terribly well tested, sorry!)

  2. mariotacke commented on Feb 9, 2018

    @mariotacke

    @simonbuchan, do you have an example which does not use TypeScript?

  3. simonbuchan commented on Feb 9, 2018

    @simonbuchan

    If you can't be bothered to strip the types yourself, you can paste it in the typescript playground

  4. ilusharulkov commented on Jun 18, 2018

    @ilusharulkov

    Hello!
    I have the same problem and solve this with tedious pause/pesume stream request.
    I've create small repo with example: tediousStreaming
    There are 2 examples:

    1. raw tedious
    2. node-mssql
      When use node-mssql make sure this commit is in node_modules
      I think data flow (process_1, process_2) is easy to emplement using librabry such as https://highlandjs.org/
  5. dhensby commented on May 7, 2019

    @dhensby
    Collaborator

    pause/resume of streams is now added

  6. hosseinGanjyar commented on May 8, 2019

    @hosseinGanjyar
    Author

    OH MY GOD...After about 2 years solved this issue.

    Tha's one small step for a man, one giant leap for mankind
    https://www.linkedin.com/feed/update/urn:li:activity:6531761519288418304/

  7. dhensby commented on May 8, 2019

    @dhensby
    Collaborator

    Yep - This was added in #775 and released in v5.0.0 - however, due to a bug in the tedious driver, it didn't actually work properly and is now in v6.0.0 without any bugs (see #838 and #832) 🎉

  8. mudouasenha commented on Nov 17, 2020

    @mudouasenha

    Thanks!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions