Example #1
0
// NewHTTP2Client creates a new HTTP2Client.
func NewHTTP2Client(
	connectionConnectTimeout time.Duration,
	connectionExpiry time.Duration) (*HTTP2Client, error) {
	client := new(HTTP2Client)
	client.conns = leverutil.NewCache(
		connectionExpiry,
		func(addr string) (interface{}, error) {
			clientLogger.WithFields("addr", addr).Debug("New connection")
			netConn, err := net.DialTimeout(
				"tcp", addr, connectionConnectTimeout)
			if err != nil {
				return nil, err
			}
			conn, err := newHTTP2Connection(false, netConn, nil)
			if err != nil {
				return nil, err
			}
			// Remove connection from pool if it gets closed.
			go client.destroyOnClose(addr, conn)
			return conn, nil
		},
		func(element interface{}) {
			clientLogger.Debug("Closing connection")
			element.(*http2Connection).close(nil)
		})
	return client, nil
}
Example #2
0
// NewGRPCPool returns a new GRPC connection pool.
func NewGRPCPool() (*GRPCPool, error) {
	expiry := GRPCUnusedTimeoutFlag.Get()
	pool := new(GRPCPool)
	pool.conns = leverutil.NewCache(
		expiry,
		func(addr string) (interface{}, error) {
			return grpc.Dial(addr, grpc.WithInsecure())
		},
		func(conn interface{}) {
			conn.(*grpc.ClientConn).Close()
		},
	)
	return pool, nil
}