Xuanwo opened a new pull request, #8942:
URL: https://github.com/apache/opendal/pull/8942
# Which issue does this PR close?
Closes #7608.
# Rationale for this change
Go callers rely on `context.Context` for cooperative cancellation and
request deadlines. Following PR #8930, which added `opendal_cancel_token` and
the `*_with_cancel` C ABI functions, this PR implements the Go binding side
(Option B in #7608) so storage operations propagate Go context cancellation to
the underlying Rust futures.
# What changes are included in this PR?
- **Error mapping (`bindings/go/error.go`)**:
- Add `CodeCancelled` matching `OPENDAL_CANCELLED`.
- Add `(*Error).Unwrap()` so cancelled operations return an `*Error` with
`CodeCancelled` whose cause is `context.Canceled` or
`context.DeadlineExceeded`, matching both `errors.As(err, &opErr)` and
`errors.Is(err, context.Canceled)` / `errors.Is(err, context.DeadlineExceeded)`.
- **Cancel token FFI & lifecycle helper (`bindings/go/cancel.go`,
`bindings/go/types.go`, `bindings/go/opendal.go`)**:
- Register `opendal_cancel_token_new`, `opendal_cancel_token_cancel`,
`opendal_cancel_token_is_cancelled`, `opendal_cancel_token_free`, and the
`*_with_cancel` C APIs via `purego`.
- Add `withCancelToken` / `withCancelTokenValue` helpers that:
- pass a `nil` token with zero native allocation when `ctx == nil` or
`ctx.Done() == nil`,
- short-circuit immediately with a `CodeCancelled` error wrapping
`ctx.Err()` when `ctx` is already done,
- wire `context.AfterFunc` to `opendal_cancel_token_cancel` and wait for
any in-flight callback to finish before calling `opendal_cancel_token_free` so
cancel and free never race.
- **Public Go API updates**:
- Add `ctx context.Context` as the first parameter on `Operator` I/O
methods (`Read`, `Reader`, `Write`, `Writer`, `CreateDir`, `Delete`, `Stat`,
`IsExist`, `List`, `Copy`, `Rename`, `Check`, `Copier`, `Deleter`,
`WithDeleter`, `Remove`, `PresignRead`, `PresignWrite`, `PresignDelete`,
`PresignStat`) and non-stdlib handle methods (`Writer.Close`, `Lister.Next`,
`Copier.Next`, `Copier.Abort`, `Deleter.Delete`, `Deleter.Flush`,
`Deleter.Close`).
- Keep standard Go `io` interface method signatures unchanged
(`Reader.Read`, `Reader.Seek`, `Reader.WriteTo`, `Reader.Close`,
`Writer.Write`, `Writer.ReadFrom`, `Lister.Close`, `Copier.Close`) and add
`Reader.ReadContext`, `Reader.SeekContext`, and `Writer.WriteContext` for
per-call cancellation.
- **Tests, examples, and documentation**:
- Add unit, race-stress, and live `libopendal_c` cancellation tests in
`bindings/go/cancel_test.go`.
- Update existing unit tests, behavior tests, guide example,
`bindings/go/README.md`, website Go docs, and add `bindings/go/upgrade.md`.
# Are there any user-facing changes?
Yes. This is a breaking API change in `bindings/go`:
- Pass a `context.Context` (such as your request context or
`context.Background()`) as the first argument to the updated `Operator`,
`Writer`, `Lister`, `Copier`, and `Deleter` methods.
- Use `Reader.ReadContext`, `Reader.SeekContext`, and `Writer.WriteContext`
when per-call cancellation is needed on streaming operations.
- Rebuild or upgrade `opendal-go-services` companion libraries against an
`opendal-c` version containing the `opendal_cancel_token_*` and `*_with_cancel`
symbols.
- Full migration details are documented in `bindings/go/upgrade.md`.
# Breaking changes
# AI Usage Statement
- Harness: pi
- Model: Gemini 4 Neon
- Effort: high
- Role: Implemented Go `context.Context` cancellation support across
`bindings/go` (FFI bindings, error unwrapping, public API migration, tests,
examples, upgrade guide, and website docs) and ran local `gofmt`,
`golangci-lint`, `go vet`, example/behavior-test compilation, `CGO_ENABLED=0`
smoke tests, and `go test -race` against a locally built `libopendal_c`
(`memory` and `fs`). Full remote behavior test matrices and website build
remain for CI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]