Просмотр исходного кода

Add pools for packetack

tags/v1.0.4^2
9seconds 6 лет назад
Родитель
Сommit
9125a29e79

+ 3
- 1
wrappers/packetack/client_abridged.go Просмотреть файл

88
 		return nil
88
 		return nil
89
 	case packetLength < clientAbridgedLargePacketLength:
89
 	case packetLength < clientAbridgedLargePacketLength:
90
 		length24 := utils.ToUint24(uint32(packetLength))
90
 		length24 := utils.ToUint24(uint32(packetLength))
91
-		buf := bytes.Buffer{}
91
+
92
+		buf := acquireClientBytesBuffer()
93
+		defer releaseClientBytesBuffer(buf)
92
 
94
 
93
 		buf.WriteByte(byte(clientAbridgedSmallPacketLength))
95
 		buf.WriteByte(byte(clientAbridgedSmallPacketLength))
94
 		buf.Write(length24[:])
96
 		buf.Write(length24[:])

+ 4
- 3
wrappers/packetack/client_intermediate_secure.go Просмотреть файл

1
 package packetack
1
 package packetack
2
 
2
 
3
 import (
3
 import (
4
-	"bytes"
5
 	"encoding/binary"
4
 	"encoding/binary"
6
 	"fmt"
5
 	"fmt"
7
 	"math/rand"
6
 	"math/rand"
35
 		return nil
34
 		return nil
36
 	}
35
 	}
37
 
36
 
38
-	buf := bytes.Buffer{}
37
+	buf := acquireClientBytesBuffer()
38
+	defer releaseClientBytesBuffer(buf)
39
+
39
 	paddingLength := rand.Intn(4)
40
 	paddingLength := rand.Intn(4)
40
 	buf.Grow(4 + len(packet) + paddingLength)
41
 	buf.Grow(4 + len(packet) + paddingLength)
41
 
42
 
42
-	binary.Write(&buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
43
+	binary.Write(buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
43
 	buf.Write(packet)
44
 	buf.Write(packet)
44
 	buf.Write(make([]byte, paddingLength))
45
 	buf.Write(make([]byte, paddingLength))
45
 
46
 

+ 37
- 0
wrappers/packetack/pools.go Просмотреть файл

1
+package packetack
2
+
3
+import (
4
+	"bytes"
5
+	"sync"
6
+)
7
+
8
+var (
9
+	poolClientBytesBuffer = sync.Pool{
10
+		New: func() interface{} {
11
+			return &bytes.Buffer{}
12
+		},
13
+	}
14
+	poolProxyBytesBuffer = sync.Pool{
15
+		New: func() interface{} {
16
+			return &bytes.Buffer{}
17
+		},
18
+	}
19
+)
20
+
21
+func acquireClientBytesBuffer() *bytes.Buffer {
22
+	return poolClientBytesBuffer.Get().(*bytes.Buffer)
23
+}
24
+
25
+func acquireProxyBytesBuffer() *bytes.Buffer {
26
+	return poolProxyBytesBuffer.Get().(*bytes.Buffer)
27
+}
28
+
29
+func releaseClientBytesBuffer(buf *bytes.Buffer) {
30
+	buf.Reset()
31
+	poolClientBytesBuffer.Put(buf)
32
+}
33
+
34
+func releaseProxyBytesBuffer(buf *bytes.Buffer) {
35
+	buf.Reset()
36
+	poolProxyBytesBuffer.Put(buf)
37
+}

+ 2
- 1
wrappers/packetack/proxy.go Просмотреть файл

22
 }
22
 }
23
 
23
 
24
 func (w *wrapperProxy) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
24
 func (w *wrapperProxy) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
25
-	buf := bytes.Buffer{}
25
+	buf := acquireProxyBytesBuffer()
26
+	defer releaseProxyBytesBuffer(buf)
26
 
27
 
27
 	flags := w.flags
28
 	flags := w.flags
28
 	if acks.Quick {
29
 	if acks.Quick {

Загрузка…
Отмена
Сохранить