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
17package edsbalancer
18
19import (
20	"google.golang.org/grpc/internal/wrr"
21	xdsclient "google.golang.org/grpc/xds/internal/client"
22)
23
24var newRandomWRR = wrr.NewRandom
25
26type dropper struct {
27	c xdsclient.OverloadDropConfig
28	w wrr.WRR
29}
30
31func newDropper(c xdsclient.OverloadDropConfig) *dropper {
32	w := newRandomWRR()
33	w.Add(true, int64(c.Numerator))
34	w.Add(false, int64(c.Denominator-c.Numerator))
35
36	return &dropper{
37		c: c,
38		w: w,
39	}
40}
41
42func (d *dropper) drop() (ret bool) {
43	return d.w.Next().(bool)
44}
45