-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
194 lines (164 loc) · 3.81 KB
/
stream.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package llsr
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"os/exec"
"github.com/golang/protobuf/proto"
"github.com/liquidm/llsr/decoderbufs"
)
var (
ErrStreamAlreadyRunning = errors.New("llsr: Replication stream is already opened")
ErrUnableToReadWholeMessage = errors.New("llsr: Unable to read whole message")
)
//Stream represents pg_recvlogical process.
//It is low level object and you should generally use service.Client.
type Stream struct {
cmd *exec.Cmd
running bool
stdOut io.ReadCloser
stdErr io.ReadCloser
errEvents chan interface{}
dataEvents chan interface{}
msgChan chan *decoderbufs.RowMessage
finished chan error
runtimeError error
}
//Creates new Stream object
func NewStream(dbConfig *DatabaseConfig, slot string, startPos LogPos) *Stream {
cmd := exec.Command("pg_recvlogical", "--start", "--file=-", "-S", slot, "-d", dbConfig.Database, "-F", "0")
if len(dbConfig.User) > 0 {
cmd.Args = append(cmd.Args, "-U", dbConfig.User)
}
if len(dbConfig.Host) > 0 {
cmd.Args = append(cmd.Args, "-h", dbConfig.Host)
}
if dbConfig.Port > 0 {
cmd.Args = append(cmd.Args, "-p", string(dbConfig.Port))
}
if len(dbConfig.Password) > 0 {
cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", dbConfig.Password))
}
if startPos > 0 {
cmd.Args = append(cmd.Args, "-I", startPos.String())
}
stream := &Stream{
cmd: cmd,
finished: make(chan error),
errEvents: make(chan interface{}),
dataEvents: make(chan interface{}),
msgChan: make(chan *decoderbufs.RowMessage),
}
return stream
}
//Establishes connection with PostgreSQL LLSR
func (s *Stream) Start() error {
var err error
if s.running {
return ErrStreamAlreadyRunning
}
s.stdOut, err = s.cmd.StdoutPipe()
if err != nil {
return err
}
s.stdErr, err = s.cmd.StderrPipe()
if err != nil {
return err
}
err = s.cmd.Start()
if err != nil {
return err
}
s.running = true
go s.recvErrors()
go s.recvData()
go s.convertData()
go s.wait()
return nil
}
//Closes connection with LLSR. It does not block. You should wait on Finished channel to ensure stream is closed.
func (s *Stream) Close() error {
return s.cmd.Process.Signal(os.Interrupt)
}
//Finished channel produces error message when underlying pg_recvlogical exits with error.
//It produces nil when pg_recvlogical exits with 0 (e.g when Close() was called)
func (s *Stream) Finished() <-chan error {
return s.finished
}
//Data channel produces RowMessage objects.
func (s *Stream) Data() <-chan *decoderbufs.RowMessage {
return s.msgChan
}
//ErrOut channel produces STDERR messages of underlying pg_recvlogical process.
func (s *Stream) ErrOut() <-chan interface{} {
return s.errEvents
}
func (s *Stream) recvErrors() {
reader := bufio.NewReader(s.stdErr)
for {
str, err := reader.ReadString('\n')
if len(str) > 0 {
s.errEvents <- str
}
if err == io.EOF {
return
}
if err != nil {
s.stopWith(err)
return
}
}
}
func (s *Stream) recvData() {
for {
var length uint64
err := binary.Read(s.stdOut, binary.BigEndian, &length)
if err != nil {
if err == io.EOF {
return
}
s.stopWith(err)
return
}
data := make([]byte, length+1)
_, err = io.ReadFull(s.stdOut, data)
if err != nil {
s.stopWith(err)
return
}
s.dataEvents <- data[:length]
}
}
func (s *Stream) convertData() {
for {
data := (<-s.dataEvents).([]byte)
decodedData := &decoderbufs.RowMessage{}
err := proto.Unmarshal(data, decodedData)
if err != nil {
s.stopWith(err)
return
}
s.msgChan <- decodedData
}
}
func (s *Stream) wait() {
err := s.cmd.Wait()
if err == nil {
err = s.runtimeError
s.runtimeError = nil
}
s.finished <- err
}
func (s *Stream) stopWith(err error) {
s.runtimeError = err
stopErr := s.Close()
if stopErr != nil {
select {
case s.finished <- err:
default:
}
}
}