Skip to content

Commit 2d7c277

Browse files
committed
Do not keep all file handles open at the same time
Fixes #73
1 parent 85c5c69 commit 2d7c277

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

go/porcelain/deploy.go

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/sha256"
88
"encoding/hex"
99
"fmt"
10-
"hash"
1110
"io"
1211
"io/ioutil"
1312
"os"
@@ -78,39 +77,25 @@ type uploadError struct {
7877

7978
type FileBundle struct {
8079
Name string
81-
SHA hash.Hash
80+
Sum string
8281

82+
// Path OR Buffer should be populated
83+
Path string
8384
Buffer io.ReadSeeker
8485
}
8586

86-
func (f *FileBundle) Sum() string {
87-
return hex.EncodeToString(f.SHA.Sum(nil))
88-
}
89-
9087
func (f *FileBundle) Read(p []byte) (n int, err error) {
9188
return f.Buffer.Read(p)
9289
}
9390

94-
func (f *FileBundle) Close() error {
95-
return nil
91+
func (f *FileBundle) Seek(offset int64, whence int) (int64, error) {
92+
return f.Buffer.Seek(offset, whence)
9693
}
9794

98-
// We're mocking up a closer, to make sure the underlying file handle
99-
// doesn't get closed during an upload, but can be rewinded for retries
100-
// This method closes the file handle for real.
101-
func (f *FileBundle) CloseForReal() error {
102-
closer, ok := f.Buffer.(io.Closer)
103-
if ok {
104-
return closer.Close()
105-
}
95+
func (f *FileBundle) Close() error {
10696
return nil
10797
}
10898

109-
func (f *FileBundle) Rewind() error {
110-
_, err := f.Buffer.Seek(0, 0)
111-
return err
112-
}
113-
11499
type deployFiles struct {
115100
Files map[string]*FileBundle
116101
Sums map[string]string
@@ -126,12 +111,10 @@ func newDeployFiles() *deployFiles {
126111
}
127112

128113
func (d *deployFiles) Add(p string, f *FileBundle) {
129-
sum := f.Sum()
130-
131114
d.Files[p] = f
132-
d.Sums[p] = sum
133-
list, _ := d.Hashed[sum]
134-
d.Hashed[sum] = append(list, f)
115+
d.Sums[p] = f.Sum
116+
list, _ := d.Hashed[f.Sum]
117+
d.Hashed[f.Sum] = append(list, f)
135118
}
136119

137120
func (n *Netlify) overCommitted(d *deployFiles) bool {
@@ -374,7 +357,7 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
374357
context.GetLogger(ctx).WithFields(logrus.Fields{
375358
"deploy_id": d.ID,
376359
"file_path": f.Name,
377-
"file_sum": f.Sum(),
360+
"file_sum": f.Sum,
378361
}).Debug("Uploading file")
379362

380363
b := backoff.NewExponentialBackOff()
@@ -404,21 +387,27 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
404387

405388
switch t {
406389
case fileUpload:
407-
params := operations.NewUploadDeployFileParams().WithDeployID(d.ID).WithPath(f.Name).WithFileBody(f)
408-
if timeout != 0 {
409-
params.SetTimeout(timeout)
390+
body, operationError := os.Open(f.Path)
391+
if operationError == nil {
392+
defer body.Close()
393+
params := operations.NewUploadDeployFileParams().WithDeployID(d.ID).WithPath(f.Name).WithFileBody(body)
394+
if timeout != 0 {
395+
params.SetTimeout(timeout)
396+
}
397+
_, operationError = n.Operations.UploadDeployFile(params, authInfo)
410398
}
411-
_, operationError = n.Operations.UploadDeployFile(params, authInfo)
412399
case functionUpload:
413400
params := operations.NewUploadDeployFunctionParams().WithDeployID(d.ID).WithName(f.Name).WithFileBody(f)
414401
if timeout != 0 {
415402
params.SetTimeout(timeout)
416403
}
417404
_, operationError = n.Operations.UploadDeployFunction(params, authInfo)
405+
if operationError != nil {
406+
f.Buffer.Seek(0, 0)
407+
}
418408
}
419409

420410
if operationError != nil {
421-
f.Rewind()
422411
context.GetLogger(ctx).WithError(operationError).Errorf("Failed to upload file %v", f.Name)
423412
apiErr, ok := operationError.(errors.Error)
424413

@@ -427,10 +416,7 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
427416
sharedErr.err = operationError
428417
sharedErr.mutex.Unlock()
429418
}
430-
} else {
431-
f.CloseForReal()
432419
}
433-
434420
return operationError
435421
}, b)
436422

@@ -475,18 +461,19 @@ func walk(dir string, observer DeployObserver) (*deployFiles, error) {
475461
if err != nil {
476462
return err
477463
}
464+
defer o.Close()
478465

479466
file := &FileBundle{
480467
Name: rel,
481-
SHA: sha1.New(),
468+
Path: path,
482469
}
483470

484-
if _, err := io.Copy(file.SHA, o); err != nil {
471+
s := sha1.New()
472+
if _, err := io.Copy(s, o); err != nil {
485473
return err
486474
}
475+
file.Sum = hex.EncodeToString(s.Sum(nil))
487476

488-
o.Seek(0, 0)
489-
file.Buffer = o
490477
files.Add(rel, file)
491478

492479
if observer != nil {
@@ -517,9 +504,9 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
517504
case ".js":
518505
file := &FileBundle{
519506
Name: strings.TrimSuffix(i.Name(), filepath.Ext(i.Name())),
520-
SHA: sha256.New(),
521507
}
522508

509+
s := sha256.New()
523510
buf := new(bytes.Buffer)
524511
archive := zip.NewWriter(buf)
525512
fileHeader, err := archive.Create(i.Name())
@@ -530,6 +517,7 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
530517
if err != nil {
531518
return nil, err
532519
}
520+
defer fileEntry.Close()
533521
if _, err = io.Copy(fileHeader, fileEntry); err != nil {
534522
return nil, err
535523
}
@@ -539,13 +527,12 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
539527
}
540528

541529
fileBuffer := new(bytes.Buffer)
542-
m := io.MultiWriter(file.SHA, fileBuffer)
530+
m := io.MultiWriter(s, fileBuffer)
543531

544532
if _, err := io.Copy(m, buf); err != nil {
545533
return nil, err
546534
}
547-
548-
fileEntry.Seek(0, 0)
535+
file.Sum = hex.EncodeToString(s.Sum(nil))
549536
file.Buffer = bytes.NewReader(fileBuffer.Bytes())
550537
functions.Add(file.Name, file)
551538

0 commit comments

Comments
 (0)