Skip to content

Instantly share code, notes, and snippets.

View nstogner's full-sized avatar

Nick Stogner nstogner

View GitHub Profile
@nstogner
nstogner / init.vim
Last active October 2, 2019 13:15
nvim
" ~/.config/nvim/init.vim
call plug#begin('~/.local/share/nvim/plugged')
Plug 'NLKNguyen/papercolor-theme' " Color scheme
Plug 'fatih/vim-go' " Go development
Plug 'scrooloose/nerdtree' " Sidebar nav
Plug 'ctrlpvim/ctrlp.vim' " Fuzzy finder nav
Plug 'easymotion/vim-easymotion' " In-file nav
" Autocomplete
@nstogner
nstogner / status_logware_usage.go
Last active June 5, 2017 22:17
Go: HTTP Status Logging Middleware Usage
func main() {
http.ListenAndServe(":8080",
logware(
http.HandlerFunc(handle),
),
)
}
func handle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
@nstogner
nstogner / status_logware.go
Last active June 23, 2023 20:41
Go: HTTP Status Logging Middleware
type statusRecorder struct {
http.ResponseWriter
status int
}
func (rec *statusRecorder) WriteHeader(code int) {
rec.status = code
rec.ResponseWriter.WriteHeader(code)
}
@nstogner
nstogner / retry_jitter.go
Last active March 19, 2023 03:29
Go: Simple retry function with jitter
func init() {
rand.Seed(time.Now().UnixNano())
}
func retry(attempts int, sleep time.Duration, f func() error) error {
if err := f(); err != nil {
if s, ok := err.(stop); ok {
// Return the original error for later checking
return s.error
}
@nstogner
nstogner / retry_http.go
Last active March 19, 2023 03:29
Go: Simple retry function (HTTP request)
// DeleteThing attempts to delete a thing. It will try a maximum of three times.
func DeleteThing(id string) error {
// Build the request
req, err := http.NewRequest(
"DELETE",
fmt.Sprintf("https://unreliable-api/things/%s", id),
nil,
)
if err != nil {
return fmt.Errorf("unable to make request: %s", err)
@nstogner
nstogner / retry_usage.go
Last active May 24, 2017 00:39
Go: Simple retry function (usage)
// 1. Prints: A
retry(3, time.Second, func() error {
fmt.Print("A")
return nil
})
// 2. Prints: BB
var i int
retry(3, time.Second, func() error {
i++
@nstogner
nstogner / retry.go
Last active January 17, 2023 06:48
Go: Simple retry function
func retry(attempts int, sleep time.Duration, fn func() error) error {
if err := fn(); err != nil {
if s, ok := err.(stop); ok {
// Return the original error for later checking
return s.error
}
if attempts--; attempts > 0 {
time.Sleep(sleep)
return retry(attempts, 2*sleep, fn)