Skip to content

Commit

Permalink
minor test fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Aug 6, 2024
1 parent efed1e0 commit cd5ff73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 14 additions & 0 deletions internal/transport/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ func (c *Reader) begin() {
}
}

func isClosed(ch <-chan int) bool {
select {
case <-ch:
return true
default:
}

return false
}

func (c *Reader) Read(p []byte) (int, error) {
if isClosed(c.sizeWant) {
return 0, io.EOF
}

select {
case <-c.ctx.Done():
return 0, c.ctx.Err()
Expand Down
9 changes: 6 additions & 3 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
const (
// The Initial DC to connect to, before auth
DefaultDataCenter = 4
DisconnectExportedAfter = 5 * time.Minute
DisconnectExportedAfter = 15 * time.Minute
)

type clientData struct {
Expand Down Expand Up @@ -364,12 +364,15 @@ func (c *Client) loopForCleaningExpiredSenders() {
for {
time.Sleep(DisconnectExportedAfter)
c.exportedSenders.Lock()
for i, s := range c.exportedSenders.senders {
newSenders := c.exportedSenders.senders[:0]
for _, s := range c.exportedSenders.senders {
if time.Since(s.added) > DisconnectExportedAfter {
s.client.Terminate()
c.exportedSenders.senders = append(c.exportedSenders.senders[:i], c.exportedSenders.senders[i+1:]...)
} else {
newSenders = append(newSenders, s)
}
}
c.exportedSenders.senders = newSenders
c.exportedSenders.Unlock()
}
}
Expand Down
13 changes: 9 additions & 4 deletions telegram/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ func (mb *Destination) WriteAt(p []byte, off int64) (n int, err error) {
return len(p), nil
}

func (mb *Destination) Close() error {
if mb.file != nil {
return mb.file.Close()
}
return nil
}

func (c *Client) DownloadMedia(file interface{}, Opts ...*DownloadOptions) (string, error) {
opts := getVariadic(Opts, &DownloadOptions{})
location, dc, size, fileName, err := GetFileLocation(file)
Expand All @@ -381,12 +388,10 @@ func (c *Client) DownloadMedia(file interface{}, Opts ...*DownloadOptions) (stri
}

fs.file = file

defer func() {
fs.file.Close()
}()
}

defer fs.Close()

parts := size / int64(partSize)
partOver := size % int64(partSize)

Expand Down

0 comments on commit cd5ff73

Please sign in to comment.