rpc

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: MIT Imports: 8 Imported by: 0

README

Usage

go get github.com/dudebing99/grpc-connection-pool

Example

package main

import (
	"context"
	"fmt"
	rpc "github.com/dudebing99/grpc-connection-pool"
)

func main() {
	pool, err := rpc.NewRpcClientPool(rpc.WithServerAddr("0.0.0.0:8080"))
	if err != nil {
		fmt.Println("init client pool error")
		return
	}

	clientConn, close, err := pool.Acquire()
	defer close()
	if err != nil {
		fmt.Println("acquire client connection error")
		return
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	reply, err := NewGreeterClient(clientConn).SayHello(ctx, &HelloRequest{Name: "SillyBoy"})
	if err != nil {
		fmt.Println("say hello error, ", err)
		return
	}

	fmt.Println(reply.Message)
}

Documentation

Index

Constants

View Source
const (
	// Ready Can be used
	Ready condition = iota
	// Put Not available. Maybe later.
	Put
	// Destroy Failure occurs and cannot be restored
	Destroy
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func()

CloseFunc should defer

type Option

type Option func(*Pool)

func WithAcquireTimeOut

func WithAcquireTimeOut(timeout time.Duration) Option

func WithDialOption

func WithDialOption(ops ...grpc.DialOption) Option
func WithDynamicLink(dynamicLink bool) Option

func WithMaxCap

func WithMaxCap(num int64) Option

func WithOverflowCap

func WithOverflowCap(overflowCap bool) Option

func WithServerAddr

func WithServerAddr(addr string) Option

type Pool

type Pool struct {
	ServerAddr     string
	MaxCap         int64
	AcquireTimeout time.Duration
	DynamicLink    bool
	OverflowCap    bool

	ChannelStat chanStat
	// contains filtered or unexported fields
}

func NewRpcClientPool

func NewRpcClientPool(ops ...Option) (*Pool, error)

func (*Pool) Acquire

func (pool *Pool) Acquire() (*grpc.ClientConn, CloseFunc, error)

func (*Pool) ClearPool

func (pool *Pool) ClearPool()

ClearPool Disconnect the link before exit the program

func (*Pool) GetStat

func (pool *Pool) GetStat() (used int64, surplus int)

GetStat Return to the use of resources in the pool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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