-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
119 lines (102 loc) · 3.62 KB
/
writer.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2020 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package speedio
import (
"io"
"time"
"github.com/tunabay/go-infounit"
)
// Writer implements both bit rate limiting and bit rate measurement for an io.Writer object.
// If only one of limiting or measurement is needed, it is recommended to use
// LimiterWriter or MeterWriter instead.
// In fact, Writer is just a concatenation of LimiterWriter and MeterWriter.
type Writer struct {
mw *MeterWriter
lw *LimiterWriter
}
// NewWriter creates a new Writer with default configurations.
func NewWriter(wr io.Writer, rate infounit.BitRate) (*Writer, error) {
return NewWriterWithConfig(wr, rate, nil, nil)
}
// NewWriterWithConfig creates a new Writer with the specified
// configurations. If conf is nil, the default configuration will be used.
func NewWriterWithConfig(wr io.Writer, rate infounit.BitRate, lconf *LimiterConfig, mconf *MeterConfig) (*Writer, error) {
mw, err := NewMeterWriterWithConfig(wr, mconf)
if err != nil {
return nil, err
}
lw, err := NewLimiterWriterWithConfig(mw, rate, lconf)
if err != nil {
return nil, err
}
return &Writer{mw: mw, lw: lw}, nil
}
// LimitingBitRate returns the current effective limiting bit rate.
func (w *Writer) LimitingBitRate() infounit.BitRate {
return w.lw.LimitingBitRate()
}
// SetBitRate sets a new bit rate limiting.
func (w *Writer) SetBitRate(rate infounit.BitRate) error {
return w.lw.SetBitRate(rate)
}
// Close closes the writer.
// If the underlying writer implements io.WriteCloser, its Close method
// is also called.
func (w *Writer) Close() error {
return w.lw.Close()
}
// CloseAt is the same as Close, except that it uses time specified as the end
// time.
func (w *Writer) CloseAt(tc time.Time) error {
if err := w.mw.CloseAt(tc); err != nil {
_ = w.lw.CloseSingle()
return err
}
return w.lw.CloseSingle()
}
// CloseSingle is the same as Close except that it does not close the underlying writer.
func (w *Writer) CloseSingle() error {
if err := w.mw.CloseSingle(); err != nil {
_ = w.lw.CloseSingle()
return err
}
return w.lw.CloseSingle()
}
// CloseSingleAt is the same as CloseAt except that it does not close the underlying writer.
func (w *Writer) CloseSingleAt(tc time.Time) error {
if err := w.mw.CloseSingleAt(tc); err != nil {
_ = w.lw.CloseSingle()
return err
}
return w.lw.CloseSingle()
}
// Write writes len(p) bytes from p to the underlying writer.
// It blocks until all the data in p is written, but it does not just wait
// until all the data is written. It repeatedly writes part of the divided p
// to the underlying writer.
func (w *Writer) Write(p []byte) (int, error) {
return w.lw.Write(p)
}
// Start starts the measurement. Calling this Start is optional, and normally it
// is started automatically at the first write. This is used to adjust the
// transfer start time for bit rate calculation.
func (w *Writer) Start() {
w.mw.Start()
}
// StartAt starts the measurement at specified time. This is used to adjust the
// transfer start time for bit rate calculation.
func (w *Writer) StartAt(tc time.Time) {
w.mw.StartAt(tc)
}
// BitRate calculates and returns the bit rate in the most recent sampling
// period.
func (w *Writer) BitRate() infounit.BitRate {
return w.mw.BitRate()
}
// Total returns the data transfer amount, elapsed time, and bit rate in the
// entire period from start. When it is called after being closed, it always
// returns the same statistics from start to close.
func (w *Writer) Total() (infounit.ByteCount, time.Duration, infounit.BitRate) {
return w.mw.Total()
}