github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/meta/example_test.go (about) 1 package meta_test 2 3 import ( 4 "context" 5 "log" 6 7 "google.golang.org/grpc/metadata" 8 9 "github.com/ydb-platform/ydb-go-sdk/v3" 10 "github.com/ydb-platform/ydb-go-sdk/v3/meta" 11 "github.com/ydb-platform/ydb-go-sdk/v3/table" 12 "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named" 13 ) 14 15 func Example_consumedUnitsCount() { 16 ctx := context.TODO() 17 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 18 if err != nil { 19 log.Fatal(err) 20 } 21 defer db.Close(ctx) // cleanup resources 22 var ( 23 query = `SELECT 42 as id, "my string" as myStr` 24 id int32 // required value 25 myStr string // optional value 26 totalConsumedUnits uint64 27 ) 28 err = db.Table().Do( // Do retry operation on errors with best effort 29 meta.WithTrailerCallback(ctx, func(md metadata.MD) { 30 totalConsumedUnits += meta.ConsumedUnits(md) 31 }), 32 func(ctx context.Context, s table.Session) (err error) { // retry operation 33 _, res, err := s.Execute(ctx, table.DefaultTxControl(), query, nil) 34 if err != nil { 35 return err // for auto-retry with driver 36 } 37 defer res.Close() // cleanup resources 38 if err = res.NextResultSetErr(ctx); err != nil { // check single result set and switch to it 39 return err // for auto-retry with driver 40 } 41 for res.NextRow() { // iterate over rows 42 err = res.ScanNamed( 43 named.Required("id", &id), 44 named.OptionalWithDefault("myStr", &myStr), 45 ) 46 if err != nil { 47 return err // generally scan error not retryable, return it for driver check error 48 } 49 log.Printf("id=%v, myStr='%s'\n", id, myStr) 50 } 51 52 return res.Err() // return finally result error for auto-retry with driver 53 }, 54 table.WithIdempotent(), 55 ) 56 if err != nil { 57 log.Printf("unexpected error: %v", err) 58 } 59 log.Println("total consumed units:", totalConsumedUnits) 60 }