Skip to content

Commit

Permalink
golang filter: update cached length (#29701)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander authored Sep 20, 2023
1 parent fd72723 commit 202ae13
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
3 changes: 0 additions & 3 deletions contrib/golang/common/go/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ type HeaderMap interface {

// RangeWithCopy calls f sequentially for each key and value copied from the map.
RangeWithCopy(f func(key, value string) bool)

// ByteSize return size of HeaderMap
ByteSize() uint64
}

type RequestHeaderMap interface {
Expand Down
25 changes: 13 additions & 12 deletions contrib/golang/filters/http/source/go/pkg/http/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ type headerMapImpl struct {
mutex sync.Mutex
}

// ByteSize return size of HeaderMap
func (h *headerMapImpl) ByteSize() uint64 {
return h.headerBytes
}

type requestOrResponseHeaderMapImpl struct {
headerMapImpl
}
Expand Down Expand Up @@ -337,17 +332,19 @@ type httpBuffer struct {
var _ api.BufferInstance = (*httpBuffer)(nil)

func (b *httpBuffer) Write(p []byte) (n int, err error) {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, string(p), api.AppendBuffer)
return len(p), nil
return b.WriteString(string(p))
}

func (b *httpBuffer) WriteString(s string) (n int, err error) {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, s, api.AppendBuffer)
return len(s), nil
n = len(s)
b.length += uint64(n)
return n, nil
}

func (b *httpBuffer) WriteByte(p byte) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, string(p), api.AppendBuffer)
b.length++
return nil
}

Expand Down Expand Up @@ -409,31 +406,35 @@ func (b *httpBuffer) String() string {
}

func (b *httpBuffer) Append(data []byte) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, string(data), api.AppendBuffer)
return nil
_, err := b.Write(data)
return err
}

func (b *httpBuffer) Prepend(data []byte) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, string(data), api.PrependBuffer)
b.length += uint64(len(data))
return nil
}

func (b *httpBuffer) AppendString(s string) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, s, api.AppendBuffer)
return nil
_, err := b.WriteString(s)
return err
}

func (b *httpBuffer) PrependString(s string) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, s, api.PrependBuffer)
b.length += uint64(len(s))
return nil
}

func (b *httpBuffer) Set(data []byte) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, string(data), api.SetBuffer)
b.length = uint64(len(data))
return nil
}

func (b *httpBuffer) SetString(s string) error {
cAPI.HttpSetBufferHelper(unsafe.Pointer(b.request.req), b.envoyBufferInstance, s, api.SetBuffer)
b.length = uint64(len(s))
return nil
}
2 changes: 2 additions & 0 deletions contrib/golang/filters/http/test/golang_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,8 @@ TEST_P(GolangIntegrationTest, BufferReset) { testBufferApi("Reset"); }

TEST_P(GolangIntegrationTest, BufferResetAfterDrain) { testBufferApi("ResetAfterDrain"); }

TEST_P(GolangIntegrationTest, BufferLen) { testBufferApi("Len"); }

TEST_P(GolangIntegrationTest, Property) {
initializePropertyConfig(PROPERTY, genSoPath(PROPERTY), PROPERTY);
initialize();
Expand Down
51 changes: 51 additions & 0 deletions contrib/golang/filters/http/test/test_data/buffer/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"reflect"

"github.com/envoyproxy/envoy/contrib/golang/common/go/api"
)
Expand Down Expand Up @@ -66,6 +67,54 @@ func testResetAfterDrain(b api.BufferInstance) {
}
}

func panicIfNotEqual(a, b any) {
if !reflect.DeepEqual(a, b) {
panic(fmt.Sprintf("expected %v, got %v", a, b))
}
}

func panicIfLenMismatch(b api.BufferInstance, size int) {
panicIfNotEqual(size, b.Len())
panicIfNotEqual(len(b.Bytes()), b.Len())
}

func testLen(b api.BufferInstance) {
b.Set([]byte("12"))
panicIfLenMismatch(b, 2)
b.SetString("123")
panicIfLenMismatch(b, 3)

b.Write([]byte("45"))
panicIfLenMismatch(b, 5)
b.WriteString("67")
panicIfLenMismatch(b, 7)
b.WriteByte('8')
panicIfLenMismatch(b, 8)
b.WriteUint16(90)
panicIfLenMismatch(b, 10)
b.WriteUint32(12)
panicIfLenMismatch(b, 12)
b.WriteUint64(12)
panicIfLenMismatch(b, 14)

b.Drain(2)
panicIfLenMismatch(b, 12)
b.Write([]byte("45"))
panicIfLenMismatch(b, 14)

b.Reset()
panicIfLenMismatch(b, 0)

b.Append([]byte("12"))
panicIfLenMismatch(b, 2)
b.Prepend([]byte("0"))
panicIfLenMismatch(b, 3)
b.AppendString("345")
panicIfLenMismatch(b, 6)
b.PrependString("00")
panicIfLenMismatch(b, 8)
}

func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
if endStream {
return api.Continue
Expand All @@ -80,6 +129,8 @@ func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.Statu
testResetAfterDrain(buffer)
case "Drain":
testDrain(buffer)
case "Len":
testLen(buffer)
default:
panic(fmt.Sprintf("unknown case %s", query))
}
Expand Down

0 comments on commit 202ae13

Please sign in to comment.