func IsEmpty(requestsToContexts RequestContextMapper) (bool, error) {
	if requestsToContexts, ok := requestsToContexts.(*requestContextMap); ok {
		return len(requestsToContexts.contexts) == 0, nil
	}
	return true, errors.New("Unknown RequestContextMapper implementation")
}
IsEmpty returns true if there are no contexts registered, or an error if it could not be determined. Intended for use by tests.
package api
import (
	"errors"
	"net/http"
	"sync"
)
type RequestContextMapper interface {
	Get(req *http.Request) (Context, bool)
	Update(req *http.Request, context Context) error
}
type requestContextMap struct {
	contexts map[*http.Request]Context
	lock     sync.Mutex
}
func NewRequestContextMapper() RequestContextMapper {
	return &requestContextMap{
		contexts: make(map[*http.Request]Context),
	}
}
func (c *requestContextMap) Get(req *http.Request) (Context, bool) {
	c.lock.Lock()
	defer c.lock.Unlock()
	context, ok := c.contexts[req]
	return context, ok
}
func (c *requestContextMap) Update(req *http.Request, context Context) error {
	c.lock.Lock()
	defer c.lock.Unlock()
	if _, ok := c.contexts[req]; !ok {
		return errors.New("No context associated")
	}
	c.contexts[req] = context
	return nil
}
func (c *requestContextMap) init(req *http.Request, context Context) bool {
	c.lock.Lock()
	defer c.lock.Unlock()
	if _, exists := c.contexts[req]; exists {
		return false
	}
	c.contexts[req] = context
	return true
}
func (c *requestContextMap) remove(req *http.Request) {
	c.lock.Lock()
	defer c.lock.Unlock()
	delete(c.contexts, req)
}
func NewRequestContextFilter(mapper RequestContextMapper, handler http.Handler) (http.Handler, error) {
	if mapper, ok := mapper.(*requestContextMap); ok {
		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			if mapper.init(req, NewContext()) {
				defer mapper.remove(req)
			}
			handler.ServeHTTP(w, req)
		}), nil
	} else {
		return handler, errors.New("Unknown RequestContextMapper implementation.")
	}
}