storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/bandwidth.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 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  
    18  package madmin
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"net/http"
    24  	"net/url"
    25  	"strings"
    26  
    27  	"storj.io/minio/pkg/bandwidth"
    28  )
    29  
    30  // Report includes the bandwidth report or the error encountered.
    31  type Report struct {
    32  	Report bandwidth.Report `json:"report"`
    33  	Err    error            `json:"error,omitempty"`
    34  }
    35  
    36  // GetBucketBandwidth - Gets a channel reporting bandwidth measurements for replication buckets. If no buckets
    37  // generate replication traffic an empty map is returned in the report until traffic is seen.
    38  func (adm *AdminClient) GetBucketBandwidth(ctx context.Context, buckets ...string) <-chan Report {
    39  	queryValues := url.Values{}
    40  	ch := make(chan Report)
    41  	if len(buckets) > 0 {
    42  		queryValues.Set("buckets", strings.Join(buckets, ","))
    43  	}
    44  
    45  	reqData := requestData{
    46  		relPath:     adminAPIPrefix + "/bandwidth",
    47  		queryValues: queryValues,
    48  	}
    49  	resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
    50  	if err != nil {
    51  		defer closeResponse(resp)
    52  		ch <- Report{bandwidth.Report{}, err}
    53  		return ch
    54  	}
    55  	if resp.StatusCode != http.StatusOK {
    56  		ch <- Report{bandwidth.Report{}, httpRespToErrorResponse(resp)}
    57  		return ch
    58  	}
    59  
    60  	dec := json.NewDecoder(resp.Body)
    61  
    62  	go func(ctx context.Context, ch chan<- Report, resp *http.Response) {
    63  		defer func() {
    64  			closeResponse(resp)
    65  			close(ch)
    66  		}()
    67  		for {
    68  			var report bandwidth.Report
    69  
    70  			if err = dec.Decode(&report); err != nil {
    71  				ch <- Report{bandwidth.Report{}, err}
    72  				return
    73  			}
    74  			select {
    75  			case <-ctx.Done():
    76  				return
    77  			case ch <- Report{Report: report, Err: err}:
    78  			}
    79  		}
    80  	}(ctx, ch, resp)
    81  	return ch
    82  }