From 63f227386f8fb481d19fb573823977bf7e907b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20L=C3=B3pez?= <1953782+julio-lopez@users.noreply.github.com> Date: Sun, 6 Oct 2024 09:05:26 -0700 Subject: [PATCH] refactor(general): levarage stdlib's `context.WithoutCancel(ctx)` (#4159) Removes `internal/ctxutil` package with equivalent implementation --- cli/command_server_start.go | 6 +++--- internal/cache/cache_storage.go | 3 +-- internal/ctxutil/detach.go | 28 ---------------------------- internal/scheduler/scheduler.go | 3 +-- internal/server/server.go | 5 ++--- internal/server/source_manager.go | 3 +-- repo/grpc_repository_client.go | 5 ++--- 7 files changed, 10 insertions(+), 43 deletions(-) delete mode 100644 internal/ctxutil/detach.go diff --git a/cli/command_server_start.go b/cli/command_server_start.go index 3532d544e..4ecd14e0a 100644 --- a/cli/command_server_start.go +++ b/cli/command_server_start.go @@ -20,7 +20,6 @@ htpasswd "github.com/tg123/go-htpasswd" "github.com/kopia/kopia/internal/auth" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/server" "github.com/kopia/kopia/repo" ) @@ -256,11 +255,12 @@ func (c *commandServerStart) run(ctx context.Context) (reterr error) { if c.serverStartShutdownWhenStdinClosed { log(ctx).Info("Server will close when stdin is closed...") - ctxutil.GoDetached(ctx, func(ctx context.Context) { + go func() { + ctx := context.WithoutCancel(ctx) // consume all stdin and close the server when it closes io.Copy(io.Discard, os.Stdin) //nolint:errcheck shutdownHTTPServer(ctx, httpServer) - }) + }() } onExternalConfigReloadRequest(srv.Refresh) diff --git a/internal/cache/cache_storage.go b/internal/cache/cache_storage.go index 7affe3b25..d037f3979 100644 --- a/internal/cache/cache_storage.go +++ b/internal/cache/cache_storage.go @@ -8,7 +8,6 @@ "github.com/pkg/errors" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/ospath" "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob/filesystem" @@ -45,7 +44,7 @@ func NewStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdi } } - fs, err := filesystem.New(ctxutil.Detach(ctx), &filesystem.Options{ + fs, err := filesystem.New(context.WithoutCancel(ctx), &filesystem.Options{ Path: contentCacheDir, Options: sharded.Options{ DirectoryShards: []int{2}, diff --git a/internal/ctxutil/detach.go b/internal/ctxutil/detach.go deleted file mode 100644 index 8393566e8..000000000 --- a/internal/ctxutil/detach.go +++ /dev/null @@ -1,28 +0,0 @@ -// Package ctxutil implements utilities for manipulating context. -package ctxutil - -import ( - "context" -) - -type detachedContext struct { - // inherit most methods from context.Background() - context.Context //nolint:containedctx - wrapped context.Context //nolint:containedctx -} - -// Detach returns a context that inheris provided context's values but not deadline or cancellation. -func Detach(ctx context.Context) context.Context { - return detachedContext{context.Background(), ctx} -} - -// GoDetached invokes the provided function in a goroutine where the context is detached. -func GoDetached(ctx context.Context, fun func(ctx context.Context)) { - go func() { - fun(Detach(ctx)) - }() -} - -func (d detachedContext) Value(key interface{}) interface{} { - return d.wrapped.Value(key) -} diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go index b2b82397e..ed03fd5a9 100644 --- a/internal/scheduler/scheduler.go +++ b/internal/scheduler/scheduler.go @@ -10,7 +10,6 @@ "time" "github.com/kopia/kopia/internal/clock" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/repo/logging" ) @@ -66,7 +65,7 @@ func Start(ctx context.Context, getItems GetItemsFunc, opts Options) *Scheduler go func() { defer s.wg.Done() - s.run(ctxutil.Detach(ctx)) + s.run(context.WithoutCancel(ctx)) }() return s diff --git a/internal/server/server.go b/internal/server/server.go index 36f311af4..7d2e6d5ae 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -21,7 +21,6 @@ "github.com/kopia/kopia/internal/auth" "github.com/kopia/kopia/internal/clock" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/mount" "github.com/kopia/kopia/internal/passwordpersist" "github.com/kopia/kopia/internal/scheduler" @@ -362,7 +361,7 @@ func (s *Server) handleRequestPossiblyNotConnected(isAuthorized isAuthorizedFunc // process the request while ignoring the cancellation signal // to ensure all goroutines started by it won't be canceled // when the request finishes. - ctx = ctxutil.Detach(ctx) + ctx = context.WithoutCancel(ctx) if isAuthorized(ctx, rc) { v, err = f(ctx, rc) @@ -585,7 +584,7 @@ func (s *Server) SetRepository(ctx context.Context, rep repo.Repository) error { s.maint = nil } - s.sched = scheduler.Start(ctxutil.Detach(ctx), s.getSchedulerItems, scheduler.Options{ + s.sched = scheduler.Start(context.WithoutCancel(ctx), s.getSchedulerItems, scheduler.Options{ TimeNow: clock.Now, Debug: s.options.DebugScheduler, RefreshChannel: s.schedulerRefresh, diff --git a/internal/server/source_manager.go b/internal/server/source_manager.go index fec1907dc..ec0dfbfd5 100644 --- a/internal/server/source_manager.go +++ b/internal/server/source_manager.go @@ -11,7 +11,6 @@ "github.com/kopia/kopia/fs" "github.com/kopia/kopia/fs/localfs" "github.com/kopia/kopia/internal/clock" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/serverapi" "github.com/kopia/kopia/internal/uitask" "github.com/kopia/kopia/repo" @@ -159,7 +158,7 @@ func (s *sourceManager) start(ctx context.Context, isLocal bool) { func (s *sourceManager) run(ctx context.Context, isLocal bool) { // make sure we run in a detached context, which ignores outside cancellation and deadline. - ctx = ctxutil.Detach(ctx) + ctx = context.WithoutCancel(ctx) s.setStatus("INITIALIZING") defer s.setStatus("STOPPED") diff --git a/repo/grpc_repository_client.go b/repo/grpc_repository_client.go index ce942d6cd..cf4c27cf6 100644 --- a/repo/grpc_repository_client.go +++ b/repo/grpc_repository_client.go @@ -18,7 +18,6 @@ "google.golang.org/grpc/credentials" "github.com/kopia/kopia/internal/clock" - "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/gather" apipb "github.com/kopia/kopia/internal/grpcapi" "github.com/kopia/kopia/internal/retry" @@ -739,7 +738,7 @@ func (r *grpcRepositoryClient) WriteContent(ctx context.Context, data gather.Byt // clone so that caller can reuse the buffer clone := data.ToByteSlice() - if err := r.doWriteAsync(ctxutil.Detach(ctx), contentID, clone, prefix, comp); err != nil { + if err := r.doWriteAsync(context.WithoutCancel(ctx), contentID, clone, prefix, comp); err != nil { return content.EmptyID, err } @@ -897,7 +896,7 @@ func (r *grpcRepositoryClient) getOrEstablishInnerSession(ctx context.Context) ( r.innerSessionAttemptCount++ v, err := retry.WithExponentialBackoff(ctx, "establishing session", func() (*grpcInnerSession, error) { - sess, err := cli.Session(ctxutil.Detach(ctx)) + sess, err := cli.Session(context.WithoutCancel(ctx)) if err != nil { return nil, errors.Wrap(err, "Session()") }