// RoundRobin is a roundrobin strategy algorithm for node selection
495770
func RoundRobin(services []*registry.Service) Next {
	var nodes []*registry.Node

	for _, service := range services {
		nodes = append(nodes, service.Nodes...)
	}

	var i = rand.Int()
	var mtx sync.Mutex

	return func() (*registry.Node, error) {
		if len(nodes) == 0 {
			return nil, ErrNoneAvailable
		}

		mtx.Lock()
		node := nodes[i%len(nodes)]
		i++
		mtx.Unlock()

		return node, nil
	}
}
634245
func (t *instrumentedHTTPRoundTripper) RoundTrip(origReq *http.Request) (*http.Response, error) {
	req := *origReq

	// If a request body was provided, wrap it in a CountingReader so we can see
	// how big it was after it's been sent.
	var requestBodyReader *iotools.CountingReader
	if req.Body != nil {
		requestBodyReader = &iotools.CountingReader{Reader: req.Body}
		req.Body = struct {
			io.Reader
			io.Closer
		}{requestBodyReader, req.Body}
	}

	start := clock.Now(t.ctx)
	resp, err := t.base.RoundTrip(&req)
	duration := clock.Now(t.ctx).Sub(start)

	var requestBytes int64
	if requestBodyReader != nil {
		requestBytes = requestBodyReader.Count
	}

	var code int
	var responseBytes int64
	if resp != nil {
		code = resp.StatusCode
		responseBytes = resp.ContentLength
	}

	UpdateHTTPMetrics(t.ctx, req.URL.Host, t.client, code, duration, requestBytes, responseBytes)

	return resp, err
}
1270920
func (t *TriremeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

	res, err := t.RoundTripper.RoundTrip(req)
	if err != nil || res == nil {
		return res, err
	}

	data := req.Context().Value(statsContextKey)
	if data == nil {
		return res, nil
	}

	state, ok := data.(*connectionState)
	if ok && state.cookie == nil {
		return res, nil
	}

	if v := state.cookie.String(); v != "" {
		res.Header.Add("Set-Cookie", v)
	}

	return res, nil
}
814538
func RoundTripperFor(config *restclient.Config) (http.RoundTripper, Upgrader, error) {
	tlsConfig, err := restclient.TLSConfigFor(config)
	if err != nil {
		return nil, nil, err
	}
	upgradeRoundTripper := spdy.NewRoundTripper(tlsConfig, true, false)
	wrapper, err := restclient.HTTPWrappersForConfig(config, upgradeRoundTripper)
	if err != nil {
		return nil, nil, err
	}
	return wrapper, upgradeRoundTripper, nil
}
93154
func (r *RoundRobin) Select(pool HostPool, request *http.Request) *UpstreamHost {
	poolLen := uint32(len(pool))
	r.mutex.Lock()
	defer r.mutex.Unlock()
	// Return next available host
	for i := uint32(0); i < poolLen; i++ {
		r.robin++
		host := pool[r.robin%poolLen]
		if host.Available() {
			return host
		}
	}
	return nil
}
629557
func (s *Selection) NextMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {
	if sel == nil {
		return s.NextMatcher(filter)
	}
	return s.NextMatcherUntilNodes(filter, sel.Nodes...)
}
238479
func RoundUpToMiBInt(size resource.Quantity) (int, error) {
	requestBytes := size.Value()
	return roundUpSizeInt(requestBytes, MiB)
}
1091306
func (t Term) Round(args ...interface{}) Term {
	return constructMethodTerm(t, "Round", p.Term_ROUND, args, map[string]interface{}{})
}
1105434
func (r *RoundRobin) AddEndpointWithOptions(endpoint endpoint.Endpoint, options EndpointOptions) error {
	r.mutex.Lock()
	defer r.mutex.Unlock()

	if endpoint == nil {
		return fmt.Errorf("Endpoint can't be nil")
	}

	if e, _ := r.findEndpointByUrl(endpoint.GetUrl()); e != nil {
		return fmt.Errorf("Endpoint already exists")
	}

	we, err := r.newWeightedEndpoint(endpoint, options)
	if err != nil {
		return err
	}

	r.endpoints = append(r.endpoints, we)
	r.resetState()
	return nil
}
647924
func (binder FastBinder) RoundTrip(stdreq *http.Request) (*http.Response, error) {
	fastreq := std2fast(stdreq)

	var conn net.Conn
	if stdreq.URL != nil && stdreq.URL.Scheme == "https" && binder.TLS != nil {
		conn = connTLS{state: binder.TLS}
	} else {
		conn = connNonTLS{}
	}

	ctx := fasthttp.RequestCtx{}
	ctx.Init2(conn, fastLogger{}, true)
	fastreq.CopyTo(&ctx.Request)

	if stdreq.ContentLength >= 0 {
		ctx.Request.Header.SetContentLength(int(stdreq.ContentLength))
	} else {
		ctx.Request.Header.Add("Transfer-Encoding", "chunked")
	}

	if stdreq.Body != nil {
		b, err := ioutil.ReadAll(stdreq.Body)
		if err == nil {
			ctx.Request.SetBody(b)
		}
	}

	binder.Handler(&ctx)

	return fast2std(stdreq, &ctx.Response), nil
}
251864
func (p *point) RoundedString(d time.Duration) string {
	if p.Time().IsZero() {
		return fmt.Sprintf("%s %s", p.Key(), string(p.fields))
	}
	return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields),
		p.time.Round(d).UnixNano())
}
337517
func RoundTripper(r http.RoundTripper) ClientOpt {
	return func(c *Client) {
		c.HTTPClient.Transport = r
	}
}
744254
func (c *Client) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
	return c.RoundTripper.RoundTrip(ctx, req, res)
}
1146637
func Round(value time.Duration, resolution time.Duration) time.Duration {
	if value < 0 {
		value -= resolution / 2
	} else {
		value += resolution / 2
	}
	return value / resolution * resolution
}
1040372
func round(d, r int64) int64 {
	neg := d < 0
	if neg {
		d = -d
	}
	if m := d % r; m+m < r {
		d = d - m
	} else {
		d = d + r - m
	}
	if neg {
		return -d
	}
	return d
}
453304
func roundString(b []byte, mode RoundingMode, pos bool, prec int) []byte {
	if prec >= len(b) {
		return append(b, bytes.Repeat(zero, prec-len(b))...)
	}

	// Trim zeros until prec. This is useful when we can round exactly by simply
	// chopping zeros off the end of the number.
	if allZeros(b[prec:]) {
		return b[:prec]
	}

	b = b[:prec+1]
	i := prec - 1

	// Blindly increment b[i] and handle possible carries later.
	switch mode {
	case AwayFromZero:
		b[i]++
	case ToZero:
		// OK
	case ToPositiveInf:
		if pos {
			b[i]++
		}
	case ToNegativeInf:
		if !pos {
			b[i]++
		}
	case ToNearestEven:
		if b[i+1] > '5' || b[i+1] == '5' && b[i]%2 != 0 {
			b[i]++
		}
	case ToNearestAway:
		if b[i+1] >= '5' {
			b[i]++
		}
	}

	if b[i] != '9'+1 {
		return b[:prec]
	}

	// We had to carry.
	b[i] = '0'

	for i--; i >= 0; i-- {
		if b[i] != '9' {
			b[i]++
			break
		}
		b[i] = '0'
	}

	// Carried all the way over to the first column, so slide the buffer down
	// and instead of reallocating.
	if b[0] == '0' {
		copy(b[1:], b)
		b[0] = '1'
		// We might end up with an extra digit of precision. E.g., given the
		// decimal 9.9 with a requested precision of 1, we'd convert 99 -> 10.
		// Let the calling code handle that case.
		prec++
	}
	return b[:prec]
}
1175889
func RoundTripper(rt http.RoundTripper) Interceptor {
	return InterceptorFunc(func(chain Chain) (*http.Response, error) {
		return rt.RoundTrip(chain.Request())
	})
}