2020-10-09 23:36:00 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* 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 bandwidth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MonitoredReader monitors the bandwidth
|
|
|
|
type MonitoredReader struct {
|
2021-04-05 19:07:53 -04:00
|
|
|
opts *MonitorReaderOptions
|
2020-10-09 23:36:00 -04:00
|
|
|
bucketMeasurement *bucketMeasurement // bucket measurement object
|
2021-04-05 19:07:53 -04:00
|
|
|
reader io.Reader // Reader to wrap
|
2020-10-09 23:36:00 -04:00
|
|
|
lastStop time.Time // Last timestamp for a measurement
|
|
|
|
throttle *throttle // throttle the rate at which replication occur
|
|
|
|
monitor *Monitor // Monitor reference
|
2021-04-05 19:07:53 -04:00
|
|
|
lastErr error // last error reported, if this non-nil all reads will fail.
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:07:53 -04:00
|
|
|
// MonitorReaderOptions provides configurable options for monitor reader implementation.
|
|
|
|
type MonitorReaderOptions struct {
|
|
|
|
Bucket string
|
|
|
|
Object string
|
|
|
|
HeaderSize int
|
|
|
|
BandwidthBytesPerSec int64
|
|
|
|
ClusterBandwidth int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMonitoredReader returns a io.Reader that reports bandwidth details.
|
|
|
|
func NewMonitoredReader(ctx context.Context, monitor *Monitor, reader io.Reader, opts *MonitorReaderOptions) *MonitoredReader {
|
2020-10-09 23:36:00 -04:00
|
|
|
timeNow := time.Now()
|
2021-04-05 19:07:53 -04:00
|
|
|
b := monitor.track(opts.Bucket, opts.Object, timeNow)
|
2020-10-09 23:36:00 -04:00
|
|
|
return &MonitoredReader{
|
2021-04-05 19:07:53 -04:00
|
|
|
opts: opts,
|
2020-10-09 23:36:00 -04:00
|
|
|
bucketMeasurement: b,
|
|
|
|
reader: reader,
|
|
|
|
lastStop: timeNow,
|
2021-04-05 19:07:53 -04:00
|
|
|
throttle: monitor.throttleBandwidth(ctx, opts.Bucket, opts.BandwidthBytesPerSec, opts.ClusterBandwidth),
|
2020-10-09 23:36:00 -04:00
|
|
|
monitor: monitor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read wraps the read reader
|
|
|
|
func (m *MonitoredReader) Read(p []byte) (n int, err error) {
|
2021-04-05 19:07:53 -04:00
|
|
|
if m.lastErr != nil {
|
|
|
|
err = m.lastErr
|
2020-10-09 23:36:00 -04:00
|
|
|
return
|
|
|
|
}
|
2021-04-05 19:07:53 -04:00
|
|
|
|
2020-10-09 23:36:00 -04:00
|
|
|
p = p[:m.throttle.GetLimitForBytes(int64(len(p)))]
|
|
|
|
|
|
|
|
n, err = m.reader.Read(p)
|
|
|
|
stop := time.Now()
|
2021-04-05 19:07:53 -04:00
|
|
|
update := uint64(n + m.opts.HeaderSize)
|
2020-10-09 23:36:00 -04:00
|
|
|
|
|
|
|
m.bucketMeasurement.incrementBytes(update)
|
|
|
|
m.lastStop = stop
|
2021-04-05 19:07:53 -04:00
|
|
|
unused := len(p) - (n + m.opts.HeaderSize)
|
|
|
|
m.opts.HeaderSize = 0 // Set to 0 post first read
|
2020-10-09 23:36:00 -04:00
|
|
|
|
|
|
|
if unused > 0 {
|
|
|
|
m.throttle.ReleaseUnusedBandwidth(int64(unused))
|
|
|
|
}
|
2021-04-05 19:07:53 -04:00
|
|
|
if err != nil {
|
|
|
|
m.lastErr = err
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2021-04-05 19:07:53 -04:00
|
|
|
return
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|