Skip to content

Instantly share code, notes, and snippets.

package main
import (
"encoding/csv"
"flag"
"log"
"math/rand"
"os"
"strconv"
"time"
type FailingStorage struct{}
func (f *FailingStorage) Add(k, v string) {
throw exception.New("now we fail")
}
var s = NewSafeStorage(&FailingStorage{})
func handleAdd(w http.ResponseWriter, r *http.Request) {
key := r.Host
value := r.Method
type FailingStorage struct{}
func (f *FailingStorage) Add(k, v string) (err error) {
return errors.New("now we fail")
}
var s = NewSafeStorage(&FailingStorage{})
type FailingStorage struct{}
func (f *FailingStorage) Add(k, v string) (err error) {
return errors.New("now we fail")
}
var s = NewSafeStorage(&FailingStorage{})
func handleAdd(w http.ResponseWriter, r *http.Request) {
key := r.Host
value := r.Method
@heppu
heppu / original.go
Last active January 26, 2019 11:16
type storage struct{}
func (s *storage) Add(k, v string) {}
var s = NewSafeStorage(&storage{})
func handleAdd(w http.ResponseWriter, r *http.Request) {
key := r.Host
value := r.Method
s.Add(key, value)
}
type Storage interface {
Add(key, val string) error
}
func (s *SafeStorage) Add(key, val string) error {
s.storageMu.Lock()
defer s.storageMu.Unlock()
return s.storage.Add(key, val)
}
type Storage interface {
Add(key, val string)
}
type SafeStorage struct {
storageMu *sync.Mutex
storage Storage
}
func NewSafeStorage(storage Storage) *SafeStorage {
@heppu
heppu / main.go
Last active June 28, 2017 10:08
Go http client file descriptor leak
/*
File that demostrates file descriptor leak
in case the client forgets to close response body
*/
package main
import (
"fmt"
"io/ioutil"
@heppu
heppu / ARCH_INSTALL.MD
Last active August 22, 2024 16:25
Installing Arch with GPT, dm-crypt, LUKS, LVM and systemd-boot

Create bootable USB

dd bs=4M if=/path/to/archlinux.iso of=/dev/sdx status=progress oflag=sync

Boot from USB and set prepare system

loadkeys <your-keymap>
@heppu
heppu / limitter.go
Last active December 9, 2016 08:21
Limit parallel goroutines with buffered channel.
package main
import (
"log"
"sync"
"time"
)
const Limit = 5