Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timeout to rpc call #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"hbs": {
"servers": ["127.0.0.1:6030"],
"timeout": 300,
"interval": 60
"interval": 60,
"callTimeout": 5
},
"alarm": {
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions cron/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func SyncStrategies() {

func syncStrategies() {
var strategiesResponse model.StrategiesResponse
err := g.HbsClient.Call("Hbs.GetStrategies", model.NullRpcRequest{}, &strategiesResponse)
err := g.HbsClient.CallTimeout("Hbs.GetStrategies", model.NullRpcRequest{}, &strategiesResponse, time.Duration(g.Config().Hbs.CallTimeout)*time.Second)
if err != nil {
log.Println("[ERROR] Hbs.GetStrategies:", err)
return
Expand Down Expand Up @@ -54,7 +54,7 @@ func rebuildStrategyMap(strategiesResponse *model.StrategiesResponse) {

func syncExpression() {
var expressionResponse model.ExpressionResponse
err := g.HbsClient.Call("Hbs.GetExpressions", model.NullRpcRequest{}, &expressionResponse)
err := g.HbsClient.CallTimeout("Hbs.GetExpressions", model.NullRpcRequest{}, &expressionResponse, time.Duration(g.Config().Hbs.CallTimeout)*time.Second)
if err != nil {
log.Println("[ERROR] Hbs.GetExpressions:", err)
return
Expand Down
7 changes: 4 additions & 3 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type RpcConfig struct {
}

type HbsConfig struct {
Servers []string `json:"servers"`
Timeout int64 `json:"timeout"`
Interval int64 `json:"interval"`
Servers []string `json:"servers"`
Timeout int64 `json:"timeout"`
Interval int64 `json:"interval"`
CallTimeout int64 `json:"callTimeout"`
}

type RedisConfig struct {
Expand Down
3 changes: 2 additions & 1 deletion g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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() {
Expand Down
21 changes: 17 additions & 4 deletions g/rpc.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package g

import (
"github.com/toolkits/net"
"log"
"math"
"net/rpc"
"sync"
"time"
)

type SingleConnRpcClient struct {
sync.Mutex
rpcClient *rpc.Client
rpcClient *TimeoutRpcClient
RpcServers []string
Timeout time.Duration
}
Expand All @@ -37,7 +35,7 @@ func (this *SingleConnRpcClient) insureConn() {
}

for _, s := range this.RpcServers {
this.rpcClient, err = net.JsonRpcClient("tcp", s, this.Timeout)
this.rpcClient, err = NewTimeoutRpcClient("tcp", s, this.Timeout)
if err == nil {
return
}
Expand Down Expand Up @@ -69,3 +67,18 @@ func (this *SingleConnRpcClient) Call(method string, args interface{}, reply int

return err
}

func (this *SingleConnRpcClient) CallTimeout(method string, args interface{}, reply interface{}, timeout time.Duration) error {

this.Lock()
defer this.Unlock()

this.insureConn()

err := this.rpcClient.CallTimeout(method, args, reply, timeout)
if err != nil {
this.close()
}

return err
}
35 changes: 35 additions & 0 deletions g/timeout_rpc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g

import (
"time"
"errors"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)

type TimeoutRpcClient struct {
*rpc.Client
}

func NewTimeoutRpcClient(network, address string, timeout time.Duration) (*TimeoutRpcClient, error) {
conn, err := net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
}

return &TimeoutRpcClient {
Client: jsonrpc.NewClient(conn),
}, nil
}

func (this *TimeoutRpcClient) CallTimeout(serviceMethod string, args interface{}, reply interface{}, timeout time.Duration) error {
call := this.Go(serviceMethod, args, reply, make(chan *rpc.Call, 1))

select {
case callPtr := <-call.Done:
return callPtr.Error
case <-time.After(timeout):
return errors.New("rpc call timeout")
}
}