go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/svcmux/svcmux.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package svcmux contains utility functions used by code generated by 16 // svcmux tool. 17 // It is not designed to be used directly. 18 package svcmux 19 20 import ( 21 "context" 22 23 "google.golang.org/grpc/codes" 24 "google.golang.org/grpc/metadata" 25 "google.golang.org/grpc/status" 26 ) 27 28 // VersionMetadataKey is key in gRPC metadata that specifies requested 29 // service version. 30 const VersionMetadataKey = "X-Luci-Service-Version" 31 32 // ServiceVersion extracts requested service version from metadata in ctx. 33 func ServiceVersion(ctx context.Context, defaultVer string) string { 34 if md, ok := metadata.FromIncomingContext(ctx); ok { 35 values := md[VersionMetadataKey] 36 if len(values) != 0 { 37 return values[0] 38 } 39 } 40 return defaultVer 41 } 42 43 // NoImplementation creates an error for a service version that does not have an 44 // implementation. 45 func NoImplementation(version string) error { 46 return status.Errorf(codes.Unimplemented, "version %s not implemented", version) 47 }