github.com/minio/madmin-go@v1.7.5/bandwidth.go (about)

     1  //
     2  // MinIO Object Storage (c) 2021 MinIO, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package madmin
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/url"
    24  	"strings"
    25  )
    26  
    27  // BandwidthDetails for the measured bandwidth
    28  type BandwidthDetails struct {
    29  	LimitInBytesPerSecond            int64   `json:"limitInBits"`
    30  	CurrentBandwidthInBytesPerSecond float64 `json:"currentBandwidth"`
    31  }
    32  
    33  // BucketBandwidthReport captures the details for all buckets.
    34  type BucketBandwidthReport struct {
    35  	BucketStats map[string]BandwidthDetails `json:"bucketStats,omitempty"`
    36  }
    37  
    38  // Report includes the bandwidth report or the error encountered.
    39  type Report struct {
    40  	Report BucketBandwidthReport `json:"report"`
    41  	Err    error                 `json:"error,omitempty"`
    42  }
    43  
    44  // GetBucketBandwidth - Gets a channel reporting bandwidth measurements for replication buckets. If no buckets
    45  // generate replication traffic an empty map is returned in the report until traffic is seen.
    46  func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...string) <-chan Report {
    47  	queryValues := url.Values{}
    48  	ch := make(chan Report)
    49  	if len(buckets) > 0 {
    50  		queryValues.Set("buckets", strings.Join(buckets, ","))
    51  	}
    52  
    53  	reqData := requestData{
    54  		relPath:     adminAPIPrefix + "/bandwidth",
    55  		queryValues: queryValues,
    56  	}
    57  	resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
    58  	if err != nil {
    59  		defer closeResponse(resp)
    60  		ch <- Report{Err: err}
    61  		return ch
    62  	}
    63  	if resp.StatusCode != http.StatusOK {
    64  		ch <- Report{Err: httpRespToErrorResponse(resp)}
    65  		return ch
    66  	}
    67  
    68  	dec := json.NewDecoder(resp.Body)
    69  
    70  	go func(ctx context.Context, ch chan<- Report, resp *http.Response) {
    71  		defer func() {
    72  			closeResponse(resp)
    73  			close(ch)
    74  		}()
    75  		for {
    76  			var report BucketBandwidthReport
    77  
    78  			if err = dec.Decode(&report); err != nil {
    79  				ch <- Report{Err: err}
    80  				return
    81  			}
    82  			select {
    83  			case <-ctx.Done():
    84  				return
    85  			case ch <- Report{Report: report}:
    86  			}
    87  		}
    88  	}(ctx, ch, resp)
    89  	return ch
    90  }