package dps import ( "errors" "git.sxidc.com/service-supports/dps-sdk/client" "git.sxidc.com/service-supports/dps-sdk/dpsv1" "sync" ) var clientMapMutex sync.Mutex var clientMap map[string]client.Client func NewClient(address string, clientVersion string, databaseID string) (client.Client, error) { clientMapMutex.Lock() defer clientMapMutex.Unlock() if clientMap == nil { clientMap = make(map[string]client.Client) } key := clientVersion + "::" + databaseID c, ok := clientMap[key] if ok { return c, nil } switch clientVersion { case "v1": innerClient, err := dpsv1.NewClient(address, databaseID) if err != nil { return nil, err } c = innerClient default: return nil, errors.New("不支持的版本") } clientMap[clientVersion] = c return c, nil } func DestroyClient(clientVersion string, databaseID string) error { clientMapMutex.Lock() defer clientMapMutex.Unlock() key := clientVersion + "::" + databaseID c, ok := clientMap[key] if !ok { return nil } switch clientVersion { case "v1": err := dpsv1.DestroyClient(c.(*dpsv1.Client)) if err != nil { return err } default: break } delete(clientMap, key) return nil }