dps.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package dps
  2. import (
  3. "errors"
  4. "git.sxidc.com/service-supports/dps-sdk/client"
  5. "git.sxidc.com/service-supports/dps-sdk/dpsv1"
  6. "sync"
  7. )
  8. var clientMapMutex sync.Mutex
  9. var clientMap map[string]client.Client
  10. func NewClient(address string, clientVersion string, databaseID string) (client.Client, error) {
  11. clientMapMutex.Lock()
  12. defer clientMapMutex.Unlock()
  13. if clientMap == nil {
  14. clientMap = make(map[string]client.Client)
  15. }
  16. key := clientVersion + "::" + databaseID
  17. c, ok := clientMap[key]
  18. if ok {
  19. return c, nil
  20. }
  21. switch clientVersion {
  22. case "v1":
  23. innerClient, err := dpsv1.NewClient(address, databaseID)
  24. if err != nil {
  25. return nil, err
  26. }
  27. c = innerClient
  28. default:
  29. return nil, errors.New("不支持的版本")
  30. }
  31. clientMap[clientVersion] = c
  32. return c, nil
  33. }
  34. func DestroyClient(clientVersion string, databaseID string) error {
  35. clientMapMutex.Lock()
  36. defer clientMapMutex.Unlock()
  37. key := clientVersion + "::" + databaseID
  38. c, ok := clientMap[key]
  39. if !ok {
  40. return nil
  41. }
  42. switch clientVersion {
  43. case "v1":
  44. err := dpsv1.DestroyClient(c.(*dpsv1.Client))
  45. if err != nil {
  46. return err
  47. }
  48. default:
  49. break
  50. }
  51. delete(clientMap, key)
  52. return nil
  53. }