Last active
December 14, 2019 08:41
-
-
Save JesseYan/2c42a87588dcb685826a5fa3f12b0eb3 to your computer and use it in GitHub Desktop.
自己实现goroutine超时控制,解决grpc超时在cloud不生效
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package goroutinetimeout | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"testing" | |
"time" | |
"proto/bi/service/olap/service" | |
"github.com/go-errors/errors" | |
"golang.guazi-corp.com/finance/go-common/log" | |
"google.golang.org/grpc" | |
) | |
func TestGrpcClient(t *testing.T) { | |
address := "bi-go-olap-grpcserver.guazi-cloud.com:80" | |
//address := "bi-go-olap-grpcserver.guazi-apps.com:80" | |
// address := "localhost:8088" | |
conn, err := grpc.Dial(address, grpc.WithInsecure()) | |
if err != nil { | |
log.CustomLogger.Printf("did not connect: %v", err) | |
return | |
} | |
defer conn.Close() | |
c := service.NewDrillDownClient(conn) | |
request := &service.DrillDownRequest{ | |
// ModelId: "galileoV2", | |
// ModelId: "305", | |
ModelId: "5128", | |
UserId: "yanjiayi", | |
// IndicatorId: []string{"sale_b", "sale_c", "sale_consign"}, | |
// IndicatorId: []string{"已买_c2b", "金融已定_c2b", "金融已定_c2c"}, | |
IndicatorId: []string{ | |
"a5b36230", | |
"a30d27d6", | |
"a13c2e6c", | |
"ae0845ed", | |
"a8284661", | |
"a7b1fa7e", | |
"ab99fe73", | |
"a0357303", | |
"a1a49a18", | |
"a70c84b2", | |
"af369475", | |
"ab2f2daa", | |
"a43f7b24", | |
"a7d6f205", | |
"a14c23a7", | |
"a2a16239", | |
"a136c320", | |
"a442dfa1", | |
"af457618", | |
"ae7c8d3d", | |
"acd053cf", | |
"a3cafd2e", | |
"aa081dd7", | |
"a663e120", | |
"a54dcb6f", | |
"a7ea8897", | |
"a3cd678e", | |
}, | |
Drilldown: []string{"a2bf0522"}, | |
//FilterMap: []*service.FilterMap{ | |
// { | |
// CName: "a1b0f397", | |
// Value: []string{"业务管理部"}, | |
// }, | |
//}, | |
OrderInfo: []*service.OrderInfo{ | |
{ | |
CName: "a2bf0522", | |
OrderType: service.OrderType_Desc, | |
}, | |
}, | |
StartTime: 1575820800, // time.Now().Add(-24 * 33 * time.Hour).Unix(), | |
EndTime: 1576252799, // time.Now().Add(-24 * 1 * time.Hour).Unix(), | |
DrilldownLevel: []int64{0}, | |
DrilldownType: "auto", | |
PageNumber: 1, | |
PageSize: 100, | |
AppId: "venus", | |
FilterType: "id", | |
} | |
var ctx = context.Background() | |
//grpc 实现timeout这块有问题 | |
//var cancel context.CancelFunc | |
//ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second) | |
//defer cancel() | |
//r, err := c.GetDrillDownData(ctx, request, grpc.MaxCallRecvMsgSize(1024*1024*256)) | |
//use this func | |
r, err := GetDrillDownWithTimeout(ctx, c, request, 10*time.Second) | |
if err != nil { | |
fmt.Printf("could not greet: %v\n", err) | |
return | |
} | |
data, _ := json.Marshal(r) | |
fmt.Printf("Greeting: \n%s\n", data) | |
} | |
type DrilldownWrapper struct { | |
Response *service.DrillDownResponse | |
err error | |
} | |
func GetDrillDownWithTimeout(ctx context.Context, client service.DrillDownClient, request *service.DrillDownRequest, timeout time.Duration) (*service.DrillDownResponse, error) { | |
ch := make(chan DrilldownWrapper) | |
//go func | |
go getDrilldownInChannel(ctx, client, request, ch) | |
select { | |
case <-time.After(timeout): | |
return nil, errors.New("olap request timeout") | |
case re := <-ch: | |
return re.Response, re.err | |
} | |
} | |
func getDrilldownInChannel(ctx context.Context, client service.DrillDownClient, request *service.DrillDownRequest, ch chan DrilldownWrapper) { | |
t, err := client.GetDrillDownData(ctx, request, grpc.MaxCallRecvMsgSize(1024*1024*256)) | |
ch <- DrilldownWrapper{ | |
Response: t, | |
err: err, | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment