Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit 2006f59

Browse files
author
Naoya Horiguchi
committed
peer/cmd/request: Introduce request timeout option
MinBFT never fails in theory as long as we meet the requirement of "<= f faulty nodes", but unfortunately there's no bug-free softwares, so it's better to prepare for such potential issues to avoid infinite wait. This patch suggests to add timeout to the sample application. Note that if timeout value is not specified, sample application waits forever as it does now. Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
1 parent 588a5eb commit 2006f59

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

sample/peer/cmd/request.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package cmd
1919

2020
import (
2121
"bufio"
22+
"context"
2223
"fmt"
2324
"os"
25+
"time"
2426

2527
"github.com/hyperledger-labs/minbft/api"
2628
"github.com/hyperledger-labs/minbft/client"
@@ -57,19 +59,43 @@ func init() {
5759
requestCmd.Flags().Int("id", 0, "ID of the client")
5860
must(viper.BindPFlag("client.id",
5961
requestCmd.Flags().Lookup("id")))
62+
requestCmd.Flags().String("timeout", "0", "Timeout for the request")
63+
must(viper.BindPFlag("client.timeout", requestCmd.Flags().Lookup("timeout")))
6064
}
6165

6266
type clientStack struct {
6367
api.Authenticator
6468
api.ReplicaConnector
6569
}
6670

67-
func request(client client.Client, arg string) {
68-
req := []byte(arg)
71+
func requestInternal(ctx context.Context, ch chan bool, client client.Client, req []byte) {
72+
defer func() {
73+
ch <- true
74+
}()
6975
res := <-client.Request(req)
7076
fmt.Println("Reply:", string(res))
7177
}
7278

79+
func request(client client.Client, arg string) {
80+
timeout := viper.GetDuration("client.timeout")
81+
ctx := context.Background()
82+
ch := make(chan bool)
83+
req := []byte(arg)
84+
85+
if timeout > time.Duration(0) {
86+
var cancel context.CancelFunc
87+
ctx, cancel = context.WithTimeout(ctx, timeout)
88+
time.AfterFunc(timeout, cancel)
89+
}
90+
91+
go requestInternal(ctx, ch, client, req)
92+
select {
93+
case <-ctx.Done():
94+
fmt.Println("Client Request timer expired.")
95+
case <-ch:
96+
}
97+
}
98+
7399
func requests(args []string) ([]byte, error) {
74100
id := uint32(viper.GetInt("client.id"))
75101

sample/peer/peer.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ client:
1616
# ID of the client instance
1717
id: 0
1818

19+
# Client request timeout (default: 0s, i.e. never timeout)
20+
timeout: 3s
21+
1922
# USIG options
2023
usig:
2124
# USIG enclave file (environment expansion is supported)

0 commit comments

Comments
 (0)