You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.2 KiB

  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 edsbalancer
  17. import (
  18. "sync"
  19. )
  20. type dropper struct {
  21. // Drop rate will be numerator/denominator.
  22. numerator uint32
  23. denominator uint32
  24. mu sync.Mutex
  25. i uint32
  26. }
  27. func newDropper(numerator, denominator uint32) *dropper {
  28. return &dropper{
  29. numerator: numerator,
  30. denominator: denominator,
  31. }
  32. }
  33. func (d *dropper) drop() (ret bool) {
  34. d.mu.Lock()
  35. defer d.mu.Unlock()
  36. // TODO: the drop algorithm needs a design.
  37. // Currently, for drop rate 3/5:
  38. // 0 1 2 3 4
  39. // d d d n n
  40. if d.i < d.numerator {
  41. ret = true
  42. }
  43. d.i++
  44. if d.i >= d.denominator {
  45. d.i = 0
  46. }
  47. return
  48. }