client

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: Apache-2.0 Imports: 15 Imported by: 2

README

A lightweight library for Connecting to Google Cloud Run Endpoints

This is a lightweight library for making authenticated calls to Cloud Run endpoints.

Documentation

Overview

Package client provides a lightweight set of methods which clients could use to make authenticated calls to services running on Google Cloud Run

It was inspired by the example: Send gRPC requests with authentication as per the Cloud Run endpoints documentation at https://cloud.google.com/run/docs/samples/cloudrun-grpc-request-auth

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConn

func NewConn(ctx context.Context, host string, insecure bool, opts ...grpc.DialOption) (*grpc.ClientConn, error)

NewConn creates a new gRPC connection.

  • host should be of the form domain:port, for example: `your-app-on-cloudrun-abcdef-ew.a.run.app:443`
  • set insecure to `true` when testing your gRPC server locally.

This approach was inspired by the example provided on the following URL: https://cloud.google.com/run/docs/samples/cloudrun-grpc-request-auth.

However, instead of manually adding the Authorization header to each gRPC request, this method implements a TokenSource pattern. By configuring the gRPC connection once, tokens are automatically injected with each subsequent gRPC request.

Tokens generally have a one-hour expiration time, and the TokenSource logic caches and automatically refreshes the token upon expiration. This greatly simplifies token recycling within your service.

Example
package main

import (
	"context"
	"go.alis.build/client"
	"log"
)

func main() {

	ctx := context.Background()
	conn, err := client.NewConn(ctx, "cloudrun-service.app:443", false)
	if err != nil {
		log.Println(err)
	}

	// Use the connection auto generated client packages, using the example at:
	// https://grpc.io/docs/languages/go/basics/#client, we will instantiate a client as follows:
	//     routeGuideClient := pb.NewRouteGuideClient(conn)

	_ = conn
}
Output:

func NewConnWithRetry added in v0.0.7

func NewConnWithRetry(ctx context.Context, host string, insecure bool, opts ...grpc.DialOption) (*grpc.ClientConn, error)

Does the same as NewConn, but retries on temporary TCP connection resets, which is common when connecting to Cloud Run services.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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