load

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2019 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

Package load include helpers for building grpclb compatible servers.

Example
package main

import (
	"log"
	"net"
	"time"

	lbpb "github.com/bsm/grpclb/grpclb_backend_v1"
	"github.com/bsm/grpclb/load"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

// GreeterServer is used to implement helloworld.GreeterServer.
type GreeterServer struct {
	reporter *load.RateReporter
}

// SayHello implements helloworld.GreeterServer
// It increments rate to report load metrics to the load balancer.
func (s *GreeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	s.reporter.Increment(1)
	return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}

func main() {
	lis, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	r := load.NewRateReporter(time.Minute)
	pb.RegisterGreeterServer(s, &GreeterServer{reporter: r})
	lbpb.RegisterLoadReportServer(s, r)
	s.Serve(lis)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateReporter

type RateReporter struct {
	// contains filtered or unexported fields
}

RateReporter maintains score as an exponentially weighted rate

func NewRateReporter

func NewRateReporter(d time.Duration) *RateReporter

NewRateReporter builds a rate reporter over a period of time.

func (*RateReporter) Increment

func (r *RateReporter) Increment(n int64)

Increment increases the rate by n. Typical use case is e.g. the rate of requests, where Increment(1) is called on every request.

func (*RateReporter) Load

Load implements github.com/bsm/grpclb/grpclb_backend_v1.LoadReportServer

func (*RateReporter) Score

func (r *RateReporter) Score() int64

Score returns the current load score

type Reporter

type Reporter struct {
	// contains filtered or unexported fields
}

Reporter is a simple and proportional github.com/bsm/grpclb/grpclb_backend_v1.LoadReportServer

func NewReporter

func NewReporter() *Reporter

NewReporter creates a new Reporter

func (*Reporter) Increment

func (r *Reporter) Increment(n int64)

Increment allows to adjust load by a certain increment. Typical use case is e.g. the number of server connections, where Increment(1) is called on evert new connect and Increment(-1) on every disconnect.

func (*Reporter) Load

Load implements github.com/bsm/grpclb/grpclb_backend_v1.LoadReportServer

func (*Reporter) Reset

func (r *Reporter) Reset()

Reset resets the load to 0

func (*Reporter) Score

func (r *Reporter) Score() int64

Score returns the current load score

func (*Reporter) Set

func (r *Reporter) Set(n int64)

Set sets the load to a particular value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL