Skip to content

Instantly share code, notes, and snippets.

@steveash
steveash / PreshingRandomSequence.java
Last active February 17, 2023 23:05
Pseudo-random generator that doesn't repeat integers
import java.util.Random;
/**
* Generates a sequence of pseudo-random numbers such that they never repeat. Can handle sequence sizes up to
* length Int.MAX_VALUE. Will throw exceptions if you ask for more than that; maps the entire [0, Integer.MAX_VALUE]
* range onto itself but in a random order
* <link>http://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/</link>
*/
public class PreshingRandomSequence {
public static final int MAX_INT_PRIME = 2147483587;
object Example {
implicit val es = Executors.newCachedThreadPool
val metrics = new MetricsCollectorImpl(MetricsCollectorConfiguration("example", "localhost", 8125))
val ironConfig: IronMqConfig = ???
val iron = IronMqMessaging(ironConfig, QueueName("example-queue"))
def main(args: Array[String]) {
val consumer = StreamMq(iron, metrics).consumer
@BHSDuncan
BHSDuncan / XMLDSigGenerator.java
Last active April 7, 2016 02:04
Class for generating an XML Digital Signature according to the W3C spec (https://www.w3.org/TR/xmldsig-core/). Note that the only part of this that requires JAXB is the output, which can then be assigned to the appropriate location in a SOAP envelope/message.
package com.whatever.service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
@cvan
cvan / HOWTO.md
Last active May 16, 2025 06:07
How to serve a custom HTTPS domain on GitHub Pages with CloudFlare: *FREE*, secure and performant by default

Instructions

CloudFlare is an awesome reverse cache proxy and CDN that provides DNS, free HTTPS (TLS) support, best-in-class performance settings (gzip, SDCH, HTTP/2, sane Cache-Control and E-Tag headers, etc.), minification, etc.

  1. Make sure you have registered a domain name.
  2. Sign up for CloudFlare and create an account for your domain.
  3. In your domain registrar's admin panel, point the nameservers to CloudFlare's (refer to this awesome list of links for instructions for various registrars).
  4. From the CloudFlare settings for that domain, enable HTTPS/SSL and set up a Page Rule to force HTTPS redirects. (If you want to get fancy, you can also enable automatic minification for text-based assets [HTML/CSS/JS/SVG/etc.], which is a pretty cool feature if you don't want already have a build step for minification.)
  5. If you
@cecilemuller
cecilemuller / letsencrypt_2020.md
Last active January 9, 2025 12:22
How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SSL rating)

How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SLL rating)


Virtual hosts

Let's say you want to host domains first.com and second.com.

Create folders for their files:

@efrecon
efrecon / run.tpl
Last active November 15, 2025 09:12
`docker inspect` template to regenerate the `docker run` command that created a container
docker run \
--name {{printf "%q" .Name}} \
{{- with .HostConfig}}
{{- if .Privileged}}
--privileged \
{{- end}}
{{- if .AutoRemove}}
--rm \
{{- end}}
{{- if .Runtime}}
import time
class Bucket(object):
def __init__(self, max_amount, refill_time, refill_amount):
self.max_amount = max_amount
self.refill_time = refill_time
self.refill_amount = refill_amount
self.reset()
def _refill_count(self):

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.

@elnur
elnur / postgresql.conf
Created April 18, 2017 06:19
PG Auto Explain Slow Queries
shared_preload_libraries = 'auto_explain'
auto_explain.log_min_duration = 100
auto_explain.log_analyze = true
auto_explain.log_verbose = true
auto_explain.log_nested_statements = true