Development

Simple Golang Retry Function

May 29, 2017
Development
golang

Adding retry policies in your software is an easy way to increase resiliency. This is especially useful when making HTTP requests or doing anything else that has to reach out across the network. If at first you don’t succeed, try, try again. In go code, that translates to: func retry(attempts int, sleep time.Duration, fn func() error) error { if err := fn(); err != nil { if s, ok := err. ...

Go: Wrapping http.ResponseWriter with Middleware

June 6, 2017
Development
golang

Using middleware provides a clean way to reduce code duplication when handling HTTP requests in Go. By utilizing the standard handler signature, func(w http.ResponseWriter, r *http.Request) we can write functions that are easily dropped into any Go web service. One of the most common uses of middleware is to provide request/response logging. In order to log responses we will need to capture the status code that was written by a nested handler. ...