From 7d05ffe7c63d2f4008914cae5b82d2324a04bfd2 Mon Sep 17 00:00:00 2001 From: AmarnathCJD Date: Sat, 30 Nov 2024 03:21:48 +0530 Subject: [PATCH] Major update, fix all memory leaks, enhance lib performance, fix log color disabling, remove unwanted codes, bug fixes, fix goroutine flooding, enhance log config, enhance cache config with option to disable completely, add example on cache, Layer 195, v1.4.4 --- examples/bot/echobot.go | 3 +-- examples/cache/main.go | 42 +++++++++++++++++++++++++++++++++++++++++ mtproto.go | 5 ++--- telegram/cache.go | 1 + telegram/client.go | 2 +- telegram/const.go | 2 +- telegram/media.go | 5 +++-- 7 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 examples/cache/main.go diff --git a/examples/bot/echobot.go b/examples/bot/echobot.go index 4c37bb27..2fe7f5ff 100644 --- a/examples/bot/echobot.go +++ b/examples/bot/echobot.go @@ -23,8 +23,7 @@ func main() { client.On(telegram.OnMessage, func(message *telegram.NewMessage) error { message.Respond(message) return nil - }, - telegram.FilterPrivate) + }, telegram.FilterPrivate) client.On("message:/start", func(message *telegram.NewMessage) error { message.Reply("Hello, I am a bot!") diff --git a/examples/cache/main.go b/examples/cache/main.go new file mode 100644 index 00000000..e7924217 --- /dev/null +++ b/examples/cache/main.go @@ -0,0 +1,42 @@ +package examples + +import ( + "github.com/amarnathcjd/gogram/telegram" +) + +const ( + appID = 6 + appHash = "YOUR_APP_HASH" + botToken = "YOUR_BOT_TOKEN" +) + +func main() { + // create a new client object + client, _ := telegram.NewClient(telegram.ClientConfig{ + AppID: appID, + AppHash: appHash, + LogLevel: telegram.LogInfo, + Cache: telegram.NewCache("cache_file.db", &telegram.CacheConfig{ + MaxSize: 1000, // TODO + LogLevel: telegram.LogInfo, + LogNoColor: true, // disable color in logs + Memory: true, // disable writing to disk + Disabled: false, // to totally disable cache + }), // if left empty, it will use the default cache 'cache.db', with default config + }) + + client.LoginBot(botToken) + + client.On(telegram.OnMessage, func(message *telegram.NewMessage) error { + message.Respond(message) + return nil + }, telegram.FilterPrivate) + + client.On("message:/start", func(message *telegram.NewMessage) error { + message.Reply("Hello, I am a bot!") + return nil + }) + + // lock the main routine + client.Idle() +} diff --git a/mtproto.go b/mtproto.go index e898ce7a..9a0ab0f2 100755 --- a/mtproto.go +++ b/mtproto.go @@ -319,7 +319,7 @@ func (m *MTProto) ExportNewSender(dcID int, mem bool) (*MTProto, error) { sender.noRedirect = true sender.exported = true - if err := sender.CreateConnection(true); err != nil { + if err := sender.CreateConnection(false); err != nil { return nil, errors.Wrap(err, "creating connection: exporting") } @@ -591,7 +591,7 @@ func (m *MTProto) startReadingResponses(ctx context.Context) { case context.Canceled: return case io.EOF: - m.Logger.Debug("EOF error, reconnecting to [" + m.Addr + "] - ...") + m.Logger.Debug("eof error, reconnecting to [" + m.Addr + "] - ...") err = m.Reconnect(false) if err != nil { m.Logger.Error(errors.Wrap(err, "reconnecting")) @@ -840,7 +840,6 @@ func (m *MTProto) offsetTime() { } if err := json.NewDecoder(resp.Body).Decode(&timeResponse); err != nil { - m.Logger.Error(errors.Wrap(err, "off-setting time")) return } diff --git a/telegram/cache.go b/telegram/cache.go index d01635ca..bb2d1895 100644 --- a/telegram/cache.go +++ b/telegram/cache.go @@ -67,6 +67,7 @@ func (c *CACHE) ImportJSON(data []byte) error { } type CacheConfig struct { + MaxSize int // Max size of cache: TODO LogLevel utils.LogLevel LogNoColor bool Memory bool diff --git a/telegram/client.go b/telegram/client.go index 497991b6..09bba290 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -376,7 +376,7 @@ func (c *Client) CreateExportedSender(dcID int) (*mtproto.MTProto, error) { var lastError error for retry := 0; retry <= retryLimit; retry++ { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() c.Log.Debug("creating exported sender for DC ", dcID) diff --git a/telegram/const.go b/telegram/const.go index ac9fabea..4ec95c1f 100644 --- a/telegram/const.go +++ b/telegram/const.go @@ -8,7 +8,7 @@ import ( const ( ApiVersion = 195 - Version = "v1.4.3" + Version = "v1.4.4" LogDebug = utils.DebugLevel LogInfo = utils.InfoLevel diff --git a/telegram/media.go b/telegram/media.go index d9d96bf5..8c684373 100644 --- a/telegram/media.go +++ b/telegram/media.go @@ -468,6 +468,7 @@ func (c *Client) DownloadMedia(file any, Opts ...*DownloadOptions) (string, erro } c.Logger.Info(fmt.Sprintf("file - download: (%s) - (%s) - (%d)", dest, sizetoHuman(size), parts)) + c.Logger.Info(fmt.Sprintf("exporting senders: dc(%d) - workers(%d)", dc, numWorkers)) go initializeWorkers(numWorkers, dc, c, w) @@ -522,7 +523,7 @@ func (c *Client) DownloadMedia(file any, Opts ...*DownloadOptions) (string, erro if err != nil { c.Log.Debug("part - (", p, ") - retrying... (", err, ")") - time.Sleep(time.Millisecond * 10) + time.Sleep(time.Millisecond * 5) continue } @@ -570,7 +571,7 @@ retrySinglePart: w.FreeWorker(sender) if err != nil { - time.Sleep(time.Millisecond * 10) + time.Sleep(time.Millisecond * 5) c.Log.Debug("seq-part - (", p, ") - retrying... (", err, ")") continue }