dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/balancer/orca/orca.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  /*
    19   *
    20   * Copyright 2019 gRPC authors.
    21   *
    22   */
    23  
    24  // Package orca implements Open Request Cost Aggregation.
    25  package orca
    26  
    27  import (
    28  	orcapb "github.com/cncf/xds/go/xds/data/orca/v3"
    29  
    30  	"github.com/dubbogo/gost/log/logger"
    31  
    32  	"github.com/golang/protobuf/proto"
    33  
    34  	"google.golang.org/grpc/metadata"
    35  )
    36  
    37  import (
    38  	"dubbo.apache.org/dubbo-go/v3/xds/utils/balancerload"
    39  )
    40  
    41  const mdKey = "X-Endpoint-Load-Metrics-Bin"
    42  
    43  // toBytes converts a orca load report into bytes.
    44  func toBytes(r *orcapb.OrcaLoadReport) []byte {
    45  	if r == nil {
    46  		return nil
    47  	}
    48  
    49  	b, err := proto.Marshal(r)
    50  	if err != nil {
    51  		logger.Warnf("orca: failed to marshal load report: %v", err)
    52  		return nil
    53  	}
    54  	return b
    55  }
    56  
    57  // ToMetadata converts a orca load report into grpc metadata.
    58  func ToMetadata(r *orcapb.OrcaLoadReport) metadata.MD {
    59  	b := toBytes(r)
    60  	if b == nil {
    61  		return nil
    62  	}
    63  	return metadata.Pairs(mdKey, string(b))
    64  }
    65  
    66  // fromBytes reads load report bytes and converts it to orca.
    67  func fromBytes(b []byte) *orcapb.OrcaLoadReport {
    68  	ret := new(orcapb.OrcaLoadReport)
    69  	if err := proto.Unmarshal(b, ret); err != nil {
    70  		logger.Warnf("orca: failed to unmarshal load report: %v", err)
    71  		return nil
    72  	}
    73  	return ret
    74  }
    75  
    76  // FromMetadata reads load report from metadata and converts it to orca.
    77  //
    78  // It returns nil if report is not found in metadata.
    79  func FromMetadata(md metadata.MD) *orcapb.OrcaLoadReport {
    80  	vs := md.Get(mdKey)
    81  	if len(vs) == 0 {
    82  		return nil
    83  	}
    84  	return fromBytes([]byte(vs[0]))
    85  }
    86  
    87  type loadParser struct{}
    88  
    89  func (*loadParser) Parse(md metadata.MD) interface{} {
    90  	return FromMetadata(md)
    91  }
    92  
    93  func init() {
    94  	balancerload.SetParser(&loadParser{})
    95  }