Skip to content

Commit b8dd7bf

Browse files
committed
fix(protocol): Peek() returns non-nil if key exists
1 parent aeb2ae5 commit b8dd7bf

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

pkg/network/netpoll/transport_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestTransport(t *testing.T) {
124124
t.Run("TestExceptionCase", func(t *testing.T) {
125125
assert.Panic(t, func() { // listen err
126126
transporter := NewTransporter(&config.Options{
127-
Network: "unknow",
127+
Network: "unknown",
128128
})
129129
transporter.ListenAndServe(func(ctx context.Context, conn interface{}) error {
130130
return nil

pkg/protocol/args.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ const (
5454
ArgsHasValue = false
5555
)
5656

57-
var nilByteSlice = []byte{}
58-
5957
type argsScanner struct {
6058
b []byte
6159
}
@@ -167,9 +165,15 @@ func allocArg(h []argsKV) ([]argsKV, *argsKV) {
167165
n := len(h)
168166
if cap(h) > n {
169167
h = h[:n+1]
170-
} else {
171-
h = append(h, argsKV{})
168+
kv := &h[n]
169+
if kv.value == nil {
170+
// bytes in value would be reused, and it's not always nil
171+
// only set to empty when it's nil
172+
kv.value = []byte{}
173+
}
174+
return h, kv
172175
}
176+
h = append(h, argsKV{value: []byte{}})
173177
return h, &h[n]
174178
}
175179

@@ -228,10 +232,7 @@ func peekArgBytes(h []argsKV, k []byte) []byte {
228232
for i, n := 0, len(h); i < n; i++ {
229233
kv := &h[i]
230234
if bytes.Equal(kv.key, k) {
231-
if kv.value != nil {
232-
return kv.value
233-
}
234-
return nilByteSlice
235+
return kv.value
235236
}
236237
}
237238
return nil

pkg/protocol/args_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,37 @@ func TestArgsVisitAll(t *testing.T) {
130130
assert.DeepEqual(t, []string{"cloudwego", "hertz", "hello", "world"}, s)
131131
}
132132

133+
func TestArgsCopyTo(t *testing.T) {
134+
var a Args
135+
a.Add("cloudwego", "")
136+
a.Add("hello", "world")
137+
138+
var b Args
139+
a.CopyTo(&b)
140+
assert.Assert(t, b.Len() == 2)
141+
assert.Assert(t, a.Peek("cloudwego") != nil && len(a.Peek("cloudwego")) == 0)
142+
assert.Assert(t, string(a.Peek("hello")) == "world")
143+
assert.Assert(t, a.Peek("key-not-exists") == nil)
144+
}
145+
146+
func TestArgsPeek(t *testing.T) {
147+
var a Args
148+
a.Add("cloudwego", "")
149+
a.Add("hello", "world")
150+
151+
assert.Assert(t, a.Peek("cloudwego") != nil && len(a.Peek("cloudwego")) == 0)
152+
assert.Assert(t, string(a.Peek("hello")) == "world")
153+
assert.Assert(t, a.Peek("key-not-exists") == nil)
154+
155+
// reset and reuse
156+
a.Reset()
157+
a.Add("cloudwego", "")
158+
a.Add("hello", "world")
159+
assert.Assert(t, a.Peek("cloudwego") != nil && len(a.Peek("cloudwego")) == 0)
160+
assert.Assert(t, string(a.Peek("hello")) == "world")
161+
assert.Assert(t, a.Peek("key-not-exists") == nil)
162+
}
163+
133164
func TestArgsPeekMulti(t *testing.T) {
134165
var a Args
135166
a.Add("cloudwego", "hertz")
@@ -141,7 +172,7 @@ func TestArgsPeekMulti(t *testing.T) {
141172
expectedVV := [][]byte{
142173
[]byte("hertz"),
143174
[]byte("kitex"),
144-
[]byte(nil),
175+
[]byte{},
145176
}
146177
assert.DeepEqual(t, expectedVV, vv)
147178

pkg/protocol/trailer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (t *Trailer) SetTrailers(trailers []byte) (err error) {
163163
}
164164

165165
utils.NormalizeHeaderKey(trailerKey, t.disableNormalizing)
166-
err = t.addArgBytes(trailerKey, nilByteSlice, argsNoValue)
166+
err = t.addArgBytes(trailerKey, nil, argsNoValue)
167167
}
168168
return
169169
}

0 commit comments

Comments
 (0)