Skip to content

Instantly share code, notes, and snippets.

Andrew Meredith kendru

View GitHub Profile
@kendru
kendru / actix_dispatch_test.rs
Last active Oct 12, 2018
Test dispatching command to an Actix actor system
View actix_dispatch_test.rs
extern crate actix;
extern crate actix_web;
extern crate futures;
use std::thread;
use std::io::{self, Write};
use actix::prelude::*;
use actix_web::{Error as ActixWebError, HttpMessage};
use actix_web::client;
use futures::future::{self, Future};
@kendru
kendru / db.js
Created Aug 30, 2018
Simple database that packs records into contiguous memory
View db.js
const POOL_SIZE = 4 * 1024;
let mySchema = ['int32', 'char(32)', 'int32'];
let bufferPool = new ArrayBuffer(POOL_SIZE);
let dir = {
tbl: {
people: { offset: 0, size: 0 }
}
};
@kendru
kendru / Bucket.js
Created Feb 23, 2018
Simple token bucket implementation
View Bucket.js
class Bucket {
constructor(size, per) {
this.per = per;
this.size = size;
this.lastFilled = Math.floor(Date.now() / 1000);
this.tokens = size;
}
take() {
@kendru
kendru / async-work-queue.js
Created Feb 9, 2018
Asynchronous work queue
View async-work-queue.js
const promise_ = Symbol('promise');
class Deferred {
constructor() {
this[promise_] = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
@kendru
kendru / mysql-sandbox.sh
Last active Dec 27, 2018
Set up a throw-away mysql server in docker and a client to connect to it
View mysql-sandbox.sh
#!/bin/bash
cleanup_db() {
docker stop mysql_sandbox &>/dev/null
docker rm mysql_sandbox &>/dev/null
docker network inspect mysql_sandbox > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
docker network rm mysql_sandbox
fi
@kendru
kendru / findNode.js
Last active Oct 31, 2017
Find a node matching a predicate in a generic JS tree
View findNode.js
function findNode(node, pred) {
if (pred(node)) {
return node;
}
if (typeof node == 'object' && node.constructor == Object) {
for (var prop in node) {
if (node.hasOwnProperty(prop)) {
let foundNode = findNode(node[prop], pred);
@kendru
kendru / golang-to-node-authenticated-encryption.js
Last active Sep 20, 2017
Decrypting AES-256-GCM encoded in Go from Node
View golang-to-node-authenticated-encryption.js
// Usage: go run main.go | node decode.js
/*
// main.go:
package main
import (
"crypto/aes"
"crypto/cipher"
@kendru
kendru / trie.js
Created Sep 5, 2017
Javascript Trie for efficient prefix search
View trie.js
class Node {
constructor(char, parent = null, weight = null) {
this.char = char;
this.parent = parent;
this.weight = weight;
this.children = {};
}
get isTerminal() {
return this.weight !== null;
@kendru
kendru / handle-sighup.js
Created Oct 10, 2016
Example of using SUGHUP with Node to reload config
View handle-sighup.js
// Example /tmp/srv.conf.json
// {"name":"bill"}
var fs = require('fs');
var config = {
name: ''
};
function loadConfig() {
You can’t perform that action at this time.