Skip to content

Commit

Permalink
fix: set pluggable component grpc dial timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb0225 committed Oct 26, 2023
1 parent 3ff0b08 commit 197364e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion components/hello/pluggable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hello
import (
"context"
"fmt"
"time"

"mosn.io/layotto/components/pluggable"
helloproto "mosn.io/layotto/spec/proto/pluggable/v1/hello"
Expand Down Expand Up @@ -43,7 +44,8 @@ func NewGRPCHello(dialer pluggable.GRPCConnectionDialer) HelloService {

func (g *grpcHello) Init(config *HelloConfig) error {
// 1.dial grpc server
ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
conn, err := g.dialer(ctx)
if err != nil {
return fmt.Errorf("dial hello pluggable component: %w", err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/runtime/pluggable/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"os"
"path/filepath"
"time"

reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"

Expand Down Expand Up @@ -80,12 +81,13 @@ type grpcConnectionCloser interface {

// discover use grpc reflect to get services' information.
func discover() ([]grpcService, error) {
ctx := context.TODO()
// set grpc connection timeout to prevent block
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
serviceList, err := serviceDiscovery(func(socket string) (client reflectServiceClient, closer func(), err error) {
conn, err := pluggable.SocketDial(
ctx,
socket,
grpc.WithBlock(), // 超时设置
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -159,7 +161,7 @@ func serviceDiscovery(reflectClientFactory func(socket string) (client reflectSe
for _, s := range serviceList {
res = append(res, grpcService{
protoRef: s,
dialer: pluggable.SocketDialer(socket, grpc.WithBlock(), grpc.FailOnNonTempDialError(true)),
dialer: pluggable.SocketDialer(socket, grpc.FailOnNonTempDialError(true)),
componentName: common.RemoveExt(f.Name()),
})
}
Expand Down

0 comments on commit 197364e

Please sign in to comment.