Skip to content

Instantly share code, notes, and snippets.

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

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@alexandreaquiles
alexandreaquiles / EscapeXmlELResolverListener.java
Last active May 7, 2020 07:19
EscapeXmlELResolverListener for Spring Boot based on pukkaone.github.io/2011/01/03/jsp-cross-site-scripting-elresolver.html
package br.com.caelum.fj91.seguranca;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspContext;
@jpukg
jpukg / TaskRunnerTest.java
Created October 10, 2017 02:07 — forked from onraz/TaskRunnerTest.java
Using CountDownLatch to Test Asynchronous Code
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@alexandreaquiles
alexandreaquiles / github.gql
Created August 28, 2017 16:32
Consulta GraphQL que busca estatísticas do projeto Express: o número de stars, de pull requests abertos, de issues abertas, a data da última release e informações do último commit.
query {
repository(owner:"expressjs", name: "express") {
stargazers {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
issues(states:OPEN) {
totalCount
package monoids;
public abstract class Monoid<T> {
public abstract T combine(T one, T other);
public abstract T identity();
public T fold(Iterable<T> elements) {
T result = identity();
for (T i : elements) {

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.

@adrianoalmeida7
adrianoalmeida7 / template_amp.html
Last active December 22, 2022 14:31
HTML Básico para AMP
<!doctype html>
<html amp lang="pt-BR">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<link rel="canonical" href="http://caminho_do_arquivo_original/arquivo.html">
<title>Página compatvel com AMP</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html" />
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{
@joshlong
joshlong / TestApplication.java
Last active June 9, 2022 09:49
an example demonstrating Spring's new `InjectionPoint` support
package com.example;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@rgl
rgl / add-virtualbox-5.1-support-to-vagrant.sh
Created July 16, 2016 14:18
Add VirtualBox 5.1 support to Vagrant 1.8.4
# see https://github.com/mitchellh/vagrant/pull/7574
curl -s https://github.com/mitchellh/vagrant/commit/b57b0e0d48fe8b4196ca6b7e01bb6c1ecb4b69f9.patch \
| sudo patch -p1 -d /opt/vagrant/embedded/gems/gems/vagrant-1.8.4
@juanplopes
juanplopes / Main.java
Created February 18, 2016 12:51
Object Pool example
package example;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String... args) throws Exception {
ObjectPool<ExpensiveObject> pool = new ObjectPool<ExpensiveObject>() {
@Override