Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throttle a Highland.js or Node.js stream to one object per second?

I'd like to be able to throttle the calls to getPagerank() to one per second. I've tried various things but can't get it to work.

var pagerank = require('pagerank');
var _ = require('highland');

var urls = [
    'google.com',
    'yahoo.com',
    'bing.com'
];

var getPagerank = _.wrapCallback(pagerank);

// I want to throttle calls to getPagerank to 1/sec
var pageRanks = _(urls)
    .map(getPagerank)
    .merge();

pageRanks.toArray(function(arr) {
    console.log(arr);
});
like image 952
Jason Avatar asked Sep 27 '22 17:09

Jason


1 Answers

You can use .ratelimit()

e.g. this will limit the stream to processing one item of the array per one second

var _ = require('highland');

_([1,2,3,4]).ratelimit(1, 1000).map(function(x){
  return String(x);
})
.pipe(process.stdout);
like image 55
Yuri Zarubin Avatar answered Oct 04 '22 19:10

Yuri Zarubin