sum7
/
yaja
Archived
1
0
Fork 0

add timeout

This commit is contained in:
Martin/Geno 2018-02-11 09:07:07 +01:00
parent 4d39701a74
commit 3b656d7284
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 26 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net" "net"
"strings" "strings"
"time"
"dev.sum7.eu/genofire/yaja/messages" "dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model" "dev.sum7.eu/genofire/yaja/model"
@ -23,6 +24,10 @@ type Client struct {
} }
func NewClient(jid *model.JID, password string) (*Client, error) { 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) _, srvEntries, err := net.LookupSRV("xmpp-client", "tcp", jid.Domain)
addr := jid.Domain + ":5222" addr := jid.Domain + ":5222"
if err == nil && len(srvEntries) > 0 { if err == nil && len(srvEntries) > 0 {
@ -38,7 +43,12 @@ func NewClient(jid *model.JID, password string) (*Client, error) {
if len(a) == 1 { if len(a) == 1 {
addr += ":5222" 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -40,7 +39,7 @@ var TesterCMD = &cobra.Command{
log.Warn("unable to load state file:", err) 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 { if err != nil {
log.Fatal("unable to connect with main jabber client: ", err) log.Fatal("unable to connect with main jabber client: ", err)
} }
@ -53,10 +52,10 @@ var TesterCMD = &cobra.Command{
Body: "yaja tester starts", Body: "yaja tester starts",
}) })
} }
testerInstance.Timeout = configTester.Timeout.Duration
testerInstance.Start(mainClient, configTester.Client.Password) testerInstance.Start(mainClient, configTester.Client.Password)
testerInstance.CheckStatus() testerInstance.CheckStatus()
testerWorker = worker.NewWorker(time.Minute, func() { testerWorker = worker.NewWorker(configTester.Interval.Duration, func() {
testerInstance.CheckStatus() testerInstance.CheckStatus()
file.SaveJSON(configTester.AccountsPath, testerInstance) file.SaveJSON(configTester.AccountsPath, testerInstance)
file.SaveJSON(configTester.OutputPath, testerInstance.Output()) file.SaveJSON(configTester.OutputPath, testerInstance.Output())

View File

@ -2,15 +2,18 @@ package tester
import ( import (
"dev.sum7.eu/genofire/yaja/model" "dev.sum7.eu/genofire/yaja/model"
"github.com/FreifunkBremen/yanic/lib/duration"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type Config struct { type Config struct {
TLSDir string `toml:"tlsdir"` TLSDir string `toml:"tlsdir"`
AccountsPath string `toml:"accounts_path"` AccountsPath string `toml:"accounts_path"`
OutputPath string `toml:"output_path"` OutputPath string `toml:"output_path"`
Logging log.Level `toml:"logging"` Logging log.Level `toml:"logging"`
Admins []*model.JID `toml:"admins"` Timeout duration.Duration `toml:"timeout"`
Interval duration.Duration `toml:"interval"`
Admins []*model.JID `toml:"admins"`
Client struct { Client struct {
JID *model.JID `toml:"jid"` JID *model.JID `toml:"jid"`
Password string `toml:"password"` Password string `toml:"password"`

View File

@ -1,6 +1,8 @@
package tester package tester
import ( import (
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/client" "dev.sum7.eu/genofire/yaja/client"
@ -11,6 +13,7 @@ import (
type Tester struct { type Tester struct {
mainClient *client.Client mainClient *client.Client
Timeout time.Duration `json:"-"`
Accounts map[string]*Account `json:"accounts"` Accounts map[string]*Account `json:"accounts"`
Status map[string]*Status `json:"-"` Status map[string]*Status `json:"-"`
} }
@ -61,7 +64,7 @@ func (t *Tester) Connect(acc *Account) {
logCTX.Warn("is already loggedin") logCTX.Warn("is already loggedin")
return return
} }
c, err := client.NewClient(acc.JID, acc.Password) c, err := client.NewClientProtocolDuration(acc.JID, acc.Password, "tcp", t.Timeout)
if err != nil { if err != nil {
logCTX.Warnf("could not connect client: %s", err) logCTX.Warnf("could not connect client: %s", err)
} else { } else {