123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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
- }
|