-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// RPCClient implements a rpc client tool with reconnect and load balance. | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net/rpc" | ||
"time" | ||
) | ||
|
||
type RPCClient struct { | ||
clients map[string]*rpc.Client | ||
random *rand.Rand | ||
} | ||
|
||
func NewRPCClient() (*RPCClient, error) { | ||
if serverInstance == nil { | ||
return nil, errorf(errServerNotInit) | ||
} | ||
if serverInstance.svrmgr == nil { | ||
return nil, errorf(errServerManagerNotInit) | ||
} | ||
return &RPCClient{ | ||
clients: make(map[string]*rpc.Client), | ||
random: rand.New(rand.NewSource(time.Now().UnixNano())), | ||
}, nil | ||
} | ||
|
||
func rpcCallWithReconnect(client *rpc.Client, addr string, serverMethod string, args interface{}, reply interface{}) error { | ||
err := client.Call(serverMethod, args, reply) | ||
if err == rpc.ErrShutdown { | ||
client, err = rpc.Dial("tcp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
} else if err == nil { | ||
return nil | ||
} | ||
|
||
err = client.Call(serverMethod, args, reply) | ||
|
||
return err | ||
} | ||
|
||
//RPC call with reconnect and retry. | ||
func (client *RPCClient) Call(severName string, serverMethod string, args interface{}, reply interface{}) error { | ||
addrs, err := serverInstance.svrmgr.GetServerHosts(severName, FlagRPCHost) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// pick a random start index for round robin | ||
total := len(addrs) | ||
start := client.random.Intn(total) | ||
|
||
for idx := 0; idx < total; idx++ { | ||
addr := addrs[(start+idx)%total] | ||
mapkey := fmt.Sprintf("%s[%s]", severName, addr) | ||
if client.clients[mapkey] == nil { | ||
client.clients[mapkey], err = rpc.Dial("tcp", addr) | ||
if err != nil { | ||
Log.Warnf("RPC dial error : %s", err) | ||
continue | ||
} | ||
} | ||
|
||
err = rpcCallWithReconnect(client.clients[mapkey], addr, serverMethod, args, reply) | ||
if err != nil { | ||
Log.Warn("RpcCallWithReconnect error : %s", err) | ||
continue | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return errorf("rpc all failed") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func validateRPCClient(t *testing.T) { | ||
rpccli, err := NewRPCClient() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
args := &Args{100, 200} | ||
var reply int | ||
|
||
err = rpccli.Call("test", "Arith.Multiply", args, &reply) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if reply != testRPCArgs.A*testRPCArgs.B { | ||
t.Fatalf("rpc client test faild, want %d, got %d", testRPCArgs.A*testRPCArgs.B, reply) | ||
} | ||
|
||
err = rpccli.Call("wrongtest", "Arith.Multiply", args, &reply) | ||
t.Log(err) | ||
if err == nil { | ||
t.Fatalf("rpc client should return error when no server found!") | ||
} | ||
} |
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