mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 17:45:17 +03:00
fix: stream close in agent connection
This commit is contained in:
@@ -44,22 +44,23 @@ func (a *AgentConnection) Listen() error {
|
|||||||
go a.listenHeartbeat(heartbeatsChan)
|
go a.listenHeartbeat(heartbeatsChan)
|
||||||
defer close(heartbeatsChan)
|
defer close(heartbeatsChan)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
err := a.Close()
|
||||||
|
if err != nil {
|
||||||
|
a.log.Warn().Err(err).Msg("failed stream close")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-a.ctx.Done():
|
case <-a.ctx.Done():
|
||||||
err := a.stream.Close()
|
|
||||||
if err != nil {
|
|
||||||
a.log.Warn().Err(err).Msg("failed stream close")
|
|
||||||
}
|
|
||||||
return a.ctx.Err()
|
return a.ctx.Err()
|
||||||
default:
|
default:
|
||||||
agentEvent, err := a.stream.Recv()
|
agentEvent, err := a.stream.Recv()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
a.cancel()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.cancel()
|
|
||||||
return fmt.Errorf("stream: %w", err)
|
return fmt.Errorf("stream: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,3 +142,8 @@ func (a *AgentConnection) Execute(ctx context.Context, request domainHub.AgentRe
|
|||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AgentConnection) Close() error {
|
||||||
|
a.cancel()
|
||||||
|
return a.stream.Close()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user