package meta

import "github.com/ydb-platform/ydb-go-sdk/v3/meta"

Example (ConsumedUnitsCount)

Code:

{
	ctx := context.TODO()
	db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close(ctx) // cleanup resources
	var (
		query              = `SELECT 42 as id, "my string" as myStr`
		id                 int32  // required value
		myStr              string // optional value
		totalConsumedUnits uint64
	)
	err = db.Table().Do( // Do retry operation on errors with best effort
		meta.WithTrailerCallback(ctx, func(md metadata.MD) {
			totalConsumedUnits += meta.ConsumedUnits(md)
		}),
		func(ctx context.Context, s table.Session) (err error) { // retry operation
			_, res, err := s.Execute(ctx, table.DefaultTxControl(), query, nil)
			if err != nil {
				return err // for auto-retry with driver
			}
			defer res.Close()                                // cleanup resources
			if err = res.NextResultSetErr(ctx); err != nil { // check single result set and switch to it
				return err // for auto-retry with driver
			}
			for res.NextRow() { // iterate over rows
				err = res.ScanNamed(
					named.Required("id", &id),
					named.OptionalWithDefault("myStr", &myStr),
				)
				if err != nil {
					return err // generally scan error not retryable, return it for driver check error
				}
				log.Printf("id=%v, myStr='%s'\n", id, myStr)
			}

			return res.Err() // return finally result error for auto-retry with driver
		},
		table.WithIdempotent(),
	)
	if err != nil {
		log.Printf("unexpected error: %v", err)
	}
	log.Println("total consumed units:", totalConsumedUnits)
}

Index

Examples

Functions

func ConsumedUnits

func ConsumedUnits(md metadata.MD) (consumedUnits uint64)

func WithAllowFeatures

func WithAllowFeatures(ctx context.Context, features ...string) context.Context

WithAllowFeatures returns a copy of parent context with allowed client feature

func WithApplicationName

func WithApplicationName(ctx context.Context, applicationName string) context.Context

WithApplicationName returns a copy of parent context with application name

func WithRequestType

func WithRequestType(ctx context.Context, requestType string) context.Context

WithRequestType returns a copy of parent context with custom request type

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID returns a copy of parent context with traceID

func WithTrailerCallback

func WithTrailerCallback(
	ctx context.Context,
	callback func(md metadata.MD),
) context.Context

WithTrailerCallback attaches callback to context for listening incoming metadata

func WithUserAgent

func WithUserAgent(ctx context.Context, _ string) context.Context

WithUserAgent returns a copy of parent context with custom user-agent info

Deprecated: use WithApplicationName instead. Will be removed after Oct 2024. Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated

Source Files

consumed_units.go context.go

Version
v3.77.0
Published
Aug 22, 2024
Platform
darwin/amd64
Imports
4 packages
Last checked
7 minutes ago

Tools for package owners.