Skip to content

Commit

Permalink
Retry 3 times when RPC timeout
Browse files Browse the repository at this point in the history
Signed-off-by: bill fort <[email protected]>
  • Loading branch information
billfort authored and yilunzhang committed Jul 8, 2023
1 parent 77d9d80 commit 65cf266
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
24 changes: 16 additions & 8 deletions admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,26 @@ func (c *Client) RPCCall(addr, method string, params interface{}, result interfa
return err
}

onReply, err := c.Send(nkn.NewStringArray(addr), req, nil)
var onReply *nkn.OnMessage
var reply *nkn.Message
Loop:
for i := 0; i < 3; i++ { // retry 3 times if timeout
onReply, err = c.Send(nkn.NewStringArray(addr), req, nil)
if err != nil {
return err
}

select {
case reply = <-onReply.C:
break Loop
case <-time.After(c.replyTimeout):
err = errReplyTimeout
}
}
if err != nil {
return err
}

var reply *nkn.Message
select {
case reply = <-onReply.C:
case <-time.After(c.replyTimeout):
return errReplyTimeout
}

resp := &rpcResp{
Result: result,
}
Expand Down
6 changes: 3 additions & 3 deletions tests/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const (

seedHex = "e68e046d13dd911594576ba0f4a196e9666790dc492071ad9ea5972c0b940435"

tcpPort = ":54321"
httpPort = ":54322"
udpPort = ":54323"
tcpPort = ":20001"
httpPort = ":20002"
udpPort = ":20003"

tunaNodeStarted = "tuna node is started"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestMain(m *testing.M) {
return
}

time.Sleep(5 * time.Second)
time.Sleep(10 * time.Second)

exitVal := m.Run()
os.Exit(exitVal)
Expand Down
12 changes: 6 additions & 6 deletions tests/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
}

if opts.Client {
port, err := getFreePort(port)
port, err = getFreePort(port)
if err != nil {
return err
}
Expand All @@ -66,7 +66,7 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
}
}()

time.Sleep(3 * time.Second) // wait for nconnect to create tunnels
time.Sleep(5 * time.Second) // wait for nconnect to create tunnels

tunnels := nc.GetTunnels()
for _, tunnel := range tunnels {
Expand Down Expand Up @@ -140,16 +140,16 @@ type Person struct {
Age int
}

func getFreePort(port int) (int, error) {
func getFreePort(p int) (int, error) {
for i := 0; i < 100; i++ {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", port))
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", p))
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
port++
p++
continue
}

Expand All @@ -164,8 +164,8 @@ func waitForSSProxReady() error {
for i := 0; i < 100; i++ {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port))
if err != nil {
fmt.Printf("waitForSSProxReady err: %v\n", err)
time.Sleep(2 * time.Second)
continue
}
if conn != nil {
conn.Close()
Expand Down
3 changes: 3 additions & 0 deletions tests/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func StartTcpServer() error {
return err
}
go func(conn net.Conn) {
defer conn.Close()
b := make([]byte, 1024)
for {
n, err := conn.Read(b)
Expand Down Expand Up @@ -57,6 +58,8 @@ func StartTCPClient(serverAddr string) error {
fmt.Printf("StartTCPClient, dailer.Dial err: %v\n", err)
return err
}

defer conn.Close()
fmt.Printf("StartTCPClient, dail to %v success\n", serverAddr)

user := &Person{Name: "tcp_boy", Age: 0}
Expand Down
1 change: 1 addition & 0 deletions tests/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func StartUdpServer() error {
return err
}

defer udpServer.Close()
fmt.Printf("UDP server is listening at %v\n", udpPort)

b := make([]byte, 1024)
Expand Down

0 comments on commit 65cf266

Please sign in to comment.