@@ -5,7 +5,9 @@ package signatures
55
66import (
77 "bytes"
8+ "errors"
89 "fmt"
10+ "net/http"
911 "strings"
1012 "sync"
1113
@@ -29,7 +31,7 @@ import (
2931
3032// constants
3133const (
32- ParallelDownloader = 100
34+ ParallelDownloader = 10
3335)
3436
3537// Zipper implements ZipBuilder interface
@@ -111,8 +113,9 @@ func (z *Zipper) buildPDFZip(claType string, claGroupID string) error {
111113 }
112114 var zipUpdated bool
113115 log .WithFields (f ).Debug ("getting s3 files" )
114- downloaderInputChan := make (chan * DownloadFileInput )
115- downloaderOutputChan := make (chan * FileContent )
116+ downloaderInputChan := make (chan * DownloadFileInput , 16 )
117+ downloaderOutputChan := make (chan * FileContent , 16 )
118+ listErrCh := make (chan error , 1 )
116119 var wg sync.WaitGroup
117120 wg .Add (ParallelDownloader )
118121 for i := 1 ; i <= ParallelDownloader ; i ++ {
@@ -123,7 +126,7 @@ func (z *Zipper) buildPDFZip(claType string, claGroupID string) error {
123126 close (downloaderOutputChan )
124127 }()
125128 go func () {
126- err = z .s3 .ListObjectsPages (& s3.ListObjectsInput {
129+ localErr : = z .s3 .ListObjectsPages (& s3.ListObjectsInput {
127130 Bucket : aws .String (z .bucketName ),
128131 Prefix : aws .String (s3ZipPrefix (claType , claGroupID )),
129132 }, func (output * s3.ListObjectsOutput , b bool ) bool {
@@ -147,12 +150,19 @@ func (z *Zipper) buildPDFZip(claType string, claGroupID string) error {
147150 return true
148151 })
149152 close (downloaderInputChan )
153+ listErrCh <- localErr
150154 }()
151155 zipUpdated = writeFileToZip (writer , downloaderOutputChan )
152- if err != nil {
153- return err
156+ if listErr := <- listErrCh ; listErr != nil {
157+ if cerr := writer .Close (); cerr != nil {
158+ log .Warnf ("zip writer close failed: %v" , cerr )
159+ return errors .Join (listErr , cerr )
160+ }
161+ return listErr
162+ }
163+ if cerr := writer .Close (); cerr != nil {
164+ return cerr
154165 }
155- writer .Close ()
156166 if zipUpdated {
157167 remoteZipFileKey := s3ZipFilepath (claType , claGroupID )
158168 log .Debugf ("Uploading zip file %s" , remoteZipFileKey )
@@ -264,13 +274,16 @@ func getZipWriter(buff *bytes.Buffer) (*zip.Writer, error) {
264274func (z * Zipper ) getZipFileFromS3 (claType string , claGroupID string ) (* bytes.Buffer , error ) {
265275 var buff aws.WriteAtBuffer
266276 remoteFileKey := s3ZipFilepath (claType , claGroupID )
267- _ , err := z .s3 .GetObject (& s3.GetObjectInput {
277+ _ , err := z .s3 .HeadObject (& s3.HeadObjectInput {
268278 Bucket : aws .String (z .bucketName ),
269279 Key : aws .String (remoteFileKey ),
270280 })
271281 if err != nil {
272- aerr , ok := err .(awserr.Error )
273- if ok && aerr .Code () == s3 .ErrCodeNoSuchKey {
282+ if rf , ok := err .(awserr.RequestFailure ); ok && rf .StatusCode () == http .StatusNotFound {
283+ log .Debugf ("zip file %s does not exist on s3" , remoteFileKey )
284+ return bytes .NewBuffer (buff .Bytes ()), nil
285+ }
286+ if aerr , ok := err .(awserr.Error ); ok && (aerr .Code () == s3 .ErrCodeNoSuchKey || aerr .Code () == "NotFound" ) {
274287 log .Debugf ("zip file %s does not exist on s3" , remoteFileKey )
275288 return bytes .NewBuffer (buff .Bytes ()), nil
276289 }
0 commit comments