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"
"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
}

View File

@ -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())

View File

@ -2,6 +2,7 @@ package tester
import (
"dev.sum7.eu/genofire/yaja/model"
"github.com/FreifunkBremen/yanic/lib/duration"
log "github.com/sirupsen/logrus"
)
@ -10,6 +11,8 @@ type Config struct {
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"`

View File

@ -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 {