| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
|
| 7 |
"github.com/ProtonMail/gluon/async" |
| 8 |
"github.com/bradenaw/juniper/parallel" |
| 9 |
) |
| 10 |
|
| 11 |
// AttachmentAllocator abstract the attachment download buffer creation. |
| 12 |
type AttachmentAllocator interface { |
| 13 |
// NewBuffer should return a new byte buffer for use. Note that this function may be called from multiple go-routines. |
| 14 |
NewBuffer() *bytes.Buffer |
| 15 |
} |
| 16 |
|
| 17 |
type DefaultAttachmentAllocator struct{} |
| 18 |
|
| 19 |
func NewDefaultAttachmentAllocator() *DefaultAttachmentAllocator { |
| 20 |
return &DefaultAttachmentAllocator{} |
| 21 |
} |
| 22 |
|
| 23 |
func (DefaultAttachmentAllocator) NewBuffer() *bytes.Buffer { |
| 24 |
return bytes.NewBuffer(nil) |
| 25 |
} |
| 26 |
|
| 27 |
// Scheduler allows the user to specify how the attachment data for the message should be downloaded. |
| 28 |
type Scheduler interface { |
| 29 |
Schedule(ctx context.Context, attachmentIDs []string, storageProvider AttachmentAllocator, downloader func(context.Context, string, *bytes.Buffer) error) ([]*bytes.Buffer, error) |
| 30 |
} |
| 31 |
|
| 32 |
// SequentialScheduler downloads the attachments one by one. |
| 33 |
type SequentialScheduler struct{} |
| 34 |
|
| 35 |
func NewSequentialScheduler() *SequentialScheduler { |
| 36 |
return &SequentialScheduler{} |
| 37 |
} |
| 38 |
|
| 39 |
func (SequentialScheduler) Schedule(ctx context.Context, attachmentIDs []string, storageProvider AttachmentAllocator, downloader func(context.Context, string, *bytes.Buffer) error) ([]*bytes.Buffer, error) { |
| 40 |
result := make([]*bytes.Buffer, len(attachmentIDs)) |
| 41 |
for i, v := range attachmentIDs { |
| 42 |
|
| 43 |
select { |
| 44 |
case <-ctx.Done(): |
| 45 |
return nil, ctx.Err() |
| 46 |
default: |
| 47 |
} |
| 48 |
|
| 49 |
buffer := storageProvider.NewBuffer() |
| 50 |
if err := downloader(ctx, v, buffer); err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
|
| 54 |
result[i] = buffer |
| 55 |
} |
| 56 |
|
| 57 |
return result, nil |
| 58 |
} |
| 59 |
|
| 60 |
type ParallelScheduler struct { |
| 61 |
workers int |
| 62 |
panicHandler async.PanicHandler |
| 63 |
} |
| 64 |
|
| 65 |
func NewParallelScheduler(workers int, panicHandler async.PanicHandler) *ParallelScheduler { |
| 66 |
if workers == 0 { |
| 67 |
workers = 1 |
| 68 |
} |
| 69 |
|
| 70 |
return &ParallelScheduler{workers: workers} |
| 71 |
} |
| 72 |
|
| 73 |
func (p ParallelScheduler) Schedule(ctx context.Context, attachmentIDs []string, storageProvider AttachmentAllocator, downloader func(context.Context, string, *bytes.Buffer) error) ([]*bytes.Buffer, error) { |
| 74 |
// If we have less attachments than the maximum works, reduce worker count to match attachment count. |
| 75 |
workers := p.workers |
| 76 |
if len(attachmentIDs) < workers { |
| 77 |
workers = len(attachmentIDs) |
| 78 |
} |
| 79 |
|
| 80 |
return parallel.MapContext(ctx, workers, attachmentIDs, func(ctx context.Context, id string) (*bytes.Buffer, error) { |
| 81 |
defer async.HandlePanic(p.panicHandler) |
| 82 |
|
| 83 |
buffer := storageProvider.NewBuffer() |
| 84 |
if err := downloader(ctx, id, buffer); err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
return buffer, nil |
| 89 |
}) |
| 90 |
|
| 91 |
} |