instance.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package dps
  2. import (
  3. "git.sxidc.com/service-supports/dps-sdk/client"
  4. "git.sxidc.com/service-supports/fslog"
  5. "sync"
  6. )
  7. var dpsClientMutex sync.Mutex
  8. var dpsClient client.Client
  9. func InitInstance(address string, clientVersion string, databaseID string) error {
  10. dpsClientMutex.Lock()
  11. defer dpsClientMutex.Unlock()
  12. if dpsClient == nil {
  13. return nil
  14. }
  15. c, err := NewClient(address, clientVersion, databaseID)
  16. if err != nil {
  17. return err
  18. }
  19. dpsClient = c
  20. return nil
  21. }
  22. func DestroyInstance(clientVersion string, databaseID string) error {
  23. dpsClientMutex.Lock()
  24. defer dpsClientMutex.Unlock()
  25. err := DestroyClient(clientVersion, databaseID)
  26. if err != nil {
  27. return err
  28. }
  29. dpsClient = nil
  30. return nil
  31. }
  32. func AutoMigrate(items []client.AutoMigrateItem) error {
  33. dpsClientMutex.Lock()
  34. defer dpsClientMutex.Unlock()
  35. if dpsClient == nil {
  36. return nil
  37. }
  38. return dpsClient.AutoMigrate(&client.AutoMigrateRequest{Items: items})
  39. }
  40. func Transaction(txFunc client.TransactionFunc) error {
  41. dpsClientMutex.Lock()
  42. defer dpsClientMutex.Unlock()
  43. if dpsClient == nil {
  44. return nil
  45. }
  46. return dpsClient.Transaction(txFunc)
  47. }
  48. func EndTransaction(tx client.Transaction) {
  49. dpsClientMutex.Lock()
  50. defer dpsClientMutex.Unlock()
  51. if dpsClient == nil {
  52. return
  53. }
  54. err := tx.End()
  55. if err != nil {
  56. fslog.Error("!!!!!!!!! END TRANSACTION" + err.Error() + "!!!!!!!!!")
  57. return
  58. }
  59. }