Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions go/ioutil/timeout_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ioutil

import (
"context"
"io"
"time"
)

// TimeoutCloser is an io.Closer that has a timeout for executing the Close() function.
type TimeoutCloser struct {
ctx context.Context
closer io.Closer
timeout time.Duration
}

func NewTimeoutCloser(ctx context.Context, closer io.Closer, timeout time.Duration) *TimeoutCloser {
return &TimeoutCloser{
ctx: ctx,
closer: closer,
timeout: timeout,
}
}

func (c *TimeoutCloser) Close() error {
done := make(chan error)

ctx, cancel := context.WithTimeout(c.ctx, c.timeout)
defer cancel()

go func() {
defer close(done)
done <- c.closer.Close()
}()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}
53 changes: 53 additions & 0 deletions go/ioutil/timeout_closer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ioutil

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type hangCloser struct {
hang bool
}

func (c hangCloser) Close() error {
if c.hang {
ch := make(chan bool)
ch <- true // hang forever
}
return nil
}

func TestTimeoutCloser(t *testing.T) {
ctx := context.Background()
{
closer := NewTimeoutCloser(ctx, &hangCloser{hang: false}, time.Second)
err := closer.Close()
require.NoError(t, err)
}
{
closer := NewTimeoutCloser(ctx, &hangCloser{hang: true}, time.Second)
err := closer.Close()
require.Error(t, err)
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
}
5 changes: 5 additions & 0 deletions go/vt/mysqlctl/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const (
RestoreState = "restore_in_progress"
// BackupTimestampFormat is the format in which we save BackupTime and FinishedTime
BackupTimestampFormat = "2006-01-02.150405"

// closeTimeout is the timeout for closing backup files after writing.
// The value is a bit arbitrary. How long does it make sense to wait for a Close()? With a cloud-based implementation,
// network might be an issue. _Seconds_ are probably too short. The whereabouts of a minute us a reasonable value.
closeTimeout = 1 * time.Minute
)

const (
Expand Down
Loading