Skip to content

Add RetryIfValue option to retry on given values #107

@totemcaf

Description

@totemcaf

Sometimes want to retry if the computed values does not satisfy certain conditions. For example, you are waiting for some change to be applied to a persisted entity, so you want to retry until the entity read from persistence satisfy certain condition.

Example:

func (h *Handler) waitForApproval(ctx context.Context, id ids.Id) (*Entity, error) {
	return retry.DoWithData(func() (*Entity, error) {
		return h.reader.FindById(ctx, id)
        }, retry.RetryIfData(func (e *Entity) bool {
		return e.Status != StatusApproved // retry if the Status is not approved
       })
}

New type and func:

type RetryIfDataFunc[T any] func(T) bool

func RetryIfData[T any](f RetryIfDataFunc[T]) Option { // Option[T] ??
    // ...
}

It is challenging to handle type for Options to ensure consistency.

If you consider this to be useful (for me it is), I can provide a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions