Skip to content

Commit

Permalink
optimize for unstable network speeds
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Dec 3, 2022
1 parent ce5f6ac commit 4a83f56
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 83 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ require (
github.com/UserExistsError/conpty v0.1.0
github.com/alexflint/go-arg v1.4.3
github.com/creack/pty v1.1.18
github.com/ncruces/zenity v0.8.9
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
golang.org/x/text v0.3.7
github.com/ncruces/zenity v0.10.0
golang.org/x/sys v0.2.0
golang.org/x/term v0.2.0
golang.org/x/text v0.4.0
)

require (
github.com/akavel/rsrc v0.10.2 // indirect
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/alexflint/go-scalar v1.2.0 // indirect
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
golang.org/x/image v0.0.0-20220617043117-41969df76e82 // indirect
golang.org/x/image v0.1.0 // indirect
)

replace github.com/alexflint/go-arg v1.4.3 => github.com/trzsz/go-arg v1.4.4-0.20220722153732-ac5a9f75703f
45 changes: 34 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ github.com/UserExistsError/conpty v0.1.0 h1:irccNCaXUL/QJsAyT17DsGU35W6cxotWAugJ
github.com/UserExistsError/conpty v0.1.0/go.mod h1:qSwznBSBiRYbqlkGadeOQHOYxvTp1dR9qqineUaDoVM=
github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -13,8 +14,8 @@ github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f h1:OGqDDftRTwrvUoL6pO
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f/go.mod h1:Dv9D0NUlAsaQcGQZa5kc5mqR9ua72SmA8VXi4cd+cBw=
github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY=
github.com/ncruces/zenity v0.8.9 h1:9HYlnmKlxL9tuJeJmuvEmItBYRnWexq4skObMKqTy2U=
github.com/ncruces/zenity v0.8.9/go.mod h1:69zUhpw7EaRR5tWzqwpxSNKYcGhDoM7tbV65TGh2yuk=
github.com/ncruces/zenity v0.10.0 h1:cu2OKaarFbLHdJx8xStMhdamSjZ+eNB2LGUkFD+0VYU=
github.com/ncruces/zenity v0.10.0/go.mod h1:XKTS4jDNihIYL8aESCCb4bT8uej0gpdnxWdxG470CN0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 h1:GranzK4hv1/pqTIhMTXt2X8MmMOuH3hMeUR0o9SP5yc=
Expand All @@ -26,17 +27,39 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/trzsz/go-arg v1.4.4-0.20220722153732-ac5a9f75703f h1:J0tsnp7CsGLA68Ga5N+8F3xl3Z7cvlUqjV9HRqWGkaU=
github.com/trzsz/go-arg v1.4.4-0.20220722153732-ac5a9f75703f/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
golang.org/x/image v0.0.0-20220617043117-41969df76e82 h1:KpZB5pUSBvrHltNEdK/tw0xlPeD13M6M6aGP32gKqiw=
golang.org/x/image v0.0.0-20220617043117-41969df76e82/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.1.0 h1:r8Oj8ZA2Xy12/b5KZYj3tuv7NG/fBz3TwQVvpJ9l8Rk=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 h1:TMjZDarEwf621XDryfitp/8awEhiZNiwgphKlTMGRIg=
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion trzsz/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type Args struct {
Escape bool `arg:"-e" help:"escape all known control characters"`
Directory bool `arg:"-d" help:"transfer directories and files"`
Bufsize BufferSize `arg:"-B" placeholder:"N" default:"10M" help:"max buffer chunk size (1K<=N<=1G). (default: 10M)"`
Timeout int `arg:"-t" placeholder:"N" default:"10" help:"timeout ( N seconds ) for each buffer chunk.\nN <= 0 means never timeout. (default: 10)"`
Timeout int `arg:"-t" placeholder:"N" default:"20" help:"timeout ( N seconds ) for each buffer chunk.\nN <= 0 means never timeout. (default: 20)"`
}

var sizeRegexp = regexp.MustCompile("(?i)^(\\d+)(b|k|m|g|kb|mb|gb)?$")
Expand Down
145 changes: 82 additions & 63 deletions trzsz/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,47 @@ func (t *TrzszTransfer) sendFileSize(file *os.File, progress ProgressCallback) (
return size, nil
}

func (t *TrzszTransfer) sendFileData(file *os.File, size int64, binary bool, escapeCodes [][]byte, maxBufSize int64, progress ProgressCallback) ([]byte, error) {
step := int64(0)
bufSize := int64(1024)
buffer := make([]byte, bufSize)
hasher := md5.New()
for step < size {
beginTime := time.Now()
n, err := file.Read(buffer)
if err != nil {
return nil, err
}
length := int64(n)
data := buffer[:n]
if err := t.sendData(data, binary, escapeCodes); err != nil {
return nil, err
}
if _, err := hasher.Write(data); err != nil {
return nil, err
}
if err := t.checkInteger(length); err != nil {
return nil, err
}
step += length
if progress != nil && !reflect.ValueOf(progress).IsNil() {
progress.onStep(step)
}
chunkTime := time.Now().Sub(beginTime)
if length == bufSize && chunkTime < 500*time.Millisecond && bufSize < maxBufSize {
bufSize = minInt64(bufSize*2, maxBufSize)
buffer = make([]byte, bufSize)
} else if chunkTime >= 2*time.Second && bufSize > 1024 {
bufSize = 1024
buffer = make([]byte, bufSize)
}
if chunkTime > t.maxChunkTime {
t.maxChunkTime = chunkTime
}
}
return hasher.Sum(nil), nil
}

func (t *TrzszTransfer) sendFileMD5(digest []byte, progress ProgressCallback) error {
if err := t.sendBinary("MD5", digest); err != nil {
return err
Expand Down Expand Up @@ -544,8 +585,6 @@ func (t *TrzszTransfer) sendFiles(files []*TrzszFile, progress ProgressCallback)
return nil, err
}

bufSize := int64(1024)
buffer := make([]byte, bufSize)
var remoteNames []string
for _, f := range files {
file, remoteName, err := t.sendFileName(f, directory, progress)
Expand All @@ -563,45 +602,17 @@ func (t *TrzszTransfer) sendFiles(files []*TrzszFile, progress ProgressCallback)

defer file.Close()

fileSize, err := t.sendFileSize(file, progress)
size, err := t.sendFileSize(file, progress)
if err != nil {
return nil, err
}

step := int64(0)
hasher := md5.New()
for step < fileSize {
beginTime := time.Now()
n, err := file.Read(buffer)
if err != nil {
return nil, err
}
size := int64(n)
data := buffer[:n]
if err := t.sendData(data, binary, escapeCodes); err != nil {
return nil, err
}
if _, err := hasher.Write(data); err != nil {
return nil, err
}
if err := t.checkInteger(size); err != nil {
return nil, err
}
step += size
if progress != nil && !reflect.ValueOf(progress).IsNil() {
progress.onStep(step)
}
chunkTime := time.Now().Sub(beginTime)
if size == bufSize && chunkTime < 500*time.Millisecond && bufSize < maxBufSize {
bufSize = minInt64(bufSize*2, maxBufSize)
buffer = make([]byte, bufSize)
}
if chunkTime > t.maxChunkTime {
t.maxChunkTime = chunkTime
}
digest, err := t.sendFileData(file, size, binary, escapeCodes, maxBufSize, progress)
if err != nil {
return nil, err
}

if err := t.sendFileMD5(hasher.Sum(nil), progress); err != nil {
if err := t.sendFileMD5(digest, progress); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -764,6 +775,37 @@ func (t *TrzszTransfer) recvFileSize(progress ProgressCallback) (int64, error) {
return size, nil
}

func (t *TrzszTransfer) recvFileData(file *os.File, size int64, binary bool, escapeCodes [][]byte, timeout time.Duration, progress ProgressCallback) ([]byte, error) {
step := int64(0)
hasher := md5.New()
for step < size {
beginTime := time.Now()
data, err := t.recvData(binary, escapeCodes, timeout)
if err != nil {
return nil, err
}
if _, err := file.Write(data); err != nil {
return nil, err
}
length := int64(len(data))
step += length
if progress != nil && !reflect.ValueOf(progress).IsNil() {
progress.onStep(step)
}
if err := t.sendInteger("SUCC", length); err != nil {
return nil, err
}
if _, err := hasher.Write(data); err != nil {
return nil, err
}
chunkTime := time.Now().Sub(beginTime)
if chunkTime > t.maxChunkTime {
t.maxChunkTime = chunkTime
}
}
return hasher.Sum(nil), nil
}

func (t *TrzszTransfer) recvFileMD5(digest []byte, progress ProgressCallback) error {
expectDigest, err := t.recvBinary("MD5", false, nil)
if err != nil {
Expand Down Expand Up @@ -829,40 +871,17 @@ func (t *TrzszTransfer) recvFiles(path string, progress ProgressCallback) ([]str

defer file.Close()

fileSize, err := t.recvFileSize(progress)
size, err := t.recvFileSize(progress)
if err != nil {
return nil, err
}

step := int64(0)
hasher := md5.New()
for step < fileSize {
beginTime := time.Now()
data, err := t.recvData(binary, escapeCodes, timeout)
if err != nil {
return nil, err
}
if _, err := file.Write(data); err != nil {
return nil, err
}
size := int64(len(data))
step += size
if progress != nil && !reflect.ValueOf(progress).IsNil() {
progress.onStep(step)
}
if err := t.sendInteger("SUCC", size); err != nil {
return nil, err
}
if _, err := hasher.Write(data); err != nil {
return nil, err
}
chunkTime := time.Now().Sub(beginTime)
if chunkTime > t.maxChunkTime {
t.maxChunkTime = chunkTime
}
digest, err := t.recvFileData(file, size, binary, escapeCodes, timeout, progress)
if err != nil {
return nil, err
}

if err := t.recvFileMD5(hasher.Sum(nil), progress); err != nil {
if err := t.recvFileMD5(digest, progress); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion trzsz/trzsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func chooseUploadPaths(directory bool) ([]string, error) {
if directory {
options = append(options, zenity.Directory())
}
files, err := zenity.SelectFileMutiple(options...)
files, err := zenity.SelectFileMultiple(options...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion trzsz/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ SOFTWARE.

package trzsz

const kTrzszVersion = "0.1.9"
const kTrzszVersion = "1.0.0"

0 comments on commit 4a83f56

Please sign in to comment.