Skip to content

Commit e7c857f

Browse files
Reasnoguxi.reasno
andauthored
refactor(ctxmeta): change synchronization (#249)
Change-Id: I6e301dd77e0cab1441205f11577af419504b5351 Co-authored-by: guxi.reasno <[email protected]>
1 parent 5d8b5d1 commit e7c857f

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

ctxmeta/ctxmeta.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"reflect"
13+
"sync"
1314

1415
"github.com/DoNewsCode/core/contract"
1516
)
@@ -46,7 +47,8 @@ var ErrNotFound = errors.New("key not found")
4647
// context, and set or get metadata. At the end of the request, all metadata
4748
// collected will be available from any point in the callstack.
4849
type Baggage[K comparable, V any] struct {
49-
ch chan []KeyVal[K, V]
50+
keyvals []KeyVal[K, V]
51+
mu sync.Mutex
5052
}
5153

5254
// Unmarshal get the value at given path, and store it into the target variable. Target must
@@ -81,10 +83,10 @@ func (b *Baggage[K, V]) Get(key K) (value V, err error) {
8183
return value, ErrNoBaggage
8284
}
8385

84-
s := <-b.ch
85-
defer func() { b.ch <- s }()
86+
b.mu.Lock()
87+
defer b.mu.Unlock()
8688

87-
for _, kv := range s {
89+
for _, kv := range b.keyvals {
8890
if kv.Key == key {
8991
return kv.Val, nil
9092
}
@@ -100,18 +102,19 @@ func (b *Baggage[K, V]) Set(key K, value V) (err error) {
100102
return ErrNoBaggage
101103
}
102104

103-
s := <-b.ch
104-
defer func() { b.ch <- s }()
105+
b.mu.Lock()
106+
defer b.mu.Unlock()
105107

108+
s := b.keyvals
106109
for i := range s {
107110
if s[i].Key == key {
108111
s[i].Val = value
109-
s = append(s[:i], append(s[i+1:], s[i])...)
112+
b.keyvals = append(s[:i], append(s[i+1:], s[i])...)
110113
return nil
111114
}
112115
}
113116

114-
s = append(s, KeyVal[K, V]{key, value})
117+
b.keyvals = append(s, KeyVal[K, V]{key, value})
115118

116119
return nil
117120
}
@@ -124,9 +127,10 @@ func (b *Baggage[K, V]) Update(key K, callback func(value V) V) (err error) {
124127
return ErrNoBaggage
125128
}
126129

127-
s := <-b.ch
128-
defer func() { b.ch <- s }()
130+
b.mu.Lock()
131+
defer b.mu.Unlock()
129132

133+
s := b.keyvals
130134
for i := range s {
131135
if s[i].Key == key {
132136
s[i].Val = callback(s[i].Val)
@@ -144,12 +148,14 @@ func (b *Baggage[K, V]) Delete(key K) (err error) {
144148
if b == nil {
145149
return ErrNoBaggage
146150
}
147-
s := <-b.ch
148-
defer func() { b.ch <- s }()
149151

152+
b.mu.Lock()
153+
defer b.mu.Unlock()
154+
155+
s := b.keyvals
150156
for i := range s {
151157
if s[i].Key == key {
152-
s = append(s[:i], s[i+1:]...)
158+
b.keyvals = append(s[:i], s[i+1:]...)
153159
return nil
154160
}
155161
}
@@ -162,11 +168,11 @@ func (b *Baggage[K, V]) Slice() []KeyVal[K, V] {
162168
if b == nil {
163169
return nil
164170
}
165-
s := <-b.ch
166-
defer func() { b.ch <- s }()
171+
b.mu.Lock()
172+
defer b.mu.Unlock()
167173

168-
r := make([]KeyVal[K, V], len(s))
169-
copy(r, s)
174+
r := make([]KeyVal[K, V], len(b.keyvals))
175+
copy(r, b.keyvals)
170176
return r
171177
}
172178

@@ -176,11 +182,11 @@ func (b *Baggage[K, V]) Map() map[K]V {
176182
return nil
177183
}
178184

179-
s := <-b.ch
180-
defer func() { b.ch <- s }()
185+
b.mu.Lock()
186+
defer b.mu.Unlock()
181187

182-
mp := make(map[K]V, len(s))
183-
for _, kv := range s {
188+
mp := make(map[K]V, len(b.keyvals))
189+
for _, kv := range b.keyvals {
184190
mp[kv.Key] = kv.Val
185191
}
186192
return mp
@@ -207,9 +213,7 @@ func New[K comparable, V any]() *MetadataSet[K, V] {
207213
// context for all further operations. The returned Baggage can be queried at any
208214
// point for metadata collected over the life of the context.
209215
func (m *MetadataSet[K, V]) Inject(ctx context.Context) (*Baggage[K, V], context.Context) {
210-
c := make(chan []KeyVal[K, V], 1)
211-
c <- make([]KeyVal[K, V], 0, 32)
212-
d := &Baggage[K, V]{ch: c}
216+
d := &Baggage[K, V]{}
213217
return d, context.WithValue(ctx, m.key, d)
214218
}
215219

0 commit comments

Comments
 (0)