load.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // Package balancerload defines APIs to parse server loads in trailers. The
  17. // parsed loads are sent to balancers in DoneInfo.
  18. package balancerload
  19. import (
  20. "google.golang.org/grpc/metadata"
  21. )
  22. // Parser converts loads from metadata into a concrete type.
  23. type Parser interface {
  24. // Parse parses loads from metadata.
  25. Parse(md metadata.MD) interface{}
  26. }
  27. var parser Parser
  28. // SetParser sets the load parser.
  29. //
  30. // Not mutex-protected, should be called before any gRPC functions.
  31. func SetParser(lr Parser) {
  32. parser = lr
  33. }
  34. // Parse calls parser.Read().
  35. func Parse(md metadata.MD) interface{} {
  36. if parser == nil {
  37. return nil
  38. }
  39. return parser.Parse(md)
  40. }