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

splithttp Read() using blocking mode #3473

Merged
merged 4 commits into from
Jun 24, 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
28 changes: 14 additions & 14 deletions transport/internet/splithttp/upload_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ func (h *UploadQueue) Close() error {
}

func (h *UploadQueue) Read(b []byte) (int, error) {
if h.closed && len(h.heap) == 0 && len(h.pushedPackets) == 0 {
if h.closed {
return 0, io.EOF
}

needMorePackets := false
if len(h.heap) == 0 {
packet, more := <-h.pushedPackets
if !more {
return 0, io.EOF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if EOF is handled here, I think line 50 can be removed? and then h.closed can be removed.

because h.pushedPackets is always closed when h.closed == true

}
heap.Push(&h.heap, packet)
}

if len(h.heap) > 0 {
for len(h.heap) > 0 {
packet := heap.Pop(&h.heap).(Packet)
n := 0

Expand Down Expand Up @@ -81,18 +87,12 @@ func (h *UploadQueue) Read(b []byte) (int, error) {
return 0, newError("packet queue is too large")
}
heap.Push(&h.heap, packet)
needMorePackets = true
}
} else {
needMorePackets = true
}

if needMorePackets {
packet, more := <-h.pushedPackets
if !more {
return 0, io.EOF
packet2, more := <-h.pushedPackets
if !more {
return 0, io.EOF
}
heap.Push(&h.heap, packet2)
}
heap.Push(&h.heap, packet)
}

return 0, nil
Expand Down
22 changes: 22 additions & 0 deletions transport/internet/splithttp/upload_queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package splithttp_test

import (
"testing"

"github.com/xtls/xray-core/common"
. "github.com/xtls/xray-core/transport/internet/splithttp"
)

func Test_regression_readzero(t *testing.T) {
q := NewUploadQueue(10)
q.Push(Packet{
Payload: []byte("x"),
Seq: 0,
})
buf := make([]byte, 20)
n, err := q.Read(buf)
common.Must(err)
if n != 1 {
t.Error("n=", n)
}
}