// Proxy will return an instance of a proxy based on the handle // If one already exists with the given handle, it will be used. // Otherwise a new one will be created. func (pf *ProxyFactory) Proxy(handle string) *proxy.Proxy { var ret *proxy.Proxy pf.lock.Lock() if _, ok := pf.proxies[handle]; !ok { proxy := proxy.New(handle) pf.availability[handle] = proxy.Available() pf.proxies[handle] = proxy } ret = pf.proxies[handle] pf.lock.Unlock() return ret }
// New is the constructor function for ProxyFactory func New() *ProxyFactory { pf := &ProxyFactory{ proxies: make(map[string]*proxy.Proxy), availability: make(map[string]bool), lock: &sync.Mutex{}, } go func() { for _ = range time.Tick(30 * time.Second) { pf.lock.Lock() for key, proxy := range pf.proxies { pf.availability[key] = proxy.Available() log.WithFields(log.Fields{ "proxy": key, "available": pf.availability[key], }).Debug("Proxy availability check") } pf.lock.Unlock() } }() return pf }