github.com/minio/madmin-go/v2@v2.2.1/bandwidth.go (about) 1 // 2 // Copyright (c) 2015-2022 MinIO, Inc. 3 // 4 // This file is part of MinIO Object Storage stack 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU Affero General Public License as 8 // published by the Free Software Foundation, either version 3 of the 9 // License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU Affero General Public License for more details. 15 // 16 // You should have received a copy of the GNU Affero General Public License 17 // along with this program. If not, see <http://www.gnu.org/licenses/>. 18 // 19 20 package madmin 21 22 import ( 23 "context" 24 "encoding/json" 25 "net/http" 26 "net/url" 27 "strings" 28 ) 29 30 // BandwidthDetails for the measured bandwidth 31 type BandwidthDetails struct { 32 LimitInBytesPerSecond int64 `json:"limitInBits"` 33 CurrentBandwidthInBytesPerSecond float64 `json:"currentBandwidth"` 34 } 35 36 // BucketBandwidthReport captures the details for all buckets. 37 type BucketBandwidthReport struct { 38 BucketStats map[string]BandwidthDetails `json:"bucketStats,omitempty"` 39 } 40 41 // Report includes the bandwidth report or the error encountered. 42 type Report struct { 43 Report BucketBandwidthReport `json:"report"` 44 Err error `json:"error,omitempty"` 45 } 46 47 // GetBucketBandwidth - Gets a channel reporting bandwidth measurements for replication buckets. If no buckets 48 // generate replication traffic an empty map is returned in the report until traffic is seen. 49 func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...string) <-chan Report { 50 queryValues := url.Values{} 51 ch := make(chan Report) 52 if len(buckets) > 0 { 53 queryValues.Set("buckets", strings.Join(buckets, ",")) 54 } 55 56 reqData := requestData{ 57 relPath: adminAPIPrefix + "/bandwidth", 58 queryValues: queryValues, 59 } 60 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 61 if err != nil { 62 defer closeResponse(resp) 63 ch <- Report{Err: err} 64 return ch 65 } 66 if resp.StatusCode != http.StatusOK { 67 ch <- Report{Err: httpRespToErrorResponse(resp)} 68 return ch 69 } 70 71 dec := json.NewDecoder(resp.Body) 72 73 go func(ctx context.Context, ch chan<- Report, resp *http.Response) { 74 defer func() { 75 closeResponse(resp) 76 close(ch) 77 }() 78 for { 79 var report BucketBandwidthReport 80 81 if err = dec.Decode(&report); err != nil { 82 ch <- Report{Err: err} 83 return 84 } 85 select { 86 case <-ctx.Done(): 87 return 88 case ch <- Report{Report: report}: 89 } 90 } 91 }(ctx, ch, resp) 92 return ch 93 }