valkey

package
v1.37.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package valkey connects to Memorystore for Valkey instances created by the terraform valkey module. It applies the same opinions the module hardcodes: IAM_AUTH as the workload identity (a fresh token per reconnect), TLS pinned to the managed server CA, and the PSC connect endpoint. Resolve the instance's full resource name once at boot, then dial clients from the resolved Endpoint.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(ctx context.Context, ep Endpoint, opts ...Option) (redis.UniversalClient, error)

NewClient dials ep as the workload identity — a standalone client against a primary endpoint, a cluster client against a discovery endpoint. Callers sharing one instance under several key namespaces should dial one client per namespace so heavy tenants (a Pub/Sub fan, a bulk writer) never contend on a shared pool.

Example
package main

import (
	"context"
	"fmt"

	"github.com/chainguard-dev/terraform-infra-common/pkg/valkey"
)

func main() {
	ctx := context.Background()
	ep, err := valkey.Resolve(ctx, "projects/my-project/locations/us-central1/instances/my-instance")
	if err != nil {
		fmt.Println("resolve failed:", err)
		return
	}
	client, err := valkey.NewClient(ctx, ep)
	if err != nil {
		fmt.Println("dial failed:", err)
		return
	}
	defer client.Close()
	_ = client
}
Example (WithOption)
package main

import (
	"context"
	"fmt"

	"github.com/chainguard-dev/terraform-infra-common/pkg/valkey"
	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()
	ep, err := valkey.Resolve(ctx, "projects/my-project/locations/us-central1/instances/my-instance")
	if err != nil {
		fmt.Println("resolve failed:", err)
		return
	}
	// Options adjust pool sizing or timeouts before the client is built.
	client, err := valkey.NewClient(ctx, ep, func(uo *redis.UniversalOptions) {
		uo.PoolSize = 10
	})
	if err != nil {
		fmt.Println("dial failed:", err)
		return
	}
	defer client.Close()
	_ = client
}

Types

type Endpoint

type Endpoint struct {
	Addr    string
	Roots   *x509.CertPool
	Cluster bool
}

Endpoint is a resolved Memorystore for Valkey instance: the PSC connect address, the managed CA pool clients pin, and whether the instance speaks the cluster protocol.

func Resolve

func Resolve(ctx context.Context, instance string) (Endpoint, error)

Resolve looks up instance's connect address and managed server CA over the Memorystore API; instance is the full resource name (projects/{p}/locations/{l}/instances/{i}). A cluster-mode instance resolves to its discovery endpoint, a standalone one to its primary. Resolving once at boot pins the CA bundle: all currently-active CAs, which outlive the weekly server-cert rotation they sign. The caller's workload identity needs memorystore.instances.get alongside its connect grant.

Example
package main

import (
	"context"
	"fmt"

	"github.com/chainguard-dev/terraform-infra-common/pkg/valkey"
)

func main() {
	ctx := context.Background()
	// Resolve looks up the PSC connect address and managed CA for a
	// Memorystore for Valkey instance. Pass the full resource name once at
	// boot and reuse the Endpoint for all NewClient calls.
	ep, err := valkey.Resolve(ctx, "projects/my-project/locations/us-central1/instances/my-instance")
	if err != nil {
		fmt.Println("resolve failed:", err)
		return
	}
	_ = ep
}

type Option

type Option func(*redis.UniversalOptions)

Option adjusts the client options (pool sizing, timeouts, hooks) before the client is built. The endpoint address, IAM credentials, and pinned TLS roots are already set when an Option runs; overwriting them defeats the package.

Jump to

Keyboard shortcuts

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