Skip to content

Instantly share code, notes, and snippets.

@niamster
niamster / gh-notifications-cache-requests.swift
Created January 7, 2022 01:29
Test request caching when requesting GH notifications
import Foundation
func fetch(cachePolicy: URLRequest.CachePolicy) {
let apiToken = ProcessInfo.processInfo.environment["GITHUB_API_TOKEN"]!
var request = URLRequest(url: URL(string: "https://api.github.com/notifications?all=false&participating=true")!, cachePolicy: cachePolicy)
request.addValue("token \(apiToken)", forHTTPHeaderField: "Authorization")
request.addValue("no-cache", forHTTPHeaderField: "Cache-Control")
URLSession.shared.dataTask(with: request) { _, response, _ in
guard let httpResponse = response as? HTTPURLResponse else { return }
let headers = httpResponse.allHeaderFields
@niamster
niamster / backoff.py
Last active October 4, 2020 22:21
Exponential backoff with hysteresis on recovery
import math
import time
import threading
import random
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
@niamster
niamster / btreemap-range-test.rs
Created May 3, 2017 00:34
BTreeMap range test for woodpecker
use std::collections::BTreeMap;
use std::collections::Bound::Included;
static mut INC: i32 = 0;
fn inc() -> i32 {
unsafe {
INC += 1;
INC
}
@niamster
niamster / lua.go
Last active February 19, 2016 20:30
calling lua scripts from goroutines
package main
import (
"github.com/yuin/gopher-lua"
"fmt"
)
const debug = false
const count = 10
@niamster
niamster / rmtx.c
Created November 5, 2015 09:00
RAII mutex pseudo in c
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
typedef struct {
int locked;
} mutex_t;
static void mutex_init(mutex_t *mtx) {
mtx->locked = 0;
@niamster
niamster / hmac-bench.c
Last active November 4, 2015 10:27
hmac-bench.c
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <assert.h>
#include <getopt.h>
@niamster
niamster / method-missing-leak.rb
Created April 6, 2015 20:38
Method missing leak
#!/usr/bin/env ruby
require 'weakref'
$LEAK = true
class Hidden
def initialize(cls)
@cls = cls
end
@niamster
niamster / block-leak.rb
Created April 6, 2015 20:13
Undead blocks
#!/usr/bin/env ruby
require 'weakref'
class Proxy < BasicObject
attr_reader :cls
def initialize(cls)
@cls = cls
end
#!/usr/bin/env ruby
require 'weakref'
foo = 'toto'
weak = WeakRef.new foo
f = Fiber.new do
p foo
# if fiber yield is not called the fiber context is never released
@niamster
niamster / futures-args-gc-463.rb
Last active August 29, 2015 14:17
test for celluloid issue #463
#!/usr/bin/env ruby
require 'celluloid'
require 'weakref'
class Bar
attr_reader :bar
def initialize(bar)
@bar = bar