Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created June 20, 2019 06:59
Show Gist options
  • Save yfuruyama/81fad95e4a179a72ffcd441fd07921c3 to your computer and use it in GitHub Desktop.
Save yfuruyama/81fad95e4a179a72ffcd441fd07921c3 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"time"
"cloud.google.com/go/spanner"
"google.golang.org/api/option"
gtransport "google.golang.org/api/transport/grpc"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
)
func main() {
var project string
var instance string
var database string
flag.StringVar(&project, "project", "", "")
flag.StringVar(&instance, "instance", "", "")
flag.StringVar(&database, "database", "", "")
flag.Parse()
if project == "" || instance == "" || database == "" {
flag.Usage()
os.Exit(1)
}
ctx := context.Background()
sessionLabelKey := "client_ts"
sessionLabelValue := fmt.Sprintf("%d", time.Now().Unix())
sessionFilter := fmt.Sprintf("labels.%s:%s\n", sessionLabelKey, sessionLabelValue)
dbPath := fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, instance, database)
client, err := spanner.NewClientWithConfig(ctx, dbPath, spanner.ClientConfig{
SessionPoolConfig: spanner.SessionPoolConfig{
MinOpened: 5,
MaxOpened: 30,
MaxIdle: 10,
MaxBurst: 30,
},
SessionLabels: map[string]string{
sessionLabelKey: sessionLabelValue,
},
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
// do transactions after 20 sec
time.AfterFunc(time.Second*20, func() {
log.Printf("Run 100 transactions in parallel\n")
for i := 0; i < 100; i++ {
go func() {
client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
time.Sleep(time.Second * 1)
return nil
})
}()
}
})
// check sessions periodically
for {
count, err := getCurrentSessionCount(ctx, dbPath, sessionFilter)
if err != nil {
log.Fatal(err)
}
log.Printf("Number of sessions: %d\n", count)
}
}
func getCurrentSessionCount(ctx context.Context, dbPath string, filter string) (int, error) {
allOpts := []option.ClientOption{
option.WithEndpoint("spanner.googleapis.com:443"),
}
conn, err := gtransport.Dial(ctx, allOpts...)
if err != nil {
return 0, err
}
rpcClient := sppb.NewSpannerClient(conn)
resp, err := rpcClient.ListSessions(ctx, &sppb.ListSessionsRequest{
Database: dbPath,
Filter: filter,
})
if err != nil {
return 0, err
}
return len(resp.Sessions), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment