actor: add CompleteWith and AwaitFuture generic package-level helpers

In this commit, we add two package-level generic helpers to the actor
module as part of the broader chan error -> Future[error] migration in
the discovery package.

CompleteWith[T](p Promise[T], val T) is a one-liner convenience wrapper
over p.Complete(fn.Ok(val)). It lets callers complete a promise with a
plain value without constructing an fn.Result inline, which cuts noise
at every completion site.

AwaitFuture[T](ctx, f Future[T]) (T, error) provides the symmetric
receive side: it blocks until the future resolves or the context is
cancelled and returns the value and any context error unpacked from
the fn.Result, matching the (val, err) convention callers expect.

Both functions are deliberately thin (no policy, no timeout, no new
state) so they compose freely with higher-level helpers built on top,
e.g. discovery.AwaitGossipResult.

go.mod is updated to pin the actor module via a local replace directive
so the rest of the lnd module picks up these additions without waiting
for a tagged release.
This commit is contained in:
Olaoluwa Osuntokun 2026-02-17 19:15:06 -08:00
parent 0a2f48625b
commit 4e0992fa4e
2 changed files with 21 additions and 4 deletions

View file

@ -17,6 +17,22 @@ type promiseImpl[T any] struct {
fut *futureImpl[T]
}
// CompleteWith completes a promise with the given value, wrapping it as a
// successful result. This is a convenience wrapper over
// promise.Complete(fn.Ok(val)). Safe to call multiple times; only the first
// call takes effect.
func CompleteWith[T any](p Promise[T], val T) {
p.Complete(fn.Ok(val))
}
// AwaitFuture blocks until the future resolves or the context is cancelled.
// On success, it returns the resolved value and a nil error. If the context
// is cancelled before the future resolves, it returns the zero value of T and
// the context cancellation error.
func AwaitFuture[T any](ctx context.Context, f Future[T]) (T, error) {
return f.Await(ctx).Unpack()
}
// NewPromise creates a new Promise. The associated Future, which consumers can
// use to await the result, can be obtained via the Future() method. The Future
// is completed by calling the Complete() method on this Promise.

9
go.mod
View file

@ -33,7 +33,7 @@ require (
github.com/lightninglabs/neutrino v0.16.2
github.com/lightninglabs/neutrino/cache v1.1.3
github.com/lightningnetwork/lightning-onion v1.3.0
github.com/lightningnetwork/lnd/actor v0.0.3
github.com/lightningnetwork/lnd/actor v0.0.5
github.com/lightningnetwork/lnd/cert v1.2.2
github.com/lightningnetwork/lnd/clock v1.1.1
github.com/lightningnetwork/lnd/fn/v2 v2.0.9
@ -204,15 +204,16 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)
// TODO(gijs): remove once new actor package is released.
replace github.com/lightningnetwork/lnd/actor => ./actor
// TODO(gijs): remove once new queue package is released.
replace github.com/lightningnetwork/lnd/queue => ./queue
// TODO(elle): remove once the gossip V2 sqldb changes have been made.
replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
// Use local actor module to pick up CompleteWith/AwaitFuture helpers added
// as part of the discovery errChan -> Future[error] migration.
replace github.com/lightningnetwork/lnd/actor => ./actor
// This replace is for https://github.com/advisories/GHSA-25xm-hr59-7c27
replace github.com/ulikunitz/xz => github.com/ulikunitz/xz v0.5.11