Skip to content

Instantly share code, notes, and snippets.

View medusar's full-sized avatar
🦊
making money

JasonWong medusar

🦊
making money
  • Beijing,China
View GitHub Profile
@caoxudong
caoxudong / MethodHandleTest.java
Last active March 25, 2021 02:44
关于MethodHandle性能的简单测试
package test;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
public class MethodHandleTest {
public static void main(String[] args) throws Throwable {

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.

@leonid-ed
leonid-ed / udp_to_local.c
Last active July 10, 2023 15:36
Examples of using raw sockets (c, linux, raw socket)
/*
An example of using raw sockets.
You can capture packets by tcpdump:
tcpdump -X -s0 -i lo -p udp
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
@rednaxelafx
rednaxelafx / HashMapStackOverflow.java
Last active July 2, 2018 06:42
JDK8: StackOverflowError of HashMap due to self reference
import java.util.*;
public class HashMapStackOverflow {
public static void main(String[] args) throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("self", map);
System.out.println(map.hashCode());
}
}
@caoxudong
caoxudong / TokenGenerator.java
Created March 9, 2015 10:36
tomcat中生成JSESSIONID的代码
package com.meiliao.ops.utils.security;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
@josiahcarlson
josiahcarlson / rate_limit2.py
Last active April 17, 2022 09:22
Regular and sliding window rate limiting to accompany two blog posts.
'''
rate_limit2.py
Copyright 2014, Josiah Carlson - josiah.carlson@gmail.com
Released under the MIT license
This module intends to show how to perform standard and sliding-window rate
limits as a companion to the two articles posted on Binpress entitled
"Introduction to rate limiting with Redis", parts 1 and 2:
@raphw
raphw / FieldBenchmark.java
Last active March 4, 2024 00:14
Java MethodHandle and reflection benchmark
package benchmark;
import org.openjdk.jmh.annotations.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;
@tomsoete
tomsoete / pom.xml
Created August 2, 2012 20:39 — forked from gordonad/pom.xml
Spring Best Practices Maven Pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gordondickens.sample</groupId>
<artifactId>sample-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>