From 3b656d72849912a37cdd019e0076a56838561e5b Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 09:07:07 +0100 Subject: [PATCH] add timeout --- client/client.go | 12 +++++++++++- daemon/tester.go | 7 +++---- daemon/tester/config.go | 13 ++++++++----- daemon/tester/tester.go | 5 ++++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/client/client.go b/client/client.go index 9669251..a50b75a 100644 --- a/client/client.go +++ b/client/client.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "strings" + "time" "dev.sum7.eu/genofire/yaja/messages" "dev.sum7.eu/genofire/yaja/model" @@ -23,6 +24,10 @@ type Client struct { } func NewClient(jid *model.JID, password string) (*Client, error) { + return NewClientProtocolDuration(jid, password, "tcp", -1) +} + +func NewClientProtocolDuration(jid *model.JID, password string, proto string, timeout time.Duration) (*Client, error) { _, srvEntries, err := net.LookupSRV("xmpp-client", "tcp", jid.Domain) addr := jid.Domain + ":5222" if err == nil && len(srvEntries) > 0 { @@ -38,7 +43,12 @@ func NewClient(jid *model.JID, password string) (*Client, error) { if len(a) == 1 { addr += ":5222" } - conn, err := net.Dial("tcp", addr) + var conn net.Conn + if timeout >= 0 { + conn, err = net.DialTimeout(proto, addr, timeout) + } else { + conn, err = net.Dial(proto, addr) + } if err != nil { return nil, err } diff --git a/daemon/tester.go b/daemon/tester.go index a46006d..5895986 100644 --- a/daemon/tester.go +++ b/daemon/tester.go @@ -4,7 +4,6 @@ import ( "os" "os/signal" "syscall" - "time" log "github.com/sirupsen/logrus" @@ -40,7 +39,7 @@ var TesterCMD = &cobra.Command{ log.Warn("unable to load state file:", err) } - mainClient, err := client.NewClient(configTester.Client.JID, configTester.Client.Password) + mainClient, err := client.NewClientProtocolDuration(configTester.Client.JID, configTester.Client.Password, "tcp", configTester.Timeout.Duration) if err != nil { log.Fatal("unable to connect with main jabber client: ", err) } @@ -53,10 +52,10 @@ var TesterCMD = &cobra.Command{ Body: "yaja tester starts", }) } - + testerInstance.Timeout = configTester.Timeout.Duration testerInstance.Start(mainClient, configTester.Client.Password) testerInstance.CheckStatus() - testerWorker = worker.NewWorker(time.Minute, func() { + testerWorker = worker.NewWorker(configTester.Interval.Duration, func() { testerInstance.CheckStatus() file.SaveJSON(configTester.AccountsPath, testerInstance) file.SaveJSON(configTester.OutputPath, testerInstance.Output()) diff --git a/daemon/tester/config.go b/daemon/tester/config.go index 4a45718..819612d 100644 --- a/daemon/tester/config.go +++ b/daemon/tester/config.go @@ -2,15 +2,18 @@ package tester import ( "dev.sum7.eu/genofire/yaja/model" + "github.com/FreifunkBremen/yanic/lib/duration" log "github.com/sirupsen/logrus" ) type Config struct { - TLSDir string `toml:"tlsdir"` - AccountsPath string `toml:"accounts_path"` - OutputPath string `toml:"output_path"` - Logging log.Level `toml:"logging"` - Admins []*model.JID `toml:"admins"` + TLSDir string `toml:"tlsdir"` + AccountsPath string `toml:"accounts_path"` + OutputPath string `toml:"output_path"` + Logging log.Level `toml:"logging"` + Timeout duration.Duration `toml:"timeout"` + Interval duration.Duration `toml:"interval"` + Admins []*model.JID `toml:"admins"` Client struct { JID *model.JID `toml:"jid"` Password string `toml:"password"` diff --git a/daemon/tester/tester.go b/daemon/tester/tester.go index 4a46867..f6f9c72 100644 --- a/daemon/tester/tester.go +++ b/daemon/tester/tester.go @@ -1,6 +1,8 @@ package tester import ( + "time" + log "github.com/sirupsen/logrus" "dev.sum7.eu/genofire/yaja/client" @@ -11,6 +13,7 @@ import ( type Tester struct { mainClient *client.Client + Timeout time.Duration `json:"-"` Accounts map[string]*Account `json:"accounts"` Status map[string]*Status `json:"-"` } @@ -61,7 +64,7 @@ func (t *Tester) Connect(acc *Account) { logCTX.Warn("is already loggedin") return } - c, err := client.NewClient(acc.JID, acc.Password) + c, err := client.NewClientProtocolDuration(acc.JID, acc.Password, "tcp", t.Timeout) if err != nil { logCTX.Warnf("could not connect client: %s", err) } else {