-
Notifications
You must be signed in to change notification settings - Fork 18
/
cpi.go
180 lines (152 loc) · 4.84 KB
/
cpi.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package datacontext
import (
"context"
"fmt"
runtime2 "runtime"
"github.com/mandelsoft/logging"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/action/handlers"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/finalizer"
)
// NewContextBase creates a context base implementation supporting
// context attributes and the binding to a context.Context.
func NewContextBase(eff Context, typ string, key interface{}, parentAttrs Attributes, delegates Delegates) InternalContext {
updater, _ := eff.(Updater)
return newContextBase(eff, typ, key, parentAttrs, &updater,
ComposeDelegates(logging.NewWithBase(delegates.LoggingContext()), handlers.NewRegistry(nil, delegates.GetActions())),
)
}
// GCWrapper is the embeddable base type
// for a context wrapper handling garbage collection.
// It aölso handles the BindTo interface for a context.
type GCWrapper struct {
self Context
key interface{}
}
// setSelf is not public to enforce
// the usage of this GCWrapper type in context
// specific garbage collection wrappers.
// It is enforced by the
// finalizableContextWrapper interface.
func (w *GCWrapper) setSelf(self Context, key interface{}) {
w.self = self
w.key = key
}
func init() { // linter complains about unused method.
(&GCWrapper{}).setSelf(nil, nil)
}
// BindTo makes the Context reachable via the resulting context.Context.
// Go requires not to use a pointer receiver, here ??????
func (b GCWrapper) BindTo(ctx context.Context) context.Context {
return context.WithValue(ctx, b.key, b.self)
}
// finalizableContextWrapper is the interface for
// a context wrapper used to establish a garbage collectable
// runtime finalizer.
// It is a helper interface for Go generics to enforce a
// struct pointer.
type finalizableContextWrapper[C InternalContext, P any] interface {
InternalContext
SetContext(C)
setSelf(Context, interface{})
*P
}
// FinalizedContext wraps a context implementation C into a separate wrapper
// object of type *W and returns this wrapper.
// It should have the type
//
// struct {
// C
// }
//
// The wrapper is created and a runtime finalizer is
// defined for this object, which calls the Finalize Method on the
// context implementation.
func FinalizedContext[W Context, C InternalContext, P finalizableContextWrapper[C, W]](c C) P {
var v W
p := (P)(&v)
p.SetContext(c)
p.setSelf(p, c.GetKey()) // prepare for generic bind operation
runtime2.SetFinalizer(p, fi[W, C, P])
Debug(p, "create context", "id", c.GetId())
return p
}
func fi[W Context, C InternalContext, P finalizableContextWrapper[C, W]](c P) {
err := c.Cleanup()
c.GetRecorder().Record(c.GetId())
Debug(c, "cleanup context", "error", err)
}
type contextBase struct {
ctxtype string
id ContextIdentity
key interface{}
effective Context
attributes *_attributes
delegates
finalizer *finalizer.Finalizer
recorder *finalizer.RuntimeFinalizationRecoder
}
var _ Context = (*contextBase)(nil)
func newContextBase(eff Context, typ string, key interface{}, parentAttrs Attributes, updater *Updater, delegates Delegates) *contextBase {
recorder := &finalizer.RuntimeFinalizationRecoder{}
id := ContextIdentity(fmt.Sprintf("%s/%d", typ, contextrange.NextId()))
c := &contextBase{
ctxtype: typ,
id: id,
key: key,
effective: eff,
finalizer: &finalizer.Finalizer{},
attributes: newAttributes(eff, parentAttrs, updater),
delegates: delegates,
recorder: recorder,
}
return c
}
func (c *contextBase) BindTo(ctx context.Context) context.Context {
panic("should never be called")
}
func (c *contextBase) GetType() string {
return c.ctxtype
}
func (c *contextBase) GetId() ContextIdentity {
return c.id
}
func (c *contextBase) GetKey() interface{} {
return c.key
}
func (c *contextBase) AttributesContext() AttributesContext {
return c.effective.AttributesContext()
}
func (c *contextBase) GetAttributes() Attributes {
return c.attributes
}
func (c *contextBase) GetRecorder() *finalizer.RuntimeFinalizationRecoder {
return c.recorder
}
func (c *contextBase) Cleanup() error {
list := errors.ErrListf("cleanup %s", c.id)
list.Addf(nil, c.finalizer.Finalize(), "finalizers")
list.Add(c.attributes.Finalize())
return list.Result()
}
func (c *contextBase) Finalize() error {
return c.finalizer.Finalize()
}
func (c *contextBase) Finalizer() *finalizer.Finalizer {
return c.finalizer
}
// AssureUpdater is used to assure the existence of an updater in
// a root context if a config context is down the context hierarchy.
// This method SHOULD only be called by a config context.
func AssureUpdater(attrs AttributesContext, u Updater) {
c, ok := attrs.(*gcWrapper)
if !ok {
return
}
if c.updater == nil {
c.updater = u
}
}