Coder Social home page Coder Social logo

grpoolc's Introduction

gRPoolC

A simple library to allow for pooling of gRPC connections in a way that takes advantage of service endpoints that horizontally scale. Connections are created by a function passed to the pool, so you can define whatever options or logic you'd like. To ensure that connections are spread across hosts behind a load balancer as they expand or contract, 5% of Get() requests will close an existing connection, and open up a new one. Maximum connection limits are enforced by not putting excess connections back in the pool.

Usage

import (
	"github.com/DMXRoid/grpoolc"
	"google.golang.org/grpc"
	"golang.org/x/net/context"
	"fmt"
	"path.to/myrpc"
)

func setupPool() {
	grpcAddr := fmt.Sprintf("%s:%d","foo.default.svc", 8080)
	dialer := func(addr string, timeout time.Duration) (net.Conn, error) {
		return tls.Dial("tcp", addr, &tls.Config{
			InsecureSkipVerify: true,
		})
	}
	newConnectionGenerator := func() (*grpc.ClientConn, error) {
		return grpc.Dial(*contentGrpcAddr, grpc.WithDialer(dialer), grpc.WithInsecure())
	}
	grpoolc.New("my-service", newConnectionGenerator, 100)

}

func callGrpcService() {
	conn, err := grpoolc.Get("my-service")
	defer grpoolc.Put("my-service", conn)
	ctx := context.Background()
	client := myrpc.NewMyRpcClient(conn)
	resp, err := client.MyRpc(ctx, &myrpc.MyRpcMessage{})
}

grpoolc's People

Contributors

dmxroid avatar

Stargazers

 avatar Kylie Rogers avatar Philip Paquette avatar 穿山甲 avatar Owen Hael avatar Krzysztof Góralski avatar Piotr Kowalczuk avatar Cole Calistra avatar  avatar Zhijian Xia avatar

Watchers

 avatar 穿山甲 avatar James Cloos avatar  avatar  avatar

Forkers

webmi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.