Debian Codegraph @

lockedcallbacks/lockedcallbacks.go

raw symbols SCIP
1 // Copyright 2023 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package lockedcallbacks provides locking per-key.
16 package lockedcallbacks
17
18 import (
19 "errors"
20 "io"
21 "sync"
22 "text/template"
23 )
24
25 // ErrStateKeyNotFound indicates the requested key was not found in the map of states
26 var ErrStateKeyNotFound error = errors.New("Unlock requested for an unknown state")
27
28 // StatesMap wraps a map of Mstate. Each key locks separately.
29 type StatesMap struct {
30 maplock sync.Mutex // locks the States map
31 States map[any]*Mstate // contains states for each template
32 }
33
34 // Mstate is used to represent entries of StatesMap
35 // each entry (Mstate) holds a callback. A mutex is protecting changes to the callback
36 // The key and statesmap attributes are used to provide self-cleaning of the entries once a state
37 // does not need to be maintained.
38 type Mstate struct {
39 statesmap *StatesMap // points back to StatesMap, so we can synchronize removing this entry when cnt==0
40 statelock sync.Mutex // is an entry-specific lock
41 cnt int // references to the state
42 key any // indicates the key in the parent states map
43 Callback func(string) string // points to the callback to protect
44 AllowFlagsCallback func(string) string // points to the allowflags callback to protect
45 }
46
47 // New returns an initialized StatesMap.
48 func New() *StatesMap {
49 return &StatesMap{States: make(map[any]*Mstate)}
50 }
51
52 // Lock acquires a state corresponding to this key.
53 func (m *StatesMap) lock(key any) *Mstate {
54
55 // read or create entry for this key atomically
56 m.maplock.Lock()
57 ent, ok := m.States[key]
58 if !ok {
59 ent = &Mstate{statesmap: m, key: key}
60 m.States[key] = ent
61 }
62 ent.cnt++
63 m.maplock.Unlock()
64
65 ent.statelock.Lock()
66
67 return ent
68 }
69
70 // Unlock releases the lock for this entry.
71 func (ms *Mstate) unlock() error {
72 m := ms.statesmap
73
74 m.maplock.Lock()
75 defer m.maplock.Unlock()
76 ent, ok := m.States[ms.key]
77 if !ok {
78 return ErrStateKeyNotFound
79 }
80 ent.cnt--
81 if ent.cnt < 1 {
82 ent.statesmap = nil
83 delete(m.States, ms.key)
84 }
85 ent.statelock.Unlock()
86 return nil
87 }
88
89 // BuildTextTemplateRemediationFunc creates a virtual method calling the template's callback.
90 func (m *StatesMap) BuildTextTemplateRemediationFunc(safeTmplUUID string, wrapperFunc func(any, func(string) string) any) func(any) any {
91 remediationFunc := func(data any) any {
92 m.maplock.Lock()
93 defer m.maplock.Unlock()
94 tmplState, ok := m.States[safeTmplUUID]
95 if !ok {
96 return nil
97 }
98 cb := tmplState.Callback
99 return wrapperFunc(data, cb)
100 }
101 return remediationFunc
102 }
103
104 // BuildAllowFlagsCallbackFunc creates a virtual method calling the template's allowFlags callback.
105 func (m *StatesMap) BuildAllowFlagsCallbackFunc(safeTmplUUID string, wrapperFunc func(any, func(string) string) any) func(data any) any {
106 allowFlagsCallbackFunc := func(data any) any {
107 m.maplock.Lock()
108 defer m.maplock.Unlock()
109 tmplState, ok := m.States[safeTmplUUID]
110 if !ok {
111 return nil
112 }
113 allowFlagsCb := tmplState.AllowFlagsCallback
114 return wrapperFunc(data, allowFlagsCb)
115 }
116 return allowFlagsCallbackFunc
117 }
118
119 // SetAndExecuteWithCallback hides implementation details of the StatesMap by taking a template and
120 // a callback to set the proper callback and execute the template.
121 func (m *StatesMap) SetAndExecuteWithCallback(tmpl *template.Template, safeTmplUUID string, cb func(string) string, result io.Writer, data any) error {
122 tmplState := m.lock(safeTmplUUID)
123 tmplState.Callback = cb
124
125 err := tmpl.Execute(result, data)
126 if err != nil {
127 return err
128 }
129 return tmplState.unlock()
130 }
131
132 // SetAndExecuteWithShCallback hides implementation details of the StatesMap by taking a template
133 // and both callbacks to set the proper callback and execute the template.
134 func (m *StatesMap) SetAndExecuteWithShCallback(tmpl *template.Template, safeTmplUUID string, cb func(string) string, allowFlagsCb func(string) string, result io.Writer, data any) error {
135 tmplState := m.lock(safeTmplUUID)
136 tmplState.Callback = cb
137 tmplState.AllowFlagsCallback = allowFlagsCb
138
139 err := tmpl.Execute(result, data)
140 if err != nil {
141 return err
142 }
143 return tmplState.unlock()
144 }