-
Notifications
You must be signed in to change notification settings - Fork 9
/
registrar.go
126 lines (100 loc) · 3.2 KB
/
registrar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resources
import (
"maps"
"slices"
"sync"
"github.com/samber/lo"
"github.com/nitrictech/nitric/core/pkg/logger"
)
type ResourceRegister[R any] struct {
RequestingServices []string
Resource *R
}
type ResourceRegistrar[R any] struct {
lock sync.RWMutex
resources map[ResourceName]*ResourceRegister[R]
}
func (r *ResourceRegistrar[R]) isAlreadyRegistered(name string, requestingService string) bool {
_, exists := r.resources[name]
if exists {
duplicate := slices.Contains(r.resources[name].RequestingServices, requestingService)
return duplicate
}
return false
}
func (r *ResourceRegistrar[R]) Register(name string, requestingService string, resource *R) error {
r.lock.Lock()
defer r.lock.Unlock()
_, exists := r.resources[name]
if exists {
if r.isAlreadyRegistered(name, requestingService) {
logger.Debugf("resource %s registered multiple times for service %s", name, requestingService)
return nil
}
// already registered, by another service, add this service to the list
r.resources[name].RequestingServices = append(r.resources[name].RequestingServices, requestingService)
return nil
}
// new resource, register it
r.resources[name] = &ResourceRegister[R]{
RequestingServices: []string{requestingService},
Resource: resource,
}
return nil
}
func (r *ResourceRegistrar[R]) Get(resourceName string) *R {
r.lock.RLock()
defer r.lock.RUnlock()
registration, ok := r.resources[resourceName]
if !ok {
return nil
}
return registration.Resource
}
func (r *ResourceRegistrar[R]) GetAll() map[ResourceName]*ResourceRegister[R] {
r.lock.RLock()
defer r.lock.RUnlock()
return maps.Clone(r.resources)
}
func (r *ResourceRegistrar[R]) GetRequestingServices(name string) []string {
r.lock.RLock()
defer r.lock.RUnlock()
registration, ok := r.resources[name]
if !ok {
return []string{}
}
return registration.RequestingServices
}
// ClearRequestingService - Remove a requesting service from all resources, if it was the only requestor for a resource, the resource is also removed
func (r *ResourceRegistrar[R]) ClearRequestingService(requestingService string) {
r.lock.Lock()
defer r.lock.Unlock()
for name, registration := range r.resources {
registration.RequestingServices = lo.Filter(registration.RequestingServices, func(item string, index int) bool {
return item != requestingService
})
if len(registration.RequestingServices) == 0 {
delete(r.resources, name)
}
}
}
func NewResourceRegistrar[R any]() *ResourceRegistrar[R] {
return &ResourceRegistrar[R]{
resources: make(map[string]*ResourceRegister[R], 0),
}
}