forked from porjo/youtubeuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter.go
177 lines (140 loc) · 3.82 KB
/
limiter.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"strings"
"time"
"golang.org/x/time/rate"
)
type limitRange struct {
start time.Time
end time.Time
}
type limitChecker struct {
lr limitRange
reader io.ReadCloser
rl *rate.Limiter
Monitor monitor
}
type monitor struct {
lastRead time.Time
start time.Time
size int64
status status
}
type status struct {
AvgRate int64
Bytes int64
TimeRem time.Duration
Progress string
}
const bucketSize = 1000
func NewLimitChecker(lr limitRange, r io.ReadCloser) *limitChecker {
lc := &limitChecker{lr: lr, reader: r}
return lc
}
func (m *monitor) Status() status {
return m.status
}
func (lc *limitChecker) Read(p []byte) (int, error) {
var err error
var read int
limit := false
if lc.Monitor.start.IsZero() {
lc.Monitor.start = time.Now()
}
if *rateLimit > 0 {
if lc.rl == nil {
lc.rl = rate.NewLimiter(rate.Limit(*rateLimit*125), bucketSize)
}
if lc.lr.start.IsZero() || lc.lr.end.IsZero() {
limit = true
} else {
if time.Since(lc.lr.start) >= time.Hour*24 {
lc.lr.start = lc.lr.start.AddDate(0, 0, 1)
lc.lr.end = lc.lr.end.AddDate(0, 0, 1)
}
now := time.Now()
if lc.lr.start.Before(now) && lc.lr.end.After(now) {
limit = true
} else {
limit = false
}
}
}
if limit {
tokens := bucketSize
if len(p) < bucketSize {
tokens = len(p)
}
for {
var readL int
err = lc.rl.WaitN(context.Background(), tokens)
if err != nil {
break
}
readL, err = lc.reader.Read(p[read : read+tokens])
read += readL
if err != nil {
break
}
if read == len(p) {
break
}
if read+tokens > len(p) {
tokens = len(p) - read
}
}
} else {
read, err = lc.reader.Read(p)
}
lc.Monitor.status.Bytes += int64(read)
// bytes read will be greater than filesize due to HTTP headers etc, so reset to filesize
if lc.Monitor.status.Bytes > lc.Monitor.size {
lc.Monitor.status.Bytes = lc.Monitor.size
}
lc.Monitor.status.Progress = fmt.Sprintf("%.2f%", float64(lc.Monitor.status.Bytes)/float64(lc.Monitor.size)*100)
lc.Monitor.status.AvgRate = int64(float64(lc.Monitor.status.Bytes) / time.Since(lc.Monitor.start).Seconds())
lc.Monitor.status.TimeRem = time.Duration(float64(lc.Monitor.size-lc.Monitor.status.Bytes)/float64(lc.Monitor.status.AvgRate)) * time.Second
return read, err
}
func (lc *limitChecker) Close() error {
return nil
}
func parseLimitBetween(between string) (limitRange, error) {
var lr limitRange
var err error
var start, end time.Time
parts := strings.Split(between, "-")
if len(parts) != 2 {
return lr, fmt.Errorf("limitBetween should have 2 parts separated by a hyphen")
}
now := time.Now()
start, err = time.ParseInLocation(inputTimeLayout, parts[0], time.Local)
if err != nil {
return lr, fmt.Errorf("limitBetween start time was invalid: %v", err)
}
lr.start = time.Date(now.Year(), now.Month(), now.Day(), start.Hour(), start.Minute(), 0, 0, time.Local)
end, err = time.ParseInLocation(inputTimeLayout, parts[1], time.Local)
if err != nil {
return lr, fmt.Errorf("limitBetween end time was invalid: %v", err)
}
lr.end = time.Date(now.Year(), now.Month(), now.Day(), end.Hour(), end.Minute(), 0, 0, time.Local)
// handle range spanning midnight
if lr.end.Before(lr.start) {
lr.end = lr.end.AddDate(0, 0, 1)
}
return lr, nil
}