refactor: improve connection readiness check and enhance goroutine management in gRPC client; ensure proper context handling in stream listeners

This commit is contained in:
Marvin Zhang
2025-08-07 11:12:46 +08:00
parent 060396af3d
commit d042bc8cd7
3 changed files with 33 additions and 9 deletions

View File

@@ -230,8 +230,11 @@ func (c *GrpcClient) Context() (ctx context.Context, cancel context.CancelFunc)
}
func (c *GrpcClient) IsReady() (res bool) {
if c.conn == nil {
return false
}
state := c.conn.GetState()
return c.conn != nil && state == connectivity.Ready
return state == connectivity.Ready
}
func (c *GrpcClient) IsReadyAndRegistered() (res bool) {
@@ -602,8 +605,12 @@ func (c *GrpcClient) getClientWithContext(ctx context.Context, getter func() int
}
func (c *GrpcClient) connect() error {
// Use a separate goroutine for reconnection handling
go c.handleReconnections()
// Start reconnection handling goroutine with proper tracking
c.wg.Add(1)
go func() {
defer c.wg.Done()
c.handleReconnections()
}()
// Initial connection attempt
return c.doConnect()

View File

@@ -2,9 +2,10 @@ package middlewares
import (
"context"
"github.com/crawlab-team/crawlab/core/errors"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
@@ -43,7 +44,7 @@ func GetGrpcClientAuthTokenUnaryChainInterceptor() grpc.UnaryClientInterceptor {
// set auth key
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.NewOutgoingContext(context.Background(), md)
ctx = metadata.NewOutgoingContext(ctx, md)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
@@ -52,7 +53,7 @@ func GetGrpcClientAuthTokenStreamChainInterceptor() grpc.StreamClientInterceptor
// set auth key
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.NewOutgoingContext(context.Background(), md)
ctx = metadata.NewOutgoingContext(ctx, md)
s, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err

View File

@@ -162,13 +162,29 @@ func (sm *StreamManager) streamListener(ts *TaskStream) {
err error
}, 1)
// Start receive operation in a separate goroutine
// Start receive operation in a separate goroutine with proper cleanup
go func() {
defer func() {
if r := recover(); r != nil {
sm.service.Errorf("stream recv goroutine panic for task[%s]: %v", ts.taskId.Hex(), r)
}
}()
msg, err := ts.stream.Recv()
resultChan <- struct {
// Use select to ensure we don't block if the main goroutine has exited
select {
case resultChan <- struct {
msg *grpc.TaskServiceSubscribeResponse
err error
}{msg, err}
}{msg, err}:
case <-ts.ctx.Done():
// Parent context cancelled, just return without sending
return
case <-sm.ctx.Done():
// Manager context cancelled, just return without sending
return
}
}()
// Wait for result, timeout, or cancellation