Skip to content

Commit

Permalink
Add a flag to set context timeout (#64)
Browse files Browse the repository at this point in the history
As you can see in the following code, gRPC client defines 30 seconds.
https://github.com/googleapis/google-cloud-go/blob/spanner/v1.41.0/spanner/apiv1/spanner_client.go#L551-L555
It's too short in some cases.
  • Loading branch information
iwata authored Dec 9, 2022
1 parent bd910ea commit 58c2c06
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"context"

"github.com/cloudspannerecosystem/wrench/pkg/spanner"
"github.com/spf13/cobra"
Expand All @@ -42,7 +43,8 @@ var applyCmd = &cobra.Command{
}

func apply(c *cobra.Command, _ []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
flagPartitioned = "partitioned"
flagPriority = "priority"
flagNode = "node"
flagTimeout = "timeout"
defaultSchemaFileName = "schema.sql"
)

Expand Down
4 changes: 3 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package cmd

import (
"context"
"io/ioutil"

"github.com/spf13/cobra"
Expand All @@ -32,7 +33,8 @@ var createCmd = &cobra.Command{
}

func create(c *cobra.Command, _ []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package cmd

import (
"context"

"github.com/spf13/cobra"
)

Expand All @@ -30,7 +32,8 @@ var dropCmd = &cobra.Command{
}

func drop(c *cobra.Command, _ []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var instanceCreateCmd = &cobra.Command{
}

func instanceCreate(c *cobra.Command, _ []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerAdminClient(ctx, c)
if err != nil {
Expand All @@ -46,7 +47,8 @@ var instanceDeleteCmd = &cobra.Command{
}

func instanceDelete(c *cobra.Command, _ []string) error {
ctx := context.Background()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerAdminClient(ctx, c)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package cmd

import (
"context"
"io/ioutil"

"github.com/spf13/cobra"
Expand All @@ -31,8 +32,9 @@ var loadCmd = &cobra.Command{
RunE: load,
}

func load(c *cobra.Command, args []string) error {
ctx := c.Context()
func load(c *cobra.Command, _ []string) error {
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand All @@ -48,7 +50,7 @@ func load(c *cobra.Command, args []string) error {
}
}

err = ioutil.WriteFile(schemaFilePath(c), ddl, 0664)
err = ioutil.WriteFile(schemaFilePath(c), ddl, 0o664)
if err != nil {
return &Error{
err: err,
Expand Down
12 changes: 8 additions & 4 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -106,7 +107,8 @@ func migrateCreate(c *cobra.Command, args []string) error {
}

func migrateUp(c *cobra.Command, args []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

limit := -1
if len(args) > 0 {
Expand Down Expand Up @@ -145,8 +147,9 @@ func migrateUp(c *cobra.Command, args []string) error {
return client.ExecuteMigrations(ctx, migrations, limit, migrationTableName)
}

func migrateVersion(c *cobra.Command, args []string) error {
ctx := c.Context()
func migrateVersion(c *cobra.Command, _ []string) error {
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand Down Expand Up @@ -180,7 +183,8 @@ func migrateVersion(c *cobra.Command, args []string) error {
}

func migrateSet(c *cobra.Command, args []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

if len(args) == 0 {
return &Error{
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd
import (
"context"
"os"
"time"

"github.com/spf13/cobra"
)
Expand All @@ -39,6 +40,7 @@ var (
directory string
schemaFile string
credentialsFile string
timeout time.Duration
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -70,6 +72,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&directory, flagNameDirectory, "", "Directory that schema file placed (required)")
rootCmd.PersistentFlags().StringVar(&schemaFile, flagNameSchemaFile, "", "Name of schema file (optional. if not set, will use default 'schema.sql' file name)")
rootCmd.PersistentFlags().StringVar(&credentialsFile, flagCredentialsFile, "", "Specify Credentials File")
rootCmd.PersistentFlags().DurationVar(&timeout, flagTimeout, time.Hour, "Context timeout")

rootCmd.Version = Version
rootCmd.SetVersionTemplate(versionTemplate)
Expand Down
5 changes: 4 additions & 1 deletion cmd/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package cmd

import (
"context"

"github.com/spf13/cobra"
)

Expand All @@ -30,7 +32,8 @@ var truncateCmd = &cobra.Command{
}

func truncate(c *cobra.Command, _ []string) error {
ctx := c.Context()
ctx, cancel := context.WithTimeout(c.Context(), timeout)
defer cancel()

client, err := newSpannerClient(ctx, c)
if err != nil {
Expand Down

0 comments on commit 58c2c06

Please sign in to comment.