From d14cd161da48f84e668281d5dfbb2c7bb9860b56 Mon Sep 17 00:00:00 2001 From: ycc Date: Sat, 20 Apr 2024 22:29:22 +0200 Subject: [PATCH] manage http timeout to allow long poll --- client/config.go | 5 ++++- client/helpers/backgroundHelper.go | 4 ++-- client/helpers/networkHelper.go | 4 ++-- http.go | 8 ++++++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/client/config.go b/client/config.go index dc392a2..fc70331 100644 --- a/client/config.go +++ b/client/config.go @@ -18,9 +18,12 @@ type Config struct { MaxIdsPerUser int `json:"max_ids_per_user,omitempty"` MsgDbRollingPeriod int `json:"msg_db_rolling_period,omitempty"` Chunksize int64 `json:"chunksize,omitempty"` - ServerPollInterval int `json:"server_poll_interval,omitempty"` DbSize int `json:"db_size,omitempty"` UserAgent string `json:"user_agent,omitempty"` + // Network + ServerPollInterval int `json:"server_poll_interval,omitempty"` + HttpTimeOut int `json:"http_timeout,omitempty"` + HttpLongPoll int `json:"http_long_poll,omitempty"` // GUI LastOpenChat string `json:"last_open_chat,omitempty"` SoundNotificationEnable bool `json:"sound_notification_enable,omitempty"` diff --git a/client/helpers/backgroundHelper.go b/client/helpers/backgroundHelper.go index f41c5f1..99bf11b 100644 --- a/client/helpers/backgroundHelper.go +++ b/client/helpers/backgroundHelper.go @@ -27,7 +27,7 @@ type ReceivedMessage struct { } // CheckForMessages checks for messages on a single server -func CheckForMessages(storage_path string, job *client.RequestsJob) (int, string, error) { +func CheckForMessages(storage_path string, job *client.RequestsJob, timeout int) (int, string, error) { count := 0 @@ -68,7 +68,7 @@ func CheckForMessages(storage_path string, job *client.RequestsJob) (int, string return -1, "CheckMessages: ProcessOutboundMessage", err } - response, err := meowlib.HttpPostMessage(job.Server.Url, data) + response, err := meowlib.HttpPostMessage(job.Server.Url, data, timeout) if err != nil { return -1, "CheckMessages: httpPostMessage", err } diff --git a/client/helpers/networkHelper.go b/client/helpers/networkHelper.go index ab28b87..4ca8246 100644 --- a/client/helpers/networkHelper.go +++ b/client/helpers/networkHelper.go @@ -5,7 +5,7 @@ import ( "forge.redroom.link/yves/meowlib/client" ) -func HttpSendMessage(serverUid string, message []byte) ([]byte, error) { +func HttpSendMessage(serverUid string, message []byte, timeout int) ([]byte, error) { id := client.GetConfig().GetIdentity() srv, err := id.MessageServers.LoadServer(serverUid) if err != nil { @@ -22,7 +22,7 @@ func HttpSendMessage(serverUid string, message []byte) ([]byte, error) { id.MessageServers.StoreServer(srv) } - response, err := meowlib.HttpPostMessage(srv.Url, message) + response, err := meowlib.HttpPostMessage(srv.Url, message, timeout) if err != nil { return nil, err } diff --git a/http.go b/http.go index 2890f62..92fb9f0 100644 --- a/http.go +++ b/http.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io" "net/http" + "time" ) func HttpGetId(url string) (response map[string]string, err error) { @@ -25,8 +26,11 @@ func HttpGetId(url string) (response map[string]string, err error) { return srvId, nil } -func HttpPostMessage(url string, msg []byte) (response []byte, err error) { - resp, err := http.Post(url+"/msg", +func HttpPostMessage(url string, msg []byte, timeout int) (response []byte, err error) { + client := http.Client{ + Timeout: time.Duration(timeout) * time.Second, + } + resp, err := client.Post(url+"/msg", "application/octet-stream", bytes.NewBuffer(msg)) if err != nil { return nil, err