Skip to content

Instantly share code, notes, and snippets.

View jackbkennedy's full-sized avatar
🤟
Stoked

Jack Kennedy jackbkennedy

🤟
Stoked
View GitHub Profile

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.

@AndrewDryga
AndrewDryga / dynamic_changeset.ex
Last active July 22, 2024 03:46
Dynamic embeds with Ecto
defmodule DynamicChangeset do
@moduledoc """
This module provides helper functions to extend `Ecto.Changeset` to support
dynamic embeds.
"""
alias Ecto.Changeset
@doc """
Casts the given embed with the embedded changeset and field which is used to define it's type.
@jackbkennedy
jackbkennedy / useCapslock.ts
Last active August 12, 2023 14:35
useCapslock
import {useState, useEffect, useCallback} from 'react';
const EVENT_KEY_DOWN = 'keydown';
const EVENT_KEY_UP = 'keyup';
/* Hook to verify state of Caps Lock */
export function useCaplocks(): boolean {
/* State for keeping track of whether caps lock is on */
const [isCaplocksActive, setIsCapLocksActive] = useState<boolean>(false);
@joshnuss
joshnuss / streaming_http_requests.ex
Last active March 31, 2023 09:02
Streaming HTTP requests
defmodule MyApp.Integrations.Skubana do
@host "..."
@headers [ ... ]
# returns a stream of shipments
def get_shipments do
# start with page 1
start = fn -> 1 end
# create a stream, it will make HTTP requests until the page returned is empty
@jackbkennedy
jackbkennedy / code.gs
Last active November 2, 2023 17:41
Google Sheet AI Content Generation
// This function runs automatically when the Google Sheet is opened.
function onOpen() {
// Obtain the user interface of the Google Spreadsheet.
var ui = SpreadsheetApp.getUi();
// Create a custom menu titled 'AI Content Generation' in the Spreadsheet's menu bar.
ui.createMenu('AI Content Generation')
// Add a menu item 'Generate All Values' that when clicked, will run the function 'generateAllValues'.
.addItem('Generate All Values', 'generateAllValues')
// Add another menu item 'Update Missing Only' that when clicked, will run the function 'updateMissingValues'.
@jackbkennedy
jackbkennedy / ZoomableImage.tsx
Created November 2, 2023 09:49
A react image component that focuses an image in the middle of the page and zooms in to make it easier to see.
import React, { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
type ZoomableImageProps = {
url: string;
style?: React.CSSProperties; // for custom styles
className?: string; // to allow custom classes
};
/*
@PJUllrich
PJUllrich / http.ex
Last active January 25, 2024 09:09
HTTP Wrapper with optional Caching
defmodule MyApp.Http do
@moduledoc """
Exposes functions to make HTTP requests and optionally cache the response.
If you want to cache the request, simply add `cache: true` to the
request options. You can also define an option time-to-live (TTL) with
`cache_ttl: ttl_in_milliseconds`. The default TTL is 5min.
Only caches 2xx responses.
"""