func (la *layeredAccessor) SetPrimary(a Accessor) {
	la.lock.Lock()
	la.err = nil
	la.primary = a
	la.lock.Unlock()
}
SetPrimary changes the instances used by this UpdateAccessor, clearing any error.  Note that Get will
still return an error if a is nil or empty.
package service
import (
	"errors"
	"fmt"
	"math/rand"
	"sync"
)
var (
	errNoInstances     = errors.New("There are no instances available")
	errNoFailOvers     = errors.New("no failover instances available")
	errFailOversFailed = errors.New("failovers could not find an instance")
)
type Accessor interface {
	Get(key []byte) (string, error)
}
type AccessorFunc func([]byte) (string, error)
func (af AccessorFunc) Get(key []byte) (string, error) {
	return af(key)
}
type emptyAccessor struct{}
func (ea emptyAccessor) Get([]byte) (string, error) {
	return "", errNoInstances
}
func EmptyAccessor() Accessor {
	return emptyAccessor{}
}
type MapAccessor map[string]string
func (ma MapAccessor) Get(key []byte) (string, error) {
	if v, ok := ma[string(key)]; ok {
		return v, nil
	} else {
		return "", fmt.Errorf("No such key: %s", string(key))
	}
}
type UpdatableAccessor struct {
	lock sync.RWMutex
	err     error
	current Accessor
}
func (ua *UpdatableAccessor) Get(key []byte) (instance string, err error) {
	ua.lock.RLock()
	switch {
	case ua.err != nil:
		err = ua.err
	case ua.current != nil:
		instance, err = ua.current.Get(key)
	default:
		err = errNoInstances
	}
	ua.lock.RUnlock()
	return
}
func (ua *UpdatableAccessor) SetError(err error) {
	ua.lock.Lock()
	ua.err = err
	ua.current = nil
	ua.lock.Unlock()
}
func (ua *UpdatableAccessor) SetInstances(a Accessor) {
	ua.lock.Lock()
	ua.err = nil
	ua.current = a
	ua.lock.Unlock()
}
func (ua *UpdatableAccessor) Update(a Accessor, err error) {
	ua.lock.Lock()
	ua.err = err
	ua.current = a
	ua.lock.Unlock()
}
type RouteTraffic interface {
	Route(instance string) error
}
type emptyRouter struct {
}
func (r emptyRouter) Route(instance string) error {
	return nil
}
func DefaultTrafficRouter() RouteTraffic {
	return emptyRouter{}
}
type AccessorQueue interface {
	Order([]string) []string
}
type emptyQueue struct {
	r *rand.Rand
}
func DefaultOrder() AccessorQueue {
	return emptyQueue{}
}
func (r emptyQueue) Order(keys []string) []string {
	return keys
}
type AccessorValue struct {
	Accessor Accessor
	Err      error
}
type LayeredAccessor interface {
	Accessor
	SetError(err error)
	SetPrimary(a Accessor)
	UpdatePrimary(a Accessor, err error)
	SetFailOver(failover map[string]AccessorValue)
	UpdateFailOver(key string, a Accessor, err error)
}
type layeredAccessor struct {
	router        RouteTraffic
	accessorQueue AccessorQueue
	err      error
	primary  Accessor
	failover map[string]AccessorValue
	lock sync.RWMutex
}
func NewLayeredAccesor(router RouteTraffic, chooser AccessorQueue) LayeredAccessor {
	return &layeredAccessor{
		router:        router,
		accessorQueue: chooser,
		failover:      make(map[string]AccessorValue),
	}
}
func (la *layeredAccessor) SetError(err error) {
	la.lock.Lock()
	la.err = err
	la.primary = nil
	la.lock.Unlock()
}