Skip to content

Instantly share code, notes, and snippets.

@jacoelho
jacoelho / pprof-auth.go
Created September 29, 2017 09:10
pprof auto
package main
import (
"fmt"
"net/http"
"net/http/pprof"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
type Cache struct {
kv map[string]*bufio.Reader
http.RoundTripper
}
func (c *Cache) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Method != "GET" {
return c.RoundTrip(req)
}
type RoundTripper interface {
RoundTrip(*Request) (*Response, error)
}
// http.Client - comments removed
type Client struct {
Transport RoundTripper
CheckRedirect func(req *Request, via []*Request) error
Jar CookieJar
Timeout time.Duration
}
c := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
resp, err := http.Get("http://example.com/")
@jacoelho
jacoelho / naive-retry.go
Last active July 31, 2017 18:43
naive retry golang
// Retry - http client with retry support
type Retry struct {
http.RoundTripper
}
// Naive Retry - every 2 seconds
func (r *Retry) RoundTrip(req *http.Request) (*http.Response, error) {
for {
resp, err := r.RoundTripper.RoundTrip(req)
@jacoelho
jacoelho / retry.go
Created July 30, 2017 23:02
retry golang
type Retry struct {
http.RoundTripper
}
func (r *Retry) RoundTrip(req *http.Request) (*http.Response, error) {
for {
log.Printf("retry: doing request %s\n", req.URL)
resp, err := r.RoundTripper.RoundTrip(req)
if err == nil && resp.StatusCode < 500 {
@jacoelho
jacoelho / shaping.sh
Last active July 18, 2017 08:19
trafic shaping
# old mac
sudo ipfw pipe 1 config bw 300kbit/s;
sudo ipfw add pipe 1 dst-ip 0.0.0.0/0;
# to reset:
sudo ipfw flush
#
dnctl pipe 1 config bw 10Kbit/s delay 300
echo "dummynet out proto tcp from any to example.com pipe 1" |pfctl -f -
@jacoelho
jacoelho / nginx.mtail
Last active July 5, 2017 11:09
mtail example
counter http_requests_total by method,status
counter http_requests_time_seconds by bucket, method, status
#log_format main '$realip_remote_addr "$remote_addr" "environment" '
# '- $remote_user '
# '[$time_local] "$request_id" "$cookie_PHPSESSID" '
# '"$request" $status $bytes_sent "$http_referer" '
# '"$http_user_agent" "-" ($request_time)';
/(?P<local>\d+(\.\d+){3})\s+/ + # local ip