Sfoglia il codice sorgente

Cleanup pools from tlstypes

tags/v1.0.6^2^2
9seconds 5 anni fa
parent
commit
95b16cd805
2 ha cambiato i file con 5 aggiunte e 29 eliminazioni
  1. 5
    6
      tlstypes/handshake.go
  2. 0
    23
      tlstypes/pools.go

+ 5
- 6
tlstypes/handshake.go Vedi File

@@ -1,6 +1,7 @@
1 1
 package tlstypes
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"io"
5 6
 
6 7
 	"github.com/9seconds/mtg/utils"
@@ -15,8 +16,7 @@ type Handshake struct {
15 16
 }
16 17
 
17 18
 func (h *Handshake) WriteBytes(writer io.Writer) {
18
-	packetBuf := acquireBytesBuffer()
19
-	defer releaseBytesBuffer(packetBuf)
19
+	packetBuf := bytes.Buffer{}
20 20
 
21 21
 	writer.Write([]byte{byte(h.Type)}) // nolint: errcheck
22 22
 
@@ -24,7 +24,7 @@ func (h *Handshake) WriteBytes(writer io.Writer) {
24 24
 	packetBuf.Write(h.Random[:])
25 25
 	packetBuf.WriteByte(byte(len(h.SessionID)))
26 26
 	packetBuf.Write(h.SessionID)
27
-	h.Tail.WriteBytes(packetBuf)
27
+	h.Tail.WriteBytes(&packetBuf)
28 28
 
29 29
 	sizeUint24 := utils.ToUint24(uint32(packetBuf.Len()))
30 30
 	sizeUint24Bytes := sizeUint24[:]
@@ -35,10 +35,9 @@ func (h *Handshake) WriteBytes(writer io.Writer) {
35 35
 }
36 36
 
37 37
 func (h *Handshake) Len() int {
38
-	buf := acquireBytesBuffer()
39
-	defer releaseBytesBuffer(buf)
38
+	buf := bytes.Buffer{}
40 39
 
41
-	h.WriteBytes(buf)
40
+	h.WriteBytes(&buf)
42 41
 
43 42
 	return buf.Len()
44 43
 }

+ 0
- 23
tlstypes/pools.go Vedi File

@@ -1,23 +0,0 @@
1
-package tlstypes
2
-
3
-import (
4
-	"bytes"
5
-	"sync"
6
-)
7
-
8
-var (
9
-	poolBytesBuffer = sync.Pool{
10
-		New: func() interface{} {
11
-			return &bytes.Buffer{}
12
-		},
13
-	}
14
-)
15
-
16
-func acquireBytesBuffer() *bytes.Buffer {
17
-	return poolBytesBuffer.Get().(*bytes.Buffer)
18
-}
19
-
20
-func releaseBytesBuffer(buf *bytes.Buffer) {
21
-	buf.Reset()
22
-	poolBytesBuffer.Put(buf)
23
-}

Loading…
Annulla
Salva