github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/orca/orca.go (about) 1 /* 2 * Copyright 2019 gRPC authors. 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 orca implements Open Request Cost Aggregation. 18 package orca 19 20 import ( 21 orcapb "github.com/cncf/xds/go/xds/data/orca/v3" 22 "github.com/golang/protobuf/proto" 23 "github.com/hxx258456/ccgo/grpc/grpclog" 24 "github.com/hxx258456/ccgo/grpc/internal/balancerload" 25 "github.com/hxx258456/ccgo/grpc/metadata" 26 ) 27 28 const mdKey = "X-Endpoint-Load-Metrics-Bin" 29 30 var logger = grpclog.Component("xds") 31 32 // toBytes converts a orca load report into bytes. 33 func toBytes(r *orcapb.OrcaLoadReport) []byte { 34 if r == nil { 35 return nil 36 } 37 38 b, err := proto.Marshal(r) 39 if err != nil { 40 logger.Warningf("orca: failed to marshal load report: %v", err) 41 return nil 42 } 43 return b 44 } 45 46 // ToMetadata converts a orca load report into grpc metadata. 47 func ToMetadata(r *orcapb.OrcaLoadReport) metadata.MD { 48 b := toBytes(r) 49 if b == nil { 50 return nil 51 } 52 return metadata.Pairs(mdKey, string(b)) 53 } 54 55 // fromBytes reads load report bytes and converts it to orca. 56 func fromBytes(b []byte) *orcapb.OrcaLoadReport { 57 ret := new(orcapb.OrcaLoadReport) 58 if err := proto.Unmarshal(b, ret); err != nil { 59 logger.Warningf("orca: failed to unmarshal load report: %v", err) 60 return nil 61 } 62 return ret 63 } 64 65 // FromMetadata reads load report from metadata and converts it to orca. 66 // 67 // It returns nil if report is not found in metadata. 68 func FromMetadata(md metadata.MD) *orcapb.OrcaLoadReport { 69 vs := md.Get(mdKey) 70 if len(vs) == 0 { 71 return nil 72 } 73 return fromBytes([]byte(vs[0])) 74 } 75 76 type loadParser struct{} 77 78 func (*loadParser) Parse(md metadata.MD) interface{} { 79 return FromMetadata(md) 80 } 81 82 func init() { 83 balancerload.SetParser(&loadParser{}) 84 }