Skip to content

Instantly share code, notes, and snippets.

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

M blinkinglight

🏠
Working from home
View GitHub Profile
@blinkinglight
blinkinglight / MidpointLruCache.py
Created August 1, 2020 20:34 — forked from midom/MidpointLruCache.py
LRU with midpoint insertion (LRU2Q) cache decorator for Python
class MidpointLruCache:
def __init__(self, size, oldpercentage):
self.size = size
self.oldsize = size * oldpercentage / 100
self.youngsize = size * (100 - oldpercentage) / 100
self.youngitems = collections.OrderedDict()
self.olditems = collections.OrderedDict()
def __call__(self, func):
// only 1 sensor to test
const int led = 8;
const int echo = 3;
const int trig = 2;
const long wait = 1000; //cooldown time for the sensor
unsigned long last = 0;
@blinkinglight
blinkinglight / I2C_LCD_driver.py
Created April 5, 2020 12:59 — forked from IdrisCytron/I2C_LCD_driver.py
Raspberry Pi I2C LCD driver.
# -*- coding: utf-8 -*-
# i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
I2CBUS = 1
# LCD Address
ADDRESS = 0x27
import smbus
from time import sleep
@blinkinglight
blinkinglight / serve.go
Created February 23, 2020 14:38 — forked from pheuter/serve.go
go http streaming
package main
import (
"fmt"
"io"
"io/ioutil"
"strings"
"exec"
"http"
)
@blinkinglight
blinkinglight / main.go
Created January 26, 2020 11:46 — forked from slok/main.go
handler & handlerfunc golang pattern example
// Package main is an example of how handler pattern works in golang.
//
// At first you will need some sort of start point. To do this we create
// ExampleHandler interface, this interface has a trigger method that will
// execute the chain, in his case is RunExample, it accepts a writer, and a
// custom input object, as you see there is an out and an in parameter.
//
// We could work like this you can create multiple ExampleHandlers in a helper
// function and call them on in another. But this is not very handy and it smells
//
@blinkinglight
blinkinglight / logger_middleware.go
Created June 23, 2019 08:38 — forked from blixt/logger_middleware.go
Logger middleware for Go HTTP servers which logs every request with response status code in the Apache format.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
@blinkinglight
blinkinglight / 1-howto-nats-websocket-proxy.txt
Last active May 22, 2019 21:06
nats.io websocket / nats.io streaming websocket / nats.io websocket proxy
on nats server run:
- start gnatsd server then:
-- go run natswsproxy.go -token test -bind :8888
on client run:
- go run client.go -to ws://ws.domain.tld:8888/mq?token=test -nats-user test -nats-pass test # user and pass from nats.server config
@blinkinglight
blinkinglight / api.go
Last active January 27, 2019 10:54
golang in-memory queue // restreaming queue
package main
func main() {
t := htc.New()
ht := http.Client{}
r, _ := ht.Get("http://500mbfile.blob")
go func() {
io.Copy(t, r.Body)
@blinkinglight
blinkinglight / golang-linked-list.go
Created July 11, 2018 04:31 — forked from maksadbek/golang-linked-list.go
golang linked list implementation
package main
import "fmt"
type Node struct {
prev *Node
next *Node
key interface{}
}
@blinkinglight
blinkinglight / backpressure.go
Created April 14, 2018 09:12 — forked from marianogappa/backpressure.go
Example backpressure implementation in Go
/*
This snippet is an example of backpressure implementation in Go.
It doesn't run in Go Playground, because it starts an HTTP Server.
The example starts an HTTP server and sends multiple requests to it. The server starts denying
requests by replying an "X" (i.e. a 502) when its buffered channel reaches capacity.
This is not the same as rate-limiting; you might be interested in https://github.com/juju/ratelimit
or https://godoc.org/golang.org/x/time/rate.