Skip to content

Instantly share code, notes, and snippets.

@tidwall
tidwall / ints2bools.go
Last active March 26, 2019 12:44
@ints2bools gjson modifier
package main
import "github.com/tidwall/gjson"
func init() {
// define the @ints2bools modifier once.
gjson.AddModifier("ints2bools", func(json, _ string) string {
res := gjson.Parse(json)
switch res.Type {
case gjson.Number:
@tidwall
tidwall / main.go
Created March 13, 2019 17:46
Go 1.12 network slowdown on Darwin
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"sync"
"sync/atomic"
@tidwall
tidwall / tile38-location-hist.lua
Created July 27, 2018 23:18
Tile38 save short history of the last 10 locations for a given point
-- add the point
tile38.call("SET", ARGV[1], ARGV[2], "POINT", ARGV[3], ARGV[4])
-- get the previous history
local oldhist = tile38.call("GET", ARGV[1] .. ":history", ARGV[2])
if oldhist == nil then
oldhist = ""
end
-- parse the last 9 lines
@tidwall
tidwall / go-cover.sh
Created May 27, 2018 19:06
Go cover helper with html output
# add this to your ~/.bash_profile or whatnot
go(){
local r
if [[ $@ == "test -cover" ]]; then
r="$(command go test -coverprofile=/tmp/coverage.out)"
if [ "$r" != "" ]; then
printf "%s\n" "$r"
fi
if [[ "$r" == *"100.0%"* ]]; then
@tidwall
tidwall / build.sh
Last active May 26, 2018 20:04
Pass custom var strings to gcc
gcc -DgitSHA="$(git rev-parse HEAD)" main.c
@tidwall
tidwall / evio_context.go
Created May 8, 2018 18:25
evio_context.go
type myConn struct {
}
func main(){
var events Events
events.Opened = func(id int, info evio.Info) (out []byte, opts evio.Options, ctx interface{}, action evio.Action){
// create a new connection object that is specific to the new connection
// and assign to the context interface. this will be passed through other events.
conn := &myConn{}
ctx = conn
return
@tidwall
tidwall / evio.go
Created April 27, 2018 00:19
Multi-threaded Evio
package main
import (
"log"
"runtime"
"github.com/tidwall/evio"
)
func main() {
@tidwall
tidwall / EVIO_REUSEPORT.md
Created March 18, 2018 18:11
Evio with SO_REUSEPORT

Evio with SO_REUSEPORT

Using the reuseport branch of evio, you can now provide an SO_REUSEPORT address options from the Serve function.

evio.Serve(events, "tcp://0.0.0.0:5000?reuseport=true")

This allows for multiple process/threads/goroutines to share the same address+port.

@tidwall
tidwall / tile38.geojson
Last active March 9, 2018 22:45
tile38.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tidwall
tidwall / bboxbounds.go
Created January 11, 2018 20:30
Calculate the bounds of geo circle
package bboxbounds
import "math"
func BBoxBounds(lat, lon, meters float64) (latMin, lonMin, latMax, lonMax float64) {
// see http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#Latitude
lat = lat * (math.Pi / 180)
lon = lon * (math.Pi / 180)
r := meters / 6371e3 // angular radius