service_map.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2023 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "context"
  9. "fmt"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/events"
  12. "github.com/syncthing/syncthing/lib/svcutil"
  13. "github.com/thejerf/suture/v4"
  14. )
  15. var errSvcNotFound = fmt.Errorf("service not found")
  16. // A serviceMap is a utility map of arbitrary keys to a suture.Service of
  17. // some kind, where adding and removing services ensures they are properly
  18. // started and stopped on the given Supervisor. The serviceMap is itself a
  19. // suture.Service and should be added to a Supervisor.
  20. // Not safe for concurrent use.
  21. type serviceMap[K comparable, S suture.Service] struct {
  22. services map[K]S
  23. tokens map[K]suture.ServiceToken
  24. supervisor *suture.Supervisor
  25. eventLogger events.Logger
  26. }
  27. func newServiceMap[K comparable, S suture.Service](eventLogger events.Logger) *serviceMap[K, S] {
  28. m := &serviceMap[K, S]{
  29. services: make(map[K]S),
  30. tokens: make(map[K]suture.ServiceToken),
  31. eventLogger: eventLogger,
  32. }
  33. m.supervisor = suture.New(m.String(), svcutil.SpecWithDebugLogger(l))
  34. return m
  35. }
  36. // Add adds a service to the map, starting it on the supervisor. If there is
  37. // already a service at the given key, it is removed first.
  38. func (s *serviceMap[K, S]) Add(k K, v S) {
  39. if tok, ok := s.tokens[k]; ok {
  40. // There is already a service at this key, remove it first.
  41. s.supervisor.Remove(tok)
  42. s.eventLogger.Log(events.Failure, fmt.Sprintf("%s replaced service at key %v", s, k))
  43. }
  44. s.services[k] = v
  45. s.tokens[k] = s.supervisor.Add(v)
  46. }
  47. // Get returns the service at the given key, or the empty value and false if
  48. // there is no service at that key.
  49. func (s *serviceMap[K, S]) Get(k K) (v S, ok bool) {
  50. v, ok = s.services[k]
  51. return
  52. }
  53. // Remove removes the service at the given key, stopping it on the supervisor.
  54. // If there is no service at the given key, nothing happens. The return value
  55. // indicates whether a service was removed.
  56. func (s *serviceMap[K, S]) Remove(k K) (found bool) {
  57. if tok, ok := s.tokens[k]; ok {
  58. found = true
  59. s.supervisor.Remove(tok)
  60. }
  61. delete(s.services, k)
  62. delete(s.tokens, k)
  63. return
  64. }
  65. // RemoveAndWait removes the service at the given key, stopping it on the
  66. // supervisor. Returns errSvcNotFound if there is no service at the given
  67. // key, otherwise the return value from the supervisor's RemoveAndWait.
  68. func (s *serviceMap[K, S]) RemoveAndWait(k K, timeout time.Duration) error {
  69. return <-s.RemoveAndWaitChan(k, timeout)
  70. }
  71. // RemoveAndWaitChan removes the service at the given key, stopping it on
  72. // the supervisor. The returned channel will produce precisely one error
  73. // value: either the return value from RemoveAndWait (possibly nil), or
  74. // errSvcNotFound if the service was not found.
  75. func (s *serviceMap[K, S]) RemoveAndWaitChan(k K, timeout time.Duration) <-chan error {
  76. ret := make(chan error, 1)
  77. if tok, ok := s.tokens[k]; ok {
  78. go func() {
  79. ret <- s.supervisor.RemoveAndWait(tok, timeout)
  80. }()
  81. } else {
  82. ret <- errSvcNotFound
  83. }
  84. delete(s.services, k)
  85. delete(s.tokens, k)
  86. return ret
  87. }
  88. // Each calls the given function for each service in the map. An error from
  89. // fn will stop the iteration and be returned as-is.
  90. func (s *serviceMap[K, S]) Each(fn func(K, S) error) error {
  91. for key, svc := range s.services {
  92. if err := fn(key, svc); err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }
  98. // Suture implementation
  99. func (s *serviceMap[K, S]) Serve(ctx context.Context) error {
  100. return s.supervisor.Serve(ctx)
  101. }
  102. func (s *serviceMap[K, S]) String() string {
  103. var kv K
  104. var sv S
  105. return fmt.Sprintf("serviceMap[%T, %T]@%p", kv, sv, s)
  106. }