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

Commit

Permalink
peer/cmd/request: Introduce request timeout option
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Naoya Horiguchi committed Nov 22, 2019
1 parent 588a5eb commit 2006f59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 28 additions & 2 deletions sample/peer/cmd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package cmd

import (
"bufio"
"context"
"fmt"
"os"
"time"

"github.com/hyperledger-labs/minbft/api"
"github.com/hyperledger-labs/minbft/client"
Expand Down Expand Up @@ -57,19 +59,43 @@ func init() {
requestCmd.Flags().Int("id", 0, "ID of the client")
must(viper.BindPFlag("client.id",
requestCmd.Flags().Lookup("id")))
requestCmd.Flags().String("timeout", "0", "Timeout for the request")
must(viper.BindPFlag("client.timeout", requestCmd.Flags().Lookup("timeout")))
}

type clientStack struct {
api.Authenticator
api.ReplicaConnector
}

func request(client client.Client, arg string) {
req := []byte(arg)
func requestInternal(ctx context.Context, ch chan bool, client client.Client, req []byte) {
defer func() {
ch <- true
}()
res := <-client.Request(req)
fmt.Println("Reply:", string(res))
}

func request(client client.Client, arg string) {
timeout := viper.GetDuration("client.timeout")
ctx := context.Background()
ch := make(chan bool)
req := []byte(arg)

if timeout > time.Duration(0) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
time.AfterFunc(timeout, cancel)
}

go requestInternal(ctx, ch, client, req)
select {
case <-ctx.Done():
fmt.Println("Client Request timer expired.")
case <-ch:
}
}

func requests(args []string) ([]byte, error) {
id := uint32(viper.GetInt("client.id"))

Expand Down
3 changes: 3 additions & 0 deletions sample/peer/peer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ client:
# ID of the client instance
id: 0

# Client request timeout (default: 0s, i.e. never timeout)
timeout: 3s

# USIG options
usig:
# USIG enclave file (environment expansion is supported)
Expand Down

0 comments on commit 2006f59

Please sign in to comment.