grpcgcp – github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp Index | Files | Directories

package grpcgcp

import "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp"

Package grpcgcp provides grpc supports for Google Cloud APIs. For now it provides connection management with affinity support.

Note: "channel" is analagous to "connection" in our context.

Usage:

1. First, initialize the api configuration. There are two ways:

1a. Create a json file defining the configuration and read it.

	// Create some_api_config.json
	{
		"channelPool": {
			"maxSize": 4,
			"maxConcurrentStreamsLowWatermark": 50
		},
		"method": [
			{
				"name": [ "/some.api.v1/Method1" ],
				"affinity": {
					"command": "BIND",
					"affinityKey": "key1"
				}
			},
			{
				"name": [ "/some.api.v1/Method2" ],
				"affinity": {
					"command": "BOUND",
					"affinityKey": "key2"
				}
			},
			{
				"name": [ "/some.api.v1/Method3" ],
				"affinity": {
					"command": "UNBIND",
					"affinityKey": "key3"
				}
			}
		]
	}

	jsonFile, err := ioutil.ReadFile("some_api_config.json")
	if err != nil {
		t.Fatalf("Failed to read config file: %v", err)
	}
	jsonCfg := string(jsonFile)

1b. Create apiConfig directly and convert it to json.

	// import (
	// 	configpb "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/grpc_gcp"
	// )

	apiConfig := &configpb.ApiConfig{
		ChannelPool: &configpb.ChannelPoolConfig{
			MaxSize:                          4,
			MaxConcurrentStreamsLowWatermark: 50,
		},
		Method: []*configpb.MethodConfig{
			&configpb.MethodConfig{
				Name: []string{"/some.api.v1/Method1"},
				Affinity: &configpb.AffinityConfig{
					Command:     configpb.AffinityConfig_BIND,
					AffinityKey: "key1",
				},
			},
			&configpb.MethodConfig{
				Name: []string{"/some.api.v1/Method2"},
				Affinity: &configpb.AffinityConfig{
					Command:     configpb.AffinityConfig_BOUND,
					AffinityKey: "key2",
				},
			},
			&configpb.MethodConfig{
				Name: []string{"/some.api.v1/Method3"},
				Affinity: &configpb.AffinityConfig{
					Command:     configpb.AffinityConfig_UNBIND,
					AffinityKey: "key3",
				},
			},
		},
	}

	c, err := protojson.Marshal(apiConfig)
	if err != nil {
		t.Fatalf("cannot json encode config: %v", err)
	}
	jsonCfg := string(c)

2. Make ClientConn with specific DialOptions to enable grpc_gcp load balancer with provided configuration. And specify gRPC-GCP interceptors.

conn, err := grpc.Dial(
	target,
	// Register and specify the grpc-gcp load balancer.
	grpc.WithDisableServiceConfig(),
	grpc.WithDefaultServiceConfig(
		fmt.Sprintf(
			`{"loadBalancingConfig": [{"%s":%s}]}`,
			grpcgcp.Name,
			jsonCfg,
		),
	),
	// Set grpcgcp interceptors.
	grpc.WithUnaryInterceptor(grpcgcp.GCPUnaryClientInterceptor),
	grpc.WithStreamInterceptor(grpcgcp.GCPStreamClientInterceptor),
)

Index

Constants

const (
	// Name is the name of grpc_gcp balancer.
	Name = "grpc_gcp"
)

Functions

func GCPStreamClientInterceptor

func GCPStreamClientInterceptor(
	ctx context.Context,
	desc *grpc.StreamDesc,
	cc *grpc.ClientConn,
	method string,
	streamer grpc.Streamer,
	opts ...grpc.CallOption,
) (grpc.ClientStream, error)

GCPStreamClientInterceptor intercepts the execution of a client streaming RPC and injects necessary information to be used by the picker.

func GCPUnaryClientInterceptor

func GCPUnaryClientInterceptor(
	ctx context.Context,
	method string,
	req interface{},
	reply interface{},
	cc *grpc.ClientConn,
	invoker grpc.UnaryInvoker,
	opts ...grpc.CallOption,
) error

GCPUnaryClientInterceptor intercepts the execution of a unary RPC and injects necessary information to be used by the picker.

Types

type GcpBalancerConfig

type GcpBalancerConfig struct {
	serviceconfig.LoadBalancingConfig
	*pb.ApiConfig
}

Source Files

doc.go gcp_balancer.go gcp_interceptor.go gcp_picker.go

Directories

PathSynopsis
grpc_gcp
mocksPackage mocks is a generated GoMock package.
test_grpc
test_grpc/helloworld
test_grpc/helloworld/helloworld
Version
v1.3.0
Published
Jan 13, 2023
Platform
windows/amd64
Imports
19 packages
Last checked
1 hour ago

Tools for package owners.