[NOD-1390] Add timeout to kaspactl (#929)

* [NOD-1390] Add timeout to kaspactl

* [NOD-1390] Fix grammar
This commit is contained in:
Ori Newman 2020-09-14 12:33:42 +03:00 committed by GitHub
parent 86411a5ca5
commit cd49c1dac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 9 deletions

View File

@ -1,18 +1,19 @@
package main
import (
"github.com/jessevdk/go-flags"
"github.com/kaspanet/kaspad/infrastructure/config"
"github.com/pkg/errors"
"github.com/jessevdk/go-flags"
)
var (
defaultRPCServer = "localhost"
defaultRPCServer = "localhost"
defaultTimeout uint64 = 30
)
type configFlags struct {
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
Timeout uint64 `short:"t" long:"timeout" description:"Timeout for the request (in seconds)"`
RequestJSON string `description:"The request in JSON format"`
config.NetworkFlags
}
@ -20,6 +21,7 @@ type configFlags struct {
func parseConfig() (*configFlags, error) {
cfg := &configFlags{
RPCServer: defaultRPCServer,
Timeout: defaultTimeout,
}
parser := flags.NewParser(cfg, flags.PrintErrors|flags.HelpFlag)
args, err := parser.Parse()

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/kaspanet/kaspad/infrastructure/network/rpcclient/grpcclient"
"os"
"time"
)
func main() {
@ -22,13 +23,26 @@ func main() {
}
defer client.Disconnect()
requestString := cfg.RequestJSON
responseString, err := client.PostJSON(requestString)
if err != nil {
printErrorAndExit(fmt.Sprintf("error posting the request to the RPC server: %s", err))
}
var responseString string
done := make(chan struct{})
fmt.Println(responseString)
go func() {
requestString := cfg.RequestJSON
var err error
responseString, err = client.PostJSON(requestString)
if err != nil {
printErrorAndExit(fmt.Sprintf("error posting the request to the RPC server: %s", err))
}
done <- struct{}{}
}()
timeout := time.Duration(cfg.Timeout) * time.Second
select {
case <-done:
fmt.Println(responseString)
case <-time.After(timeout):
printErrorAndExit(fmt.Sprintf("timeout of %s has been exceeded", timeout))
}
}
func printErrorAndExit(message string) {