Update main.go, controlSocket.go, and 2 more files...

This commit is contained in:
RamiBerm
2021-05-24 10:45:22 +03:00
parent 24dba9e851
commit c1409251e2
4 changed files with 41 additions and 20 deletions

View File

@@ -1,28 +1,34 @@
package shared
import (
"fmt"
"github.com/gorilla/websocket"
"time"
)
func ConnectToSocketServer(address string) (*websocket.Conn, error) {
const maxTry = 5
const sleepTime = time.Second * 2
const (
DEFAULT_SOCKET_RETRIES = 3
DEFAULT_SOCKET_RETRY_SLEEP_TIME = time.Second * 10
)
func ConnectToSocketServer(address string, retries int, retrySleepTime time.Duration, hideTimeoutErrors bool) (*websocket.Conn, error) {
var err error
var connection *websocket.Conn
try := 0
// Connection to server fails if client pod is up before server.
// Retries solve this issue.
for try < maxTry {
for try < retries {
connection, _, err = websocket.DefaultDialer.Dial(address, nil)
if err != nil {
try++
// fmt.Printf("Failed connecting to websocket server: %s, (%v,%+v)\n", err, err, err)
if !hideTimeoutErrors {
fmt.Printf("Failed connecting to websocket server: %s, (%v,%+v)\n", err, err, err)
}
} else {
break
}
time.Sleep(sleepTime)
time.Sleep(retrySleepTime)
}
if err != nil {