You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

registry.go 987 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package extension
  2. import (
  3. "sync"
  4. )
  5. import (
  6. "github.com/pkg/errors"
  7. )
  8. import (
  9. "github.com/transaction-wg/seata-golang/pkg/base/registry"
  10. )
  11. var (
  12. registriesMu sync.RWMutex
  13. registries = make(map[string]func() (registry.Registry, error))
  14. )
  15. // SetRegistry sets the registry extension with @name
  16. func SetRegistry(name string, v func() (registry.Registry, error)) {
  17. //写加锁,参考sql.go
  18. registriesMu.Lock()
  19. defer registriesMu.Unlock()
  20. if v == nil {
  21. panic("registry: Register v is nil")
  22. }
  23. if _, dup := registries[name]; dup {
  24. panic("registry: Register called twice for registry " + name)
  25. }
  26. registries[name] = v
  27. }
  28. // GetRegistry finds the registry extension with @name
  29. func GetRegistry(name string) (registry.Registry, error) {
  30. registriesMu.RLock()
  31. registry := registries[name]
  32. registriesMu.RUnlock()
  33. if registry == nil {
  34. return nil, errors.Errorf("registry for " + name + " is not existing, make sure you have import the package.")
  35. }
  36. return registry()
  37. }