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

Add pooler for mtproto_frame wrapper

tags/v1.0.4^2
9seconds 6 лет назад
Родитель
Сommit
6449109b2b
2 измененных файлов: 28 добавлений и 4 удалений
  1. 5
    4
      wrappers/packet/mtproto_frame.go
  2. 23
    0
      wrappers/packet/pools.go

+ 5
- 4
wrappers/packet/mtproto_frame.go Просмотреть файл

42
 }
42
 }
43
 
43
 
44
 func (w *wrapperMtprotoFrame) Read() (conntypes.Packet, error) { // nolint: funlen
44
 func (w *wrapperMtprotoFrame) Read() (conntypes.Packet, error) { // nolint: funlen
45
-	buf := &bytes.Buffer{}
45
+	buf := acquireMtprotoFrameBytesBuffer()
46
+	defer releaseMtprotoFrameBytesBuffer(buf)
47
+
46
 	sum := crc32.NewIEEE()
48
 	sum := crc32.NewIEEE()
47
 	writer := io.MultiWriter(buf, sum)
49
 	writer := io.MultiWriter(buf, sum)
48
 
50
 
71
 	}
73
 	}
72
 
74
 
73
 	buf.Reset()
75
 	buf.Reset()
74
-	buf.Grow(int(messageLength) - 4 - 4)
75
 
76
 
76
 	if _, err := io.CopyN(writer, w.parent, int64(messageLength)-4-4); err != nil {
77
 	if _, err := io.CopyN(writer, w.parent, int64(messageLength)-4-4); err != nil {
77
 		return nil, fmt.Errorf("cannot read the message frame: %w", err)
78
 		return nil, fmt.Errorf("cannot read the message frame: %w", err)
113
 	messageLength := 4 + 4 + len(p) + 4
114
 	messageLength := 4 + 4 + len(p) + 4
114
 	paddingLength := (aes.BlockSize - messageLength%aes.BlockSize) % aes.BlockSize
115
 	paddingLength := (aes.BlockSize - messageLength%aes.BlockSize) % aes.BlockSize
115
 
116
 
116
-	buf := &bytes.Buffer{}
117
-	buf.Grow(messageLength + paddingLength)
117
+	buf := acquireMtprotoFrameBytesBuffer()
118
+	defer releaseMtprotoFrameBytesBuffer(buf)
118
 
119
 
119
 	binary.Write(buf, binary.LittleEndian, uint32(messageLength)) // nolint: errcheck
120
 	binary.Write(buf, binary.LittleEndian, uint32(messageLength)) // nolint: errcheck
120
 	binary.Write(buf, binary.LittleEndian, w.writeSeqNo)          // nolint: errcheck
121
 	binary.Write(buf, binary.LittleEndian, w.writeSeqNo)          // nolint: errcheck

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

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

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