Commit ef912c89 authored by Loïck Bonniot's avatar Loïck Bonniot

[c] Use net.DefaultTimeout connected with flag

parent 1868373b
Pipeline #2120 passed with stage
......@@ -5,6 +5,7 @@ import (
"time"
"dfss"
"dfss/net"
dapi "dfss/dfssd/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
......@@ -22,6 +23,7 @@ A tool to sign multiparty contract using a secure cryptographic protocol`,
_ = cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
net.DefaultTimeout = viper.GetDuration("timeout")
dapi.Configure(viper.GetString("demo") != "", viper.GetString("demo"), "client")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
......@@ -40,8 +42,8 @@ func init() {
RootCmd.PersistentFlags().StringP("demo", "d", "", "demonstrator address and port, empty will disable it")
RootCmd.PersistentFlags().String("host", "localhost:9000", "host of the dfss platform")
RootCmd.PersistentFlags().IntP("port", "p", 9005, "port to use for P2P communication between clients")
RootCmd.PersistentFlags().Duration("timeout", 10*time.Second, "time to wait for connection and evidences before failing")
signCmd.Flags().Duration("timeout", time.Minute, "time to wait before fallback")
signCmd.Flags().Duration("slowdown", 0, "delay between each promises round (test only)")
signCmd.Flags().Int("stopbefore", 0, "stop signature just before the promises round n, -1 to stop right before signature round (test only)")
......@@ -53,6 +55,7 @@ func init() {
_ = viper.BindPFlag("demo", RootCmd.PersistentFlags().Lookup("demo"))
_ = viper.BindPFlag("local_port", RootCmd.PersistentFlags().Lookup("port"))
_ = viper.BindPFlag("platform_addrport", RootCmd.PersistentFlags().Lookup("host"))
_ = viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout"))
// Bind subcommands to root
RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd)
......
......@@ -18,7 +18,6 @@ var signCmd = &cobra.Command{
os.Exit(1)
}
_ = viper.BindPFlag("timeout", cmd.Flags().Lookup("timeout"))
_ = viper.BindPFlag("slowdown", cmd.Flags().Lookup("slowdown"))
_ = viper.BindPFlag("stopbefore", cmd.Flags().Lookup("stopbefore"))
......
......@@ -7,7 +7,7 @@ import (
cAPI "dfss/dfssc/api"
dAPI "dfss/dfssd/api"
pAPI "dfss/dfssp/api"
"github.com/spf13/viper"
"dfss/net"
"golang.org/x/net/context"
)
......@@ -59,7 +59,7 @@ func (m *SignatureManager) SendEvidence(promise *cAPI.Promise, signature *cAPI.S
return
}
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel()
var result *pAPI.ErrorCode
......
......@@ -6,7 +6,7 @@ import (
cAPI "dfss/dfssc/api"
"dfss/dfssc/common"
"github.com/spf13/viper"
"dfss/net"
)
// ExchangeAllSignatures creates and sends signatures to all the signers of the contract
......@@ -84,7 +84,7 @@ func (m *SignatureManager) ReceiveAllSignatures(out chan error) {
m.archives.receivedSignatures = append(m.archives.receivedSignatures, signature)
}
case <-time.After(viper.GetDuration("timeout")):
case <-time.After(net.DefaultTimeout):
out <- fmt.Errorf("Signature reception timeout!")
return
}
......
......@@ -85,9 +85,6 @@ func NewSignatureManager(passphrase string, c *contract.JSON) (*SignatureManager
m.mail = m.auth.Cert.Subject.CommonName
dAPI.SetIdentifier(m.mail)
net.DefaultTimeout = viper.GetDuration("timeout")
m.cServer = m.GetServer()
go func() { _ = net.Listen("0.0.0.0:"+strconv.Itoa(viper.GetInt("local_port")), m.cServer) }()
......@@ -212,7 +209,7 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
// need to create another way to access it
m.peersConn[user.Email] = conn
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel()
msg, err := client.Discover(ctx, &cAPI.Hello{Version: dfss.Version})
if err != nil {
......
......@@ -8,14 +8,17 @@ import (
"os"
"os/user"
"path/filepath"
"time"
"dfss/net"
"github.com/spf13/viper"
)
// Config is the structure that will be persisted in the configuration file
type Config struct {
Email string `json:"email"`
Platform string `json:"platform"`
Email string `json:"email"`
Platform string `json:"platform"`
Timeout time.Duration `json:"timeout"`
}
// Load loads the configuration file into memory.
......@@ -42,12 +45,17 @@ func Load() {
viper.Set("authenticated", isFileValid(viper.GetString("file_cert")))
viper.Set("local_port", 9005)
// Configure timeout
if t := viper.GetDuration("timeout"); t > 0 {
net.DefaultTimeout = t
}
return
}
// Save stores the current configuration object from memory.
func Save() {
c := Config{viper.GetString("email"), viper.GetString("platform")}
c := Config{viper.GetString("email"), viper.GetString("platform"), viper.GetDuration("timeout")}
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return
......
......@@ -17,7 +17,7 @@ import (
)
// DefaultTimeout should be used when a non-critical timeout is used in the application.
var DefaultTimeout = 5 * time.Second
var DefaultTimeout = 10 * time.Second
// Connect to a peer.
//
......
......@@ -107,7 +107,7 @@ func createClient(tmpDir string, ca []byte, port int) (*exec.Cmd, error) {
// Prepare the client command.
// The last argument is up to you!
cmd := exec.Command(path, "--ca", caPath, "--cert", certPath, "--host", "127.0.0.1:"+testPort, "--key", keyPath, "--port", strconv.Itoa(port), "-v", "-d", "localhost:9099")
cmd := exec.Command(path, "--ca", caPath, "--cert", certPath, "--host", "127.0.0.1:"+testPort, "--key", keyPath, "--port", strconv.Itoa(port), "--timeout", "3s" ,"-v", "-d", "localhost:9099")
return cmd, nil
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment