Skip to content

Commit

Permalink
Major update, fix all memory leaks, enhance lib performance, fix log …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
AmarnathCJD committed Nov 29, 2024
1 parent 88dda88 commit 7d05ffe
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/bot/echobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
42 changes: 42 additions & 0 deletions examples/cache/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 2 additions & 3 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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 + "] - <Tcp> ...")
m.Logger.Debug("eof error, reconnecting to [" + m.Addr + "] - <Tcp> ...")
err = m.Reconnect(false)
if err != nil {
m.Logger.Error(errors.Wrap(err, "reconnecting"))
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions telegram/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion telegram/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const (
ApiVersion = 195
Version = "v1.4.3"
Version = "v1.4.4"

LogDebug = utils.DebugLevel
LogInfo = utils.InfoLevel
Expand Down
5 changes: 3 additions & 2 deletions telegram/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7d05ffe

Please sign in to comment.