mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-1390] Add timeout to kaspactl (#929)
* [NOD-1390] Add timeout to kaspactl * [NOD-1390] Fix grammar
This commit is contained in:
parent
86411a5ca5
commit
cd49c1dac7
@ -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()
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user