Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update http2/tcp_buffer.go #58

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := v0.2.4
VERSION := v0.2.5

BIN_NAME = grpcr
CONTAINER = grpcr
Expand Down
4 changes: 2 additions & 2 deletions http2/http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (hc *Http2Conn) deal() {
buf := make([]byte, HeaderSize)
_, err = io.ReadFull(hc.Reader, buf)
if err != nil {
slog.Error("Http2Conn.deal, ReadFull:%v", err)
slog.Warn("Http2Conn.deal, ReadFull:%v", err)
break
}

Expand All @@ -170,7 +170,7 @@ func (hc *Http2Conn) deal() {
if fb.Length > 0 {
_, err = io.ReadFull(hc.Reader, buf)
if err != nil {
slog.Error("Http2Conn.deal, ReadFull:%v", err)
slog.Warn("Http2Conn.deal, ReadFull:%v", err)
break
}
}
Expand Down
28 changes: 28 additions & 0 deletions http2/tcp_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync/atomic"
)

const MaxWindowSize = 65536

type TCPBuffer struct {
//The number of bytes of data currently cached
size atomic.Int64
Expand Down Expand Up @@ -55,6 +57,14 @@ func (sb *TCPBuffer) AddTCP(tcpPkg *layers.TCP) {
slog.Debug("[start]SocketBuffer.addTCP, size:%v, actualCanReadSize:%v, expectedSeq:%v",
sb.size.Load(), sb.actualCanReadSize.Load(), sb.expectedSeq)

// Discard packets outside the sliding window
if !validPackage(sb.expectedSeq, MaxWindowSize, tcpPkg.Seq) {
slog.Debug("[end]SocketBuffer.addTCP-discard packets outside the sliding window, "+
"size:%v, actualCanReadSize:%v, expectedSeq:%v",
sb.size.Load(), sb.actualCanReadSize.Load(), sb.expectedSeq)
return
}

// duplicate package
if sb.List.Get(tcpPkg.Seq) != nil {
slog.Debug("[end]SocketBuffer.addTCP-duplicate package, size:%v, actualCanReadSize:%v, expectedSeq:%v",
Expand Down Expand Up @@ -91,3 +101,21 @@ func (sb *TCPBuffer) AddTCP(tcpPkg *layers.TCP) {
slog.Debug("[end]SocketBuffer.addTCP, size:%v, actualCanReadSize:%v, expectedSeq:%v",
sb.size.Load(), sb.actualCanReadSize.Load(), sb.expectedSeq)
}

func validPackage(expectedSeq uint32, maxWindowSize uint32, pkgSeq uint32) bool {
rightBorder := (expectedSeq + maxWindowSize) % math.MaxUint32
// case 1: sequence wrap around
if rightBorder < expectedSeq {
if (pkgSeq <= rightBorder) || (pkgSeq >= expectedSeq) {
return true
} else {
return false
}
} else { // case 2
if pkgSeq >= expectedSeq && pkgSeq <= expectedSeq+maxWindowSize {
return true
} else {
return false
}
}
}
22 changes: 22 additions & 0 deletions http2/tcp_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,25 @@ func TestSocketBufferWrapAround3(t *testing.T) {
// assert for nil (good for errors)
assert.Nil(t, err)
}

func TestValidPackage(t *testing.T) {
testCases := []struct {
expectedSeq uint32
maxWindowSize uint32
pkgSeq uint32
expected bool
}{
// case 1
{4294966995, 10000, 4294967095, true},
{4294966995, 10000, 9500, true},
{4294966995, 10000, 4294946995, false},
// case 2
{10000, 10000, 10200, true},
{10000, 10000, 3000, false},
{10000, 10000, 20300, false},
}
for _, testCase := range testCases {
actual := validPackage(testCase.expectedSeq, testCase.maxWindowSize, testCase.pkgSeq)
assert.Equal(t, testCase.expected, actual, "Not consistent with expectations")
}
}
Loading