Skip to content

Do

Executes a function synchronously with the full middleware chain (resilience, telemetry, tracing) and returns the error directly.

Signature

go
func Do(ctx context.Context, fn Func, opts ...GoOption) error

Parameters

ParameterTypeDescription
ctxcontext.ContextContext for the operation.
fnFuncThe function to execute. Signature: func(ctx context.Context) error
opts...GoOptionFunctional options. Accepts any baseOpt or goOnlyOpt.

Options

Naming

OptionDescription
WithName(name)Custom metric/tracing label. Default: "gofuncy.do"

Resilience

OptionDescription
WithTimeout(d)Per-invocation timeout. Each retry attempt gets a fresh deadline.
WithRetry(n, opts...)Automatic retry with configurable backoff.
WithCircuitBreaker(cb)Fail fast on broken dependencies. Stateful — share across calls.
WithFallback(fn, opts...)Called when the operation fails. Return nil to suppress the error.

Telemetry

OptionDefaultDescription
WithoutTracing()onDisable span creation.
WithDetachedTrace()variesRoot span linked to parent instead of child span. Default for Go/Start/StartWithReady/StartWithStop/GoWithCancel.
WithChildTrace()variesForce child span. Default for Do/Wait/NewGroup.
WithoutStartedCounter()onDisable started counter.
WithoutErrorCounter()onDisable error counter.
WithoutActiveUpDownCounter()onDisable active counter.
WithDurationHistogram()offEnable duration histogram.
WithMeterProvider(mp)globalCustom OTel meter provider.
WithTracerProvider(tp)globalCustom OTel tracer provider.

Concurrency

OptionDescription
WithLimiter(sem)Shared *semaphore.Weighted for cross-callsite concurrency control.

Middleware

OptionDescription
WithMiddleware(m...)Append custom middleware. Applied after resilience, before telemetry.
WithLogger(l)Custom *slog.Logger for error reporting.
WithStallThreshold(d)Log a warning if the goroutine runs longer than d.
WithStallHandler(h)Custom callback for stall detection.

Error Handling

OptionDescription
WithCallerSkip(n)Adjust stack depth for span caller attributes.

Behavior

  1. The full middleware chain is built: panic recovery, resilience (timeout, retry, circuit breaker, fallback), user middlewares, metrics, tracing, stall detection.
  2. If a WithLimiter semaphore is set, it is acquired before execution and released when complete.
  3. The function runs synchronously on the calling goroutine.
  4. The error is returned directly to the caller.

Do vs Wait vs Go

DoWaitGo
ExecutionSynchronousAsync — returns wait functionAsync — fire-and-forget
Error handlingReturns errorWait function returns errorErrorHandler callback
Use caseInline call with resilienceLaunch now, collect result laterBackground work

Example

go
// Synchronous call with retry and timeout
err := gofuncy.Do(ctx, func(ctx context.Context) error {
    user, err := api.GetUser(ctx, userID)
    if err != nil {
        return err
    }
    // process user...
    return nil
},
    gofuncy.WithTimeout(5*time.Second),
    gofuncy.WithRetry(3),
)
if err != nil {
    return fmt.Errorf("fetching user: %w", err)
}