-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathbandwidthlimiter.go
More file actions
61 lines (51 loc) · 1.38 KB
/
bandwidthlimiter.go
File metadata and controls
61 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package bandwidthlimiter
import (
"io"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/interop"
)
func BandwidthLimitingCopy(dst *BandwidthLimitingWriter, src io.Reader) (written int64, err error) {
written, err = io.Copy(dst, src)
_ = dst.Close()
return
}
func NewBandwidthLimitingWriter(w io.Writer, bucket *Bucket) (*BandwidthLimitingWriter, error) {
throttler, err := NewThrottler(bucket)
if err != nil {
return nil, err
}
return &BandwidthLimitingWriter{w: w, th: throttler}, nil
}
type BandwidthLimitingWriter struct {
w io.Writer
th *Throttler
}
func (w *BandwidthLimitingWriter) ChunkedWrite(p []byte) (n int, err error) {
i := NewChunkIterator(p, int(w.th.b.capacity))
for {
buf := i.Next()
if buf == nil {
return
}
written, writeErr := w.th.bandwidthLimitingWrite(w.w, buf)
n += written
if writeErr != nil {
return n, writeErr
}
}
}
func (w *BandwidthLimitingWriter) Write(p []byte) (n int, err error) {
w.th.start()
if int64(len(p)) > w.th.b.capacity {
return w.ChunkedWrite(p)
}
return w.th.bandwidthLimitingWrite(w.w, p)
}
func (w *BandwidthLimitingWriter) Close() (err error) {
w.th.stop()
return
}
func (w *BandwidthLimitingWriter) GetMetrics() (metrics *interop.InvokeResponseMetrics) {
return w.th.metrics
}