add timeout
This commit is contained in:
parent
4d39701a74
commit
3b656d7284
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
|
|
@ -2,6 +2,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,6 +11,8 @@ type Config struct {
|
||||||
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"`
|
||||||
|
Timeout duration.Duration `toml:"timeout"`
|
||||||
|
Interval duration.Duration `toml:"interval"`
|
||||||
Admins []*model.JID `toml:"admins"`
|
Admins []*model.JID `toml:"admins"`
|
||||||
Client struct {
|
Client struct {
|
||||||
JID *model.JID `toml:"jid"`
|
JID *model.JID `toml:"jid"`
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Reference in New Issue