mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-10 15:37:35 +02:00
rpc: do not leak grpc state change logger goroutine
This commit is contained in:
@ -29,6 +29,7 @@ type Client struct {
|
|||||||
controlClient pdu.ReplicationClient // this the grpc client instance, see constructor
|
controlClient pdu.ReplicationClient // this the grpc client instance, see constructor
|
||||||
controlConn *grpc.ClientConn
|
controlConn *grpc.ClientConn
|
||||||
loggers Loggers
|
loggers Loggers
|
||||||
|
closed chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ logic.Endpoint = &Client{}
|
var _ logic.Endpoint = &Client{}
|
||||||
@ -46,14 +47,21 @@ func NewClient(cn transport.Connecter, loggers Loggers) *Client {
|
|||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
loggers: loggers,
|
loggers: loggers,
|
||||||
|
closed: make(chan struct{}),
|
||||||
}
|
}
|
||||||
grpcConn := grpchelper.ClientConn(muxedConnecter.control, loggers.Control)
|
grpcConn := grpchelper.ClientConn(muxedConnecter.control, loggers.Control)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
<-c.closed
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
defer cancel()
|
||||||
|
for ctx.Err() == nil {
|
||||||
state := grpcConn.GetState()
|
state := grpcConn.GetState()
|
||||||
loggers.General.WithField("grpc_state", state.String()).Debug("grpc state change")
|
loggers.General.WithField("grpc_state", state.String()).Debug("grpc state change")
|
||||||
grpcConn.WaitForStateChange(context.TODO(), state)
|
grpcConn.WaitForStateChange(ctx, state)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
c.controlClient = pdu.NewReplicationClient(grpcConn)
|
c.controlClient = pdu.NewReplicationClient(grpcConn)
|
||||||
@ -64,8 +72,9 @@ func NewClient(cn transport.Connecter, loggers Loggers) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Close() {
|
func (c *Client) Close() {
|
||||||
|
close(c.closed)
|
||||||
if err := c.controlConn.Close(); err != nil {
|
if err := c.controlConn.Close(); err != nil {
|
||||||
c.loggers.General.WithError(err).Error("cannot cloe control connection")
|
c.loggers.General.WithError(err).Error("cannot close control connection")
|
||||||
}
|
}
|
||||||
// TODO c.dataClient should have Close()
|
// TODO c.dataClient should have Close()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user