Skip to content

Instantly share code, notes, and snippets.

View adamnoto's full-sized avatar

Adam adamnoto

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.

Organization name: leexij@gmail.com
Serial Key: eNrzzU/OLi0odswsqslJTa3IzHJIz03MzNFLzs+tMTQyNrcwsTQyAIEa5xpDAIFxDy8k
# Extract and load data from BigQuery to BigQuery and Google CloudStorage
class Etl::BigQueryLoadQueryService
GOOGLE_API_PRIVATE_KEY = Rails.application.secrets.google_api_private_key
GOOGLE_PROJECT_ID = Rails.application.secrets.google_project_id
# google service account email address for use for API
GOOGLE_API_EMAIL_ADDRESS = Rails.application.secrets.google_api_email_address
SECRETS_CONFIGURATION = {
installed: {
def cloud_storage_bucket
@cloud_storage_bucket ||= begin
auth_client = get_auth_client "https://www.googleapis.com/auth/devstorage.full_control"
storage = Google::Cloud::Storage.new(project: GOOGLE_PROJECT_ID, keyfile: auth_client)
storage.bucket('bucket_name')
end
end
def upload_to_cloudstorage!
log("Uploading to Google CloudStorage...")
def google_drive_folder
@google_drive_folder ||= begin
access_token = get_access_token "https://www.googleapis.com/auth/drive"
session = GoogleDrive::Session.from_credentials(access_token)
session.file_by_id('some_google_folder_id')
end
end
def upload_to_google_drive!
temp_file = Tempfile.open
# Extract and load data from BigQuery to BigQuery and Google CloudStorage
class Etl::BigQueryLoadQueryService
GOOGLE_API_PRIVATE_KEY = Rails.application.secrets.google_api_private_key
GOOGLE_PROJECT_ID = Rails.application.secrets.google_project_id
# google service account email address for use for API
GOOGLE_API_EMAIL_ADDRESS = Rails.application.secrets.google_api_email_address
SECRETS_CONFIGURATION = {
installed: {
date_activity = Date.current.beginning_of_year
query = <<-sql.squish
SELECT * FROM some_table
WHERE booked_at >= CAST("#{date_activity.strftime("%Y-%m-%d")}" AS DATE)
sql
Etl::BigQueryLoadQueryService.new(
query: query,
dataset: 'statistics',
@adamnoto
adamnoto / adamtheengineer_phoenix14_webpack.config.js
Last active October 14, 2019 14:11
for the post about "Add Bootstrap to Phoenix 1.4" => webpack.config.js
// snip snip...
module.exports = (env, options) => ({
// snip snip...
module: {
rules: [
// .. some more snip snip..
{
test: /\.s?css$/,
// add sass-loader here
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
@adamnoto
adamnoto / adamtheengineer_phoenix14_app.js
Created October 14, 2019 08:10
for the post about "Add Bootstrap to Phoenix 1.4" => app.js
import css from "../css/app.scss" // ensure .scss!!!
import $ from "jquery"
global.jQuery = $
global.$ = $
require('bootstrap')
@adamnoto
adamnoto / adamtheengineer_phoenix14_app.scss
Created October 14, 2019 08:12
for the post about "Add Bootstrap to Phoenix 1.4" => app.scss
@import '~bootstrap/scss/bootstrap.scss';
@import '~font-awesome/scss/font-awesome.scss';