Skip to content

Instantly share code, notes, and snippets.

View deckarep's full-sized avatar
🎯
Focusing

Ralph Caraveo deckarep

🎯
Focusing
View GitHub Profile
@deckarep
deckarep / twisted_shared_threadpool.py
Created March 28, 2016 02:12
Demonstrates how Twisted's Enterprise ADBAPI adapter can be patched to share a global threadpool.
# Excerpt from twisted.enterprise.adbapi.py
# Demonstrates a patch of the __init__ constructor to share threadpools for Twisted
# Confirmed that this actually works
# TODO: benchmark various threadpool sizes
def __init__(self, dbapiName, *connargs, **connkw):
"""Create a new ConnectionPool.
Any positional or keyword arguments other than those documented here
are passed to the DB-API object when connecting. Use these arguments to
#!/bin/sh
# Welcome to the thoughtbot laptop script!
# Be prepared to turn your laptop (or desktop, no haters here)
# into an awesome development machine.
fancy_echo() {
local fmt="$1"; shift
# shellcheck disable=SC2059
#! /bin/sh
black='\033[0;30m'
white='\033[0;37m'
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
#!/bin/sh
# Alot of these configs have been taken from the various places
# on the web, most from here
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
# Set the colours you can use
black='\033[0;30m'
white='\033[0;37m'
red='\033[0;31m'
@deckarep
deckarep / Go pre-commit
Created September 7, 2016 22:32 — forked from mboersma/Go pre-commit
Go project pre-commit hook
#!/bin/sh
# Save this file as ".git/hooks/pre-commit" in your
# git repository and set it to executable.
#
# To use the "go vet" command:
# $ go get -v code.google.com/p/go.tools/cmd/vet
# To use the "golint" command:
# $ go get -v github.com/golang/lint/golint
@deckarep
deckarep / goroutine-channel-pattern.go
Last active December 13, 2016 20:46
A Goroutine safe pattern using channels that abstracts away the usage of channels.
/*
A Goroutine safe pattern using channels that abstracts away the channels
This is a concept of creating a goroutine safe object that uses channels under the covers to communicate
with the internal map[string]string structure. I know that typically this kind of solution may done
with mutexes but the excercise was in using channels on purpose although they are heavier.
Note a couple of points:
- When using channels, you can still build a public-facing api that nicely abstracts them away, therefore
@deckarep
deckarep / no_shared.go
Last active December 20, 2016 23:36
Shared Nothing Concurrency
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
@deckarep
deckarep / communicate_shared.go
Last active December 21, 2016 04:03
Don't communicate by sharing memory; instead, blah blah blah...
package main
import (
"fmt"
)
var doublerChannel = make(chan *item, 0)
var adderChannel = make(chan *item, 0)
var completeChannel = make(chan *item, 0)
@deckarep
deckarep / channels_broken.go
Last active December 21, 2016 04:23
Breaking Go's mantra
func adder(amount int) {
for item := range adderChannel {
item.number += amount
doublerChannel <- item
fmt.Println(item.number) // <-- uh oh!
}
}
@deckarep
deckarep / unsafe_before_mutex.go
Last active December 22, 2016 20:59
Unsafe without the use of locks
package main
import (
"fmt"
"math/rand"
"time"
)
const totalEnemies = 1