Skip to content

Instantly share code, notes, and snippets.

View cggaurav's full-sized avatar
🏠
Working from home

Gaurav cggaurav

🏠
Working from home
View GitHub Profile
@cggaurav
cggaurav / shuffle.js
Created December 31, 2018 14:33 — forked from guilhermepontes/shuffle.js
Shuffle Array - JavaScript ES2015, ES6
// original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
// fully random by @BetonMAN
const shuffleArray = arr => arr
.map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map(a => a[1]);
shuffleArray([1, 2, 3]) //[3, 1, 2]

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@cggaurav
cggaurav / generators-test.js
Created August 25, 2018 01:10 — forked from grimen/generators-test.js
Experiments with Node.js Generator API - partially requires Node 10.
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const { promisify } = require('util')
sleep = promisify(setTimeout)
@cggaurav
cggaurav / streams-test.js
Created June 18, 2018 20:06 — forked from grimen/streams-test.js
Experiments with Node.js Stream API.
/* =============================================
Dependencies
------------------------------------------ */
const debug = require('debug')
const { Readable, Writable, Transform } = require('stream')
const JSONStream = require('JSONStream')
@cggaurav
cggaurav / cloudapp-export.rb
Last active October 9, 2017 00:10 — forked from adamcooke/cloudapp-export.rb
Export all your drops from CloudApp using this quick Ruby script
#!/usr/bin/env ruby
# A quick script to download all your files from CloudApp.
# To run this just run the script passing your e-mail & password
# to the script, for example:
#
# gem install cloudapp_api
# ruby cloudapp-export.rb adam@atechmedia.com mypassword
#
@cggaurav
cggaurav / promises-faq.md
Created May 18, 2017 23:45 — forked from joepie91/promises-faq.md
The Promises FAQ - addressing the most common questions and misconceptions about Promises.
$ wget \
--recursive \
--no-clobber \
--page-requisites \
--html-extension \
--convert-links \
--restrict-file-names=windows \
--domains website.org \
--no-parent \
www.website.org/tutorials/html/
/*
* Based on https://gist.github.com/583836 from http://stackoverflow.com/questions/3709391/node-js-base64-encode-a-downloaded-image-for-use-in-data-uri.
* Neither that gist nor this one work for me in 0.2.x or 0.3.x.
*/
var request = require('request'),
BufferList = require('bufferlist').BufferList,
sys = require('sys'),
bl = new BufferList(),
url = 'http://nodejs.org/logo.png'
;
@cggaurav
cggaurav / README.markdown
Created September 9, 2012 04:01 — forked from gudbergur/README.markdown
Bootstrap's Typeahead plugin extended (allowing for AJAX functionality) among other things

This is a fork of Bootstrap Typeahead that adds minimal but powerful extensions.

For example, process typeahead list asynchronously and return objects

  # This example does an AJAX lookup and is in CoffeeScript
  $('.typeahead').typeahead(
    # source can be a function
    source: (typeahead, query) ->
 # this function receives the typeahead object and the query string
@cggaurav
cggaurav / index.html
Created March 21, 2012 15:07 — forked from mbostock/README.md
Interactive Tree (D3)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Node-Link Tree</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.25.0"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.25.0"></script>
<style type="text/css">
circle.node {