Skip to content

Commit e39e6e7

Browse files
Merge pull request #10 from connectfit-team/fix-close-method
Fixed the cancellation of the event delivery
2 parents e25516e + f477ed0 commit e39e6e7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

client.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Client struct {
8080
password string
8181
uuid string
8282
conn net.Conn
83+
done chan struct{}
8384
wg sync.WaitGroup
8485
clientOptions
8586
}
@@ -167,11 +168,15 @@ func (c *Client) Stop() error {
167168
return ErrNotConnected
168169
}
169170

171+
// Close the connection to stop the blocking read call.
170172
err := c.conn.Close()
171173

174+
// In case the goroutine handling the events was trying to send an event
175+
// on the channel, we tell it to stop so we can close the event channel safely.
176+
close(c.done)
177+
172178
c.wg.Wait()
173179

174-
c.conn = nil
175180
return err
176181
}
177182

@@ -213,6 +218,7 @@ func (c *Client) register() error {
213218
// See https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#request-data
214219
func (c *Client) requestData(database, table, version, gtid string) (<-chan Event, error) {
215220
events := make(chan Event, 1)
221+
c.done = make(chan struct{}, 1)
216222

217223
cmd, err := c.formatRequestDataCommand(database, table, version, gtid)
218224
if err != nil {
@@ -268,7 +274,14 @@ func (c *Client) handleEvents(data chan<- Event) error {
268274
if err != nil {
269275
return err
270276
}
271-
data <- event
277+
278+
// If the client is stopped, we stop sending events on the channel.
279+
// Otherwise it would block forever if no one is receiving from it.
280+
select {
281+
case data <- event:
282+
case <-c.done:
283+
return nil
284+
}
272285
}
273286
return scanner.Err()
274287
}

0 commit comments

Comments
 (0)