diff --git a/modules/judge/g/g.go b/modules/judge/g/g.go index f024877bd..a5ea4e92c 100644 --- a/modules/judge/g/g.go +++ b/modules/judge/g/g.go @@ -22,8 +22,9 @@ import ( // change log // 2.0.1: bugfix HistoryData limit // 2.0.2: clean stale data +// 2.0.3: add timeout to sync strategies and expressions const ( - VERSION = "2.0.2" + VERSION = "2.0.3" ) func init() { diff --git a/modules/judge/g/rpc.go b/modules/judge/g/rpc.go index 374f3ca43..f16a0db6c 100644 --- a/modules/judge/g/rpc.go +++ b/modules/judge/g/rpc.go @@ -15,6 +15,7 @@ package g import ( + "errors" "github.com/toolkits/net" "log" "math" @@ -25,9 +26,10 @@ import ( type SingleConnRpcClient struct { sync.Mutex - rpcClient *rpc.Client - RpcServers []string - Timeout time.Duration + rpcClient *rpc.Client + RpcServers []string + Timeout time.Duration + CallTimeout time.Duration } func (this *SingleConnRpcClient) close() { @@ -76,7 +78,19 @@ func (this *SingleConnRpcClient) Call(method string, args interface{}, reply int this.insureConn() - err := this.rpcClient.Call(method, args, reply) + done := make(chan error, 1) + go func() { + done <- this.rpcClient.Call(method, args, reply) + }() + + var err error + + select { + case <-time.After(this.CallTimeout): + err = errors.New("call hbs timeout") + case err = <-done: + } + if err != nil { this.close() } diff --git a/modules/judge/g/var.go b/modules/judge/g/var.go index d044b6651..1de935d2a 100644 --- a/modules/judge/g/var.go +++ b/modules/judge/g/var.go @@ -47,8 +47,9 @@ var ( func InitHbsClient() { HbsClient = &SingleConnRpcClient{ - RpcServers: Config().Hbs.Servers, - Timeout: time.Duration(Config().Hbs.Timeout) * time.Millisecond, + RpcServers: Config().Hbs.Servers, + Timeout: time.Duration(Config().Hbs.Timeout) * time.Millisecond, + CallTimeout: time.Duration(3000) * time.Millisecond, } }