rpc调用加入超时时间

This commit is contained in:
boyce
2019-02-28 14:08:07 +08:00
parent 9fb205cbdd
commit 4445b377f8

View File

@@ -8,11 +8,13 @@ import (
"bufio" "bufio"
"encoding/gob" "encoding/gob"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
"sync" "sync"
"time"
) )
// ServerError represents an error that has been returned from // ServerError represents an error that has been returned from
@@ -326,6 +328,12 @@ func (client *Client) Go(serviceMethod string, args interface{}, reply interface
// Call invokes the named function, waits for it to complete, and returns its error status. // Call invokes the named function, waits for it to complete, and returns its error status.
func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error { func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error {
call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done select {
return call.Error case call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done:
return call.Error
case <-time.After(10 * time.Second):
}
//call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done
return errors.New(fmt.Sprintf("Call RPC %s is time out 10s", serviceMethod))
} }