Skip to content

Commit

Permalink
perf(ftp): non use SIZE FTP command (close #5150)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 3, 2023
1 parent 8e40465 commit f6ab1f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion drivers/ftp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (d *FTP) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*m
return nil, err
}

r := NewFTPFileReader(d.conn, file.GetPath())
r := NewFileReader(d.conn, file.GetPath(), file.GetSize())
link := &model.Link{
MFile: r,
}
Expand Down
41 changes: 18 additions & 23 deletions drivers/ftp/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"sync"
"sync/atomic"
"time"

"github.com/jlaffaye/ftp"
Expand All @@ -30,31 +31,32 @@ func (d *FTP) login() error {
return nil
}

// An FTP file reader that implements io.MFile for seeking.
type FTPFileReader struct {
// FileReader An FTP file reader that implements io.MFile for seeking.
type FileReader struct {
conn *ftp.ServerConn
resp *ftp.Response
offset int64
offset atomic.Int64
readAtOffset int64
mu sync.Mutex
path string
size int64
}

func NewFTPFileReader(conn *ftp.ServerConn, path string) *FTPFileReader {
return &FTPFileReader{
func NewFileReader(conn *ftp.ServerConn, path string, size int64) *FileReader {
return &FileReader{
conn: conn,
path: path,
size: size,
}
}

func (r *FTPFileReader) Read(buf []byte) (n int, err error) {
n, err = r.ReadAt(buf, r.offset)
r.mu.Lock()
defer r.mu.Unlock()
r.offset += int64(n)
func (r *FileReader) Read(buf []byte) (n int, err error) {
n, err = r.ReadAt(buf, r.offset.Load())
r.offset.Add(int64(n))
return
}
func (r *FTPFileReader) ReadAt(buf []byte, off int64) (n int, err error) {

func (r *FileReader) ReadAt(buf []byte, off int64) (n int, err error) {
if off < 0 {
return -1, os.ErrInvalid
}
Expand All @@ -80,23 +82,16 @@ func (r *FTPFileReader) ReadAt(buf []byte, off int64) (n int, err error) {
return
}

func (r *FTPFileReader) Seek(offset int64, whence int) (int64, error) {
r.mu.Lock()
defer r.mu.Unlock()

oldOffset := r.offset
func (r *FileReader) Seek(offset int64, whence int) (int64, error) {
oldOffset := r.offset.Load()
var newOffset int64
switch whence {
case io.SeekStart:
newOffset = offset
case io.SeekCurrent:
newOffset = oldOffset + offset
case io.SeekEnd:
size, err := r.conn.FileSize(r.path)
if err != nil {
return oldOffset, err
}
newOffset = offset + int64(size)
return r.size, nil
default:
return -1, os.ErrInvalid
}
Expand All @@ -109,11 +104,11 @@ func (r *FTPFileReader) Seek(offset int64, whence int) (int64, error) {
// offset not changed, so return directly
return oldOffset, nil
}
r.offset = newOffset
r.offset.Store(newOffset)
return newOffset, nil
}

func (r *FTPFileReader) Close() error {
func (r *FileReader) Close() error {
if r.resp != nil {
return r.resp.Close()
}
Expand Down

0 comments on commit f6ab1f7

Please sign in to comment.