idempotent

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package idempotent provides a SQLite-backed idempotency guard. It stores a SHA-256 hash of the caller-provided key and the result of the first execution. Subsequent calls with the same key return the cached result without re-executing the function.

Index

Constants

View Source
const Schema = `` /* 278-byte string literal not displayed */

Schema creates the idempotent_log table.

Variables

This section is empty.

Functions

This section is empty.

Types

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard provides idempotent execution backed by SQLite.

func New

func New(db *sql.DB) *Guard

New creates a Guard backed by the given database. Call Init() to create the table.

func (*Guard) Count

func (g *Guard) Count(ctx context.Context) (int64, error)

Count returns the number of entries in the log.

func (*Guard) Delete

func (g *Guard) Delete(ctx context.Context, key string) error

Delete removes a specific key from the log, allowing re-execution.

func (*Guard) Init

func (g *Guard) Init() error

Init creates the idempotent_log table. Idempotent.

func (*Guard) Once

func (g *Guard) Once(ctx context.Context, key string, fn func() ([]byte, error)) ([]byte, error)

Once executes fn only if key has not been seen before. If key was already processed, returns the cached result. The key is hashed with SHA-256 before storage.

If fn returns an error, the error message is stored and returned on subsequent calls (the function is NOT retried).

func (*Guard) Prune

func (g *Guard) Prune(ctx context.Context, olderThan time.Duration) (int64, error)

Prune deletes entries older than the given duration. Returns the number of deleted rows.

func (*Guard) Seen

func (g *Guard) Seen(ctx context.Context, key string) (bool, error)

Seen checks whether a key has been processed before.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL