grpcp

package module
v0.0.0-...-6d47723 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: MIT Imports: 6 Imported by: 1

README

grpcp is a Grpc Persistent Connection Pool.

LICENSE Build Status codecov Go Report Card Godoc

Installation

go get -u github.com/0x5010/grpcp

Usage

default

import (
    "context"
    "fmt"

    "google.golang.org/grpc"
    pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

func main() {
    var addr, name string
    conn, _ := grpc.Dial(addr, grpc.WithInsecure())
    defer conn.Close()
    client := pb.NewGreeterClient(conn)
    r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
    fmt.Println(r.GetMessage())
}

with grpcp

import (
    "context"
    "fmt"

    "google.golang.org/grpc"
    pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

func main() {
    var addr, name string

    conn, _ := grpcp.GetConn(addr)  // get conn with grpcp default pool
    // defer conn.Close()  // no close, close will disconnect
    client := pb.NewGreeterClient(conn)
    r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
    fmt.Println(r.GetMessage())
}

custom dial function

import (
    "context"
    "fmt"

    "github.com/0x5010/grpcp"
    "google.golang.org/grpc"
    pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

func main() {
    var addr, name string

    pool := grpcp.New(func(addr string) (*grpc.ClientConn, error) {
        return grpc.Dial(
            addr,
            grpc.WithInsecure(),
        )
    })
    conn, _ := pool.GetConn(addr)

    client := pb.NewGreeterClient(conn)
    r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
    fmt.Println(r.GetMessage())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alives

func Alives() []string

Alives current live connections from default pool

func Dial

func Dial(addr string) (*grpc.ClientConn, error)

Dial force to create new connection from default pool, this operation will close old connection!

func GetConn

func GetConn(addr string) (*grpc.ClientConn, error)

GetConn create or get an existing connection from default pool

Types

type ConnectionTracker

type ConnectionTracker struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ConnectionTracker keep connections and maintain their status

func New

func New(dial DialFunc, opts ...TrackerOption) *ConnectionTracker

New initialization ConnectionTracker

func (*ConnectionTracker) Alives

func (ct *ConnectionTracker) Alives() []string

Alives current live connections

func (*ConnectionTracker) Dial

func (ct *ConnectionTracker) Dial(addr string) (*grpc.ClientConn, error)

Dial force to create new connection, this operation will close old connection!

func (*ConnectionTracker) GetConn

func (ct *ConnectionTracker) GetConn(addr string) (*grpc.ClientConn, error)

GetConn create or get an existing connection

type DialFunc

type DialFunc func(addr string) (*grpc.ClientConn, error)

DialFunc dial function

type ReadyCheckFunc

type ReadyCheckFunc func(ctx context.Context, conn *grpc.ClientConn) connectivity.State

ReadyCheckFunc check conn is ready function

type TrackerOption

type TrackerOption func(*ConnectionTracker)

TrackerOption initialization options

func CustomReadyCheck

func CustomReadyCheck(f ReadyCheckFunc) TrackerOption

CustomReadyCheck custom ready check function

func SetCheckReadyTimeout

func SetCheckReadyTimeout(timeout time.Duration) TrackerOption

SetCheckReadyTimeout custom checkReadyTimeout

func SetHeartbeatInterval

func SetHeartbeatInterval(interval time.Duration) TrackerOption

SetHeartbeatInterval custom heartbeatInterval

func SetTimeout

func SetTimeout(timeout time.Duration) TrackerOption

SetTimeout custom timeout

Jump to

Keyboard shortcuts

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