Skip to content

Instantly share code, notes, and snippets.

@cep21
cep21 / request.go
Created August 28, 2016 23:13
ClientTrace example
if trace != nil && trace.WroteHeaders != nil {
trace.WroteHeaders()
}
@cep21
cep21 / client_trace_log.go
Created August 27, 2016 02:18
client trace as logging instead
type ClientTrace struct {
Log(state string)
}
type Request struct {
Method string
URL *url.URL
// ...
// Trace callbacks are executed if they are set
Trace ClientTrace
}
type ClientTrace struct {
// Called before a connection is established. The returned context is used
// to finish processing the request.
GetConn func(ctx context.Context, hostPort string) context.Context
}
type ClientTrace struct {
// Called before a connection is established. If it returns a
// non nil object, that object is used instead for the Connection.
GetConn func(hostPort string) *net.Conn
}
@cep21
cep21 / client_trace.go
Created August 26, 2016 22:40
ClientTrace object
type ClientTrace struct {
// GetConn is called before a connection is created or
// retrieved from an idle pool. The hostPort is the
// "host:port" of the target or proxy. GetConn is called even
// if there's already an idle cached connection available.
GetConn func(hostPort string)
// GotConn is called after a successful connection is
// obtained. There is no hook for failure to obtain a
// connection; instead, use the error from
@cep21
cep21 / httptrace_example.go
Last active July 28, 2023 15:23
Example of http trace in Go 1.7
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"os"
)
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
@cep21
cep21 / ctx_middleware_chain.go
Last active July 25, 2022 10:17
Example of using context.Value() as a middleware chain
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
@cep21
cep21 / context_in_struct.go
Last active August 17, 2017 17:49
Example of context with message passing
package main
import "fmt"
import "golang.org/x/net/context"
// A message processes parameter and returns the result on responseChan.
// ctx is places in a struct, but this is ok to do.
type message struct {
responseChan chan<- int
parameter string