How to use the stream-throttle.ThrottleGroup function in stream-throttle

To help you get started, we’ve selected a few stream-throttle examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github qiniu / kodo-browser / node / s3store / lib / stream.js View on Github external
download(params, config) {
    const client = this.client;

    if (!config) config = {};

    const maxRetries = config.maxRetries ? config.maxRetries : 3;
    const partSize = config.partSize ? config.partSize : 1 << 22; // 4 MB
    const maxConcurrentStreams = config.maxConcurrentStreams ? config.maxConcurrentStreams : 5;
    const objectSize = config.totalObjectSize ? config.totalObjectSize : 0;
    const downloaded = config.totalBytesDownloaded ? config.totalBytesDownloaded : 0;
    const throttleGroup = config.speedLimit ? new ThrottleGroup({rate: config.speedLimit * 1024}) : null;

    const rs = new Readable({ highWaterMark: 1 << 22 });
    const downloadPart = function(callback) {
      const context = this;
      const params = Object.assign({}, context.params);
      const part = new Buffer(context.size);
      let partLen = 0;
      let haveCallbacked = false;

      const httpGet = function() {
        const expectedSize = context.size - partLen;
        let actualSize = 0;
        params.Range = `bytes=${context.lowerBound + partLen}-${context.upperBound}`;
        rs.emit('debug', { req: 'start', from: context.lowerBound + partLen, to: context.upperBound, partId: context.partId });
        let stream = client.getObject(params).createReadStream();
        if (throttleGroup) {
github alibaba / anyproxy / proxy.js View on Github external
this.requestHandler = null;

    // copy the rule to keep the original proxyRule independent
    this.proxyRule = config.rule || {};

    if (config.silent) {
      logUtil.setPrintStatus(false);
    }

    if (config.throttle) {
      logUtil.printLog('throttle :' + config.throttle + 'kb/s');
      const rate = parseInt(config.throttle, 10);
      if (rate < 1) {
        throw new Error('Invalid throttle rate value, should be positive integer');
      }
      global._throttle = new ThrottleGroup({ rate: 1024 * rate }); // rate - byte/sec
    }

    // init recorder
    this.recorder = config.recorder;

    // init request handler
    const RequestHandler = util.freshRequire('./requestHandler');
    this.requestHandler = new RequestHandler({
      wsIntercept: config.wsIntercept,
      httpServerPort: config.port, // the http server port for http proxy
      forceProxyHttps: !!config.forceProxyHttps,
      dangerouslyIgnoreUnauthorized: !!config.dangerouslyIgnoreUnauthorized
    }, this.proxyRule, this.recorder);
  }
github BrowserSync / UI / lib / plugins / network-throttle / throttle-server.js View on Github external
return require(module).createServer(serverOpts, function (local) {

        var remote = require(module)[method]({
            host: opts.target.hostname,
            port: opts.target.port,
            allowHalfOpen: true,
            rejectUnauthorized: false
        });

        var upThrottle = new ThrottleGroup({ rate: options.upstream });
        var downThrottle = new ThrottleGroup({ rate: options.downstream });

        var localThrottle = upThrottle.throttle();
        var remoteThrottle = downThrottle.throttle();

        setTimeout(function () {
            local
                .pipe(localThrottle)
                .pipe(remote);
        }, opts.speed.latency);

        setTimeout(function () {
            remote
                .pipe(remoteThrottle)
                .pipe(local);
        }, opts.speed.latency);
github greim / hoxy / src / proxy.js View on Github external
if (val === undefined) { return }
        if (typeof val !== 'number') {
          throw new Error(`slow.${name} must be a number`)
        }
        if (val < 0) {
          throw new Error(`slow.${name} must be >= 0`)
        }
      })
      if (opts.rate) {
        slow.rate = new ThrottleGroup({ rate: opts.rate })
      }
      if (opts.latency) {
        slow.latency = opts.latency
      }
      if (opts.up) {
        slow.up = new ThrottleGroup({ rate: opts.up })
      }
      if (opts.down) {
        slow.down = new ThrottleGroup({ rate: opts.down })
      }
    } else {
      if (!this._slow) {
        return undefined
      } else {
        return this._slow.opts
      }
    }
  }
github greim / hoxy / src / proxy.js View on Github external
slow(opts) {
    if (opts) {
      let slow = this._slow = { opts, latency: 0 };
      ['rate', 'latency', 'up', 'down'].forEach(name => {
        let val = opts[name]
        if (val === undefined) { return }
        if (typeof val !== 'number') {
          throw new Error(`slow.${name} must be a number`)
        }
        if (val < 0) {
          throw new Error(`slow.${name} must be >= 0`)
        }
      })
      if (opts.rate) {
        slow.rate = new ThrottleGroup({ rate: opts.rate })
      }
      if (opts.latency) {
        slow.latency = opts.latency
      }
      if (opts.up) {
        slow.up = new ThrottleGroup({ rate: opts.up })
      }
      if (opts.down) {
        slow.down = new ThrottleGroup({ rate: opts.down })
      }
    } else {
      if (!this._slow) {
        return undefined
      } else {
        return this._slow.opts
      }
github beakerbrowser / beaker-core / dat / daemon / index.js View on Github external
async setBandwidthThrottle ({up, down}) {
    if (typeof up !== 'undefined') {
      upThrottleGroup = up ? new ThrottleGroup({rate: up * 1e6}) : null
    }
    if (typeof down !== 'undefined') {
      downThrottleGroup = down ? new ThrottleGroup({rate: down * 1e6}) : null
    }
  },
github iamdjones / sublime-text-browser-sync / browser-sync / node_modules / browser-sync-ui / lib / plugins / network-throttle / throttle-server.js View on Github external
return require(module).createServer(serverOpts, function (local) {

        var remote = require(module)[method]({
            host: opts.target.hostname,
            port: opts.target.port,
            allowHalfOpen: true,
            rejectUnauthorized: false
        });

        var upThrottle = new ThrottleGroup({ rate: options.upstream });
        var downThrottle = new ThrottleGroup({ rate: options.downstream });

        var localThrottle = upThrottle.throttle();
        var remoteThrottle = downThrottle.throttle();

        setTimeout(function () {
            local
                .pipe(localThrottle)
                .pipe(remote);
        }, opts.speed.latency);

        setTimeout(function () {
            remote
                .pipe(remoteThrottle)
                .pipe(local);
        }, opts.speed.latency);
github tjgq / grunt-throttle / tasks / throttle.js View on Github external
grunt.registerMultiTask('throttle', 'Grunt plugin for testing under a throttled connection.', function() {

    var options = extend(defaultOptions, this.data);

    if (typeof options.local_port === 'undefined') {
      grunt.fatal('Must specify local port');
    }

    if (typeof options.remote_port === 'undefined') {
      grunt.fatal('Must specify remote port');
    }

    var keepAlive = this.flags.keepalive || options.keepalive;

    var upThrottle = new ThrottleGroup({ rate: options.upstream });
    var downThrottle = new ThrottleGroup({ rate: options.downstream });

    var server = net.createServer({ allowHalfOpen: true }, function (local) {
      var remote = net.createConnection({
        host: options.remote_host,
        port: options.remote_port,
        allowHalfOpen: true
      });

      var localThrottle = upThrottle.throttle();
      var remoteThrottle = downThrottle.throttle();

      local.pipe(localThrottle).pipe(remote);
      local.on('error', function() {
        remote.destroy();
        local.destroy();
      });
github BrowserSync / UI / lib / plugins / network-throttle / throttle-server.js View on Github external
return require(module).createServer(serverOpts, function (local) {

        var remote = require(module)[method]({
            host: opts.target.hostname,
            port: opts.target.port,
            allowHalfOpen: true,
            rejectUnauthorized: false
        });

        var upThrottle = new ThrottleGroup({ rate: options.upstream });
        var downThrottle = new ThrottleGroup({ rate: options.downstream });

        var localThrottle = upThrottle.throttle();
        var remoteThrottle = downThrottle.throttle();

        setTimeout(function () {
            local
                .pipe(localThrottle)
                .pipe(remote);
        }, opts.speed.latency);

        setTimeout(function () {
            remote
                .pipe(remoteThrottle)
                .pipe(local);
        }, opts.speed.latency);
github beakerbrowser / beaker-core / dat / daemon / index.js View on Github external
async setBandwidthThrottle ({up, down}) {
    if (typeof up !== 'undefined') {
      upThrottleGroup = up ? new ThrottleGroup({rate: up * 1e6}) : null
    }
    if (typeof down !== 'undefined') {
      downThrottleGroup = down ? new ThrottleGroup({rate: down * 1e6}) : null
    }
  },

stream-throttle

A rate limiter for Node.js streams.

BSD-3-Clause
Latest version published 11 years ago

Package Health Score

65 / 100
Full package analysis