mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
refactor: improve connection readiness check and enhance goroutine management in gRPC client; ensure proper context handling in stream listeners
This commit is contained in:
@@ -230,8 +230,11 @@ func (c *GrpcClient) Context() (ctx context.Context, cancel context.CancelFunc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *GrpcClient) IsReady() (res bool) {
|
func (c *GrpcClient) IsReady() (res bool) {
|
||||||
|
if c.conn == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
state := c.conn.GetState()
|
state := c.conn.GetState()
|
||||||
return c.conn != nil && state == connectivity.Ready
|
return state == connectivity.Ready
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GrpcClient) IsReadyAndRegistered() (res bool) {
|
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 {
|
func (c *GrpcClient) connect() error {
|
||||||
// Use a separate goroutine for reconnection handling
|
// Start reconnection handling goroutine with proper tracking
|
||||||
go c.handleReconnections()
|
c.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer c.wg.Done()
|
||||||
|
c.handleReconnections()
|
||||||
|
}()
|
||||||
|
|
||||||
// Initial connection attempt
|
// Initial connection attempt
|
||||||
return c.doConnect()
|
return c.doConnect()
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package middlewares
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/crawlab-team/crawlab/core/errors"
|
"github.com/crawlab-team/crawlab/core/errors"
|
||||||
"github.com/crawlab-team/crawlab/core/utils"
|
"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"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
)
|
)
|
||||||
@@ -43,7 +44,7 @@ func GetGrpcClientAuthTokenUnaryChainInterceptor() grpc.UnaryClientInterceptor {
|
|||||||
// set auth key
|
// set auth key
|
||||||
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
|
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 {
|
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...)
|
return invoker(ctx, method, req, reply, cc, opts...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,7 +53,7 @@ func GetGrpcClientAuthTokenStreamChainInterceptor() grpc.StreamClientInterceptor
|
|||||||
// set auth key
|
// set auth key
|
||||||
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
|
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) {
|
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...)
|
s, err := streamer(ctx, desc, cc, method, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -162,13 +162,29 @@ func (sm *StreamManager) streamListener(ts *TaskStream) {
|
|||||||
err error
|
err error
|
||||||
}, 1)
|
}, 1)
|
||||||
|
|
||||||
// Start receive operation in a separate goroutine
|
// Start receive operation in a separate goroutine with proper cleanup
|
||||||
go func() {
|
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()
|
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
|
msg *grpc.TaskServiceSubscribeResponse
|
||||||
err error
|
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
|
// Wait for result, timeout, or cancellation
|
||||||
|
|||||||
Reference in New Issue
Block a user