forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_buffer.go
53 lines (44 loc) · 1.04 KB
/
message_buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package pgvertica
import (
"io"
"net"
"reflect"
"github.com/jackc/pgproto3/v2"
)
type MessageBufferInterface interface {
queueMessages(messages ...pgproto3.Message)
sendQueuedMessages() error
buffSize() int
}
type MessageBuffer struct {
conn net.Conn
buffer []pgproto3.Message
}
func newMessagesBuffer(conn net.Conn) *MessageBuffer {
return &MessageBuffer{
conn: conn,
buffer: make([]pgproto3.Message, 0),
}
}
func (mb *MessageBuffer) queueMessages(messages ...pgproto3.Message) {
mb.buffer = append(mb.buffer, messages...)
}
func (mb *MessageBuffer) sendQueuedMessages() error {
if err := writeMessages(mb.conn, mb.buffer...); err != nil {
return err
}
mb.buffer = make([]pgproto3.Message, 0)
return nil
}
func (mb *MessageBuffer) buffSize() int {
return len(mb.buffer)
}
func writeMessages(w io.Writer, msgs ...pgproto3.Message) error {
var buf []byte
for _, msg := range msgs {
Logger.Debug("[send] message", "type", reflect.TypeOf(msg), "message", msg)
buf = msg.Encode(buf)
}
_, err := w.Write(buf)
return err
}