Help us shape the future of Go! Take the 2026 Go Developer Survey

client

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package client is a Go client for light-tokenmeter's gRPC API: recording usage events and reading summaries, with large windows fetched in parts so a summary is not cut at the server's row cap.

Index

Constants

View Source
const MaxBatch = 500

MaxBatch is the most events one RecordUsageEvents call takes.

View Source
const MinWindow = time.Hour

MinWindow is the shortest window Summary splits a truncated one into.

Variables

This section is empty.

Functions

func Merge

func Merge(rows []*usagev1.UsageSummaryRow, byDay bool) []*usagev1.UsageSummaryRow

Merge sums rows that have the same dimensions, and the same day when byDay is set; without it every row covers the whole window. The order is that of each key's first row.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration reads Go durations plus d (days) and w (weeks): 7d, 2w, 36h.

func USD

func USD(nanousd int64) float64

USD converts nano-USD, the API's unit of cost, to dollars.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to one light-tokenmeter. It is safe for concurrent use.

func New

func New(o Options) (*Client, error)

New connects lazily: the first call dials.

func (*Client) ByMeta

ByMeta returns what the events tagged meta_key=meta_value used.

func (*Client) Close

func (c *Client) Close() error

Close releases the connection.

func (*Client) Record

func (c *Client) Record(ctx context.Context, events ...*usagev1.UsageEvent) (created, duplicates int, err error)

Record sends events in batches of MaxBatch and counts what was new. A refused batch stops it; the batches before it stay recorded.

func (*Client) Service

func (c *Client) Service() usagev1.UsageServiceClient

Service is the generated client, for calls this package does not wrap.

func (*Client) Summary

func (c *Client) Summary(ctx context.Context, req *usagev1.GetUsageSummaryRequest) (rows []*usagev1.UsageSummaryRow, truncated bool, err error)

Summary returns the rows of req's window. When the server caps a window (truncated), Summary fetches its halves and merges them, down to MinWindow; truncated is true only if a window that short is still capped.

type Options

type Options struct {
	Addr      string      // gRPC host:port
	Token     string      // bearer token; empty when the service runs without auth
	TLS       bool        // use TLS
	TLSConfig *tls.Config // optional TLS settings; nil means system roots, TLS 1.2+
	// DialOptions are added after the client's own, for tests and custom transports.
	DialOptions []grpc.DialOption
}

Options say where light-tokenmeter is and how to reach it.

Jump to

Keyboard shortcuts

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