gsvc_registry.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gsvc
  7. import (
  8. "context"
  9. "github.com/gogf/gf/v2/errors/gcode"
  10. "github.com/gogf/gf/v2/errors/gerror"
  11. )
  12. // Register registers `service` to default registry..
  13. func Register(ctx context.Context, service Service) (Service, error) {
  14. if defaultRegistry == nil {
  15. return nil, gerror.NewCodef(gcode.CodeNotImplemented, `no Registry is registered`)
  16. }
  17. ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
  18. defer cancel()
  19. return defaultRegistry.Register(ctx, service)
  20. }
  21. // Deregister removes `service` from default registry.
  22. func Deregister(ctx context.Context, service Service) error {
  23. if defaultRegistry == nil {
  24. return gerror.NewCodef(gcode.CodeNotImplemented, `no Registry is registered`)
  25. }
  26. ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
  27. defer cancel()
  28. return defaultRegistry.Deregister(ctx, service)
  29. }