decred.org/dcrwallet/v3@v3.1.0/internal/loader/vsp.go (about) 1 package loader 2 3 import ( 4 "sync" 5 6 "decred.org/dcrwallet/v3/errors" 7 "decred.org/dcrwallet/v3/internal/vsp" 8 ) 9 10 var vspClients = struct { 11 mu sync.Mutex 12 clients map[string]*vsp.Client 13 }{ 14 clients: make(map[string]*vsp.Client), 15 } 16 17 // VSP loads or creates a package-global instance of the VSP client for a host. 18 // This allows clients to be created and reused across various subsystems. 19 func VSP(cfg vsp.Config) (*vsp.Client, error) { 20 key := cfg.URL 21 vspClients.mu.Lock() 22 defer vspClients.mu.Unlock() 23 client, ok := vspClients.clients[key] 24 if ok { 25 return client, nil 26 } 27 client, err := vsp.New(cfg) 28 if err != nil { 29 return nil, err 30 } 31 vspClients.clients[key] = client 32 return client, nil 33 } 34 35 // LookupVSP returns a previously-configured VSP client, if one has been created 36 // and registered with the VSP function. Otherwise, a NotExist error is 37 // returned. 38 func LookupVSP(host string) (*vsp.Client, error) { 39 vspClients.mu.Lock() 40 defer vspClients.mu.Unlock() 41 client, ok := vspClients.clients[host] 42 if !ok { 43 err := errors.Errorf("VSP client for %q not found", host) 44 return nil, errors.E(errors.NotExist, err) 45 } 46 return client, nil 47 } 48 49 // AllVSPs returns the list of all currently registered VSPs. 50 func AllVSPs() map[string]*vsp.Client { 51 // Create a copy to avoid callers mutating the list. 52 vspClients.mu.Lock() 53 defer vspClients.mu.Unlock() 54 res := make(map[string]*vsp.Client, len(vspClients.clients)) 55 for host, client := range vspClients.clients { 56 res[host] = client 57 } 58 return res 59 }