Skip to content

Commit e1f402c

Browse files
committed
Init project
0 parents  commit e1f402c

File tree

434 files changed

+76511
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

434 files changed

+76511
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
debug
2+
debug.test
3+
main
4+
mockgen_tmp.go
5+
*.qtr
6+
*.qlog
7+
*.txt
8+
race.[0-9]*
9+
10+
fuzzing/*/*.zip
11+
fuzzing/*/coverprofile
12+
fuzzing/*/crashers
13+
fuzzing/*/sonarprofile
14+
fuzzing/*/suppressions
15+
fuzzing/*/corpus/
16+
17+
gomock_reflect_*/

.golangci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
run:
2+
skip-files:
3+
- internal/qtls/structs_equal_test.go
4+
5+
linters-settings:
6+
depguard:
7+
type: blacklist
8+
packages:
9+
- github.com/marten-seemann/qtls
10+
packages-with-error-message:
11+
- github.com/marten-seemann/qtls: "importing qtls only allowed in internal/qtls"
12+
misspell:
13+
ignore-words:
14+
- ect
15+
16+
linters:
17+
disable-all: true
18+
enable:
19+
- asciicheck
20+
- deadcode
21+
- depguard
22+
- exhaustive
23+
- exportloopref
24+
- goimports
25+
- gofmt # redundant, since gofmt *should* be a no-op after gofumpt
26+
- gofumpt
27+
- gosimple
28+
- ineffassign
29+
- misspell
30+
- prealloc
31+
- staticcheck
32+
- stylecheck
33+
- structcheck
34+
- unconvert
35+
- unparam
36+
- unused
37+
- varcheck
38+
- vet
39+
40+
issues:
41+
exclude-rules:
42+
- path: internal/qtls
43+
linters:
44+
- depguard

buffer_pool.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package quic
2+
3+
import (
4+
"sync"
5+
6+
"github.com/imannamdari/quic-go/internal/protocol"
7+
)
8+
9+
type packetBuffer struct {
10+
Data []byte
11+
12+
// refCount counts how many packets Data is used in.
13+
// It doesn't support concurrent use.
14+
// It is > 1 when used for coalesced packet.
15+
refCount int
16+
}
17+
18+
// Split increases the refCount.
19+
// It must be called when a packet buffer is used for more than one packet,
20+
// e.g. when splitting coalesced packets.
21+
func (b *packetBuffer) Split() {
22+
b.refCount++
23+
}
24+
25+
// Decrement decrements the reference counter.
26+
// It doesn't put the buffer back into the pool.
27+
func (b *packetBuffer) Decrement() {
28+
b.refCount--
29+
if b.refCount < 0 {
30+
panic("negative packetBuffer refCount")
31+
}
32+
}
33+
34+
// MaybeRelease puts the packet buffer back into the pool,
35+
// if the reference counter already reached 0.
36+
func (b *packetBuffer) MaybeRelease() {
37+
// only put the packetBuffer back if it's not used any more
38+
if b.refCount == 0 {
39+
b.putBack()
40+
}
41+
}
42+
43+
// Release puts back the packet buffer into the pool.
44+
// It should be called when processing is definitely finished.
45+
func (b *packetBuffer) Release() {
46+
b.Decrement()
47+
if b.refCount != 0 {
48+
panic("packetBuffer refCount not zero")
49+
}
50+
b.putBack()
51+
}
52+
53+
// Len returns the length of Data
54+
func (b *packetBuffer) Len() protocol.ByteCount {
55+
return protocol.ByteCount(len(b.Data))
56+
}
57+
58+
func (b *packetBuffer) putBack() {
59+
if cap(b.Data) != int(protocol.MaxPacketBufferSize) {
60+
panic("putPacketBuffer called with packet of wrong size!")
61+
}
62+
bufferPool.Put(b)
63+
}
64+
65+
var bufferPool sync.Pool
66+
67+
func getPacketBuffer() *packetBuffer {
68+
buf := bufferPool.Get().(*packetBuffer)
69+
buf.refCount = 1
70+
buf.Data = buf.Data[:0]
71+
return buf
72+
}
73+
74+
func init() {
75+
bufferPool.New = func() interface{} {
76+
return &packetBuffer{
77+
Data: make([]byte, 0, protocol.MaxPacketBufferSize),
78+
}
79+
}
80+
}

buffer_pool_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package quic
2+
3+
import (
4+
"github.com/imannamdari/quic-go/internal/protocol"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("Buffer Pool", func() {
11+
It("returns buffers of cap", func() {
12+
buf := getPacketBuffer()
13+
Expect(buf.Data).To(HaveCap(int(protocol.MaxPacketBufferSize)))
14+
})
15+
16+
It("releases buffers", func() {
17+
buf := getPacketBuffer()
18+
buf.Release()
19+
})
20+
21+
It("gets the length", func() {
22+
buf := getPacketBuffer()
23+
buf.Data = append(buf.Data, []byte("foobar")...)
24+
Expect(buf.Len()).To(BeEquivalentTo(6))
25+
})
26+
27+
It("panics if wrong-sized buffers are passed", func() {
28+
buf := getPacketBuffer()
29+
buf.Data = make([]byte, 10)
30+
Expect(func() { buf.Release() }).To(Panic())
31+
})
32+
33+
It("panics if it is released twice", func() {
34+
buf := getPacketBuffer()
35+
buf.Release()
36+
Expect(func() { buf.Release() }).To(Panic())
37+
})
38+
39+
It("panics if it is decremented too many times", func() {
40+
buf := getPacketBuffer()
41+
buf.Decrement()
42+
Expect(func() { buf.Decrement() }).To(Panic())
43+
})
44+
45+
It("waits until all parts have been released", func() {
46+
buf := getPacketBuffer()
47+
buf.Split()
48+
buf.Split()
49+
// now we have 3 parts
50+
buf.Decrement()
51+
buf.Decrement()
52+
buf.Decrement()
53+
Expect(func() { buf.Decrement() }).To(Panic())
54+
})
55+
})

0 commit comments

Comments
 (0)