restriction_manager.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  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. package baggage
  15. const (
  16. defaultMaxValueLength = 2048
  17. )
  18. // Restriction determines whether a baggage key is allowed and contains any restrictions on the baggage value.
  19. type Restriction struct {
  20. keyAllowed bool
  21. maxValueLength int
  22. }
  23. // NewRestriction returns a new Restriction.
  24. func NewRestriction(keyAllowed bool, maxValueLength int) *Restriction {
  25. return &Restriction{
  26. keyAllowed: keyAllowed,
  27. maxValueLength: maxValueLength,
  28. }
  29. }
  30. // KeyAllowed returns whether the baggage key for this restriction is allowed.
  31. func (r *Restriction) KeyAllowed() bool {
  32. return r.keyAllowed
  33. }
  34. // MaxValueLength returns the max length for the baggage value.
  35. func (r *Restriction) MaxValueLength() int {
  36. return r.maxValueLength
  37. }
  38. // RestrictionManager keeps track of valid baggage keys and their restrictions. The manager
  39. // will return a Restriction for a specific baggage key which will determine whether the baggage
  40. // key is allowed for the current service and any other applicable restrictions on the baggage
  41. // value.
  42. type RestrictionManager interface {
  43. GetRestriction(service, key string) *Restriction
  44. }
  45. // DefaultRestrictionManager allows any baggage key.
  46. type DefaultRestrictionManager struct {
  47. defaultRestriction *Restriction
  48. }
  49. // NewDefaultRestrictionManager returns a DefaultRestrictionManager.
  50. func NewDefaultRestrictionManager(maxValueLength int) *DefaultRestrictionManager {
  51. if maxValueLength == 0 {
  52. maxValueLength = defaultMaxValueLength
  53. }
  54. return &DefaultRestrictionManager{
  55. defaultRestriction: &Restriction{keyAllowed: true, maxValueLength: maxValueLength},
  56. }
  57. }
  58. // GetRestriction implements RestrictionManager#GetRestriction.
  59. func (m *DefaultRestrictionManager) GetRestriction(service, key string) *Restriction {
  60. return m.defaultRestriction
  61. }