Documentation
¶
Overview ¶
Package progress offers a simple, standardized interface for monitoring progress in your applications.
This package exposes the progress.Reader interface, designed to be similar in spirit to io.Reader but for progress reporting. This interface makes tracking long-running tasks and their progress a breeze.
The interface defines two key methods:
- DoneChan() returns a channel that signals when the task is done, and a boolean indicating if the task is completed. - Count() provides the current count of finished tasks and the total number of tasks.
In addition to the interface, the package provides several useful tools:
- progress.NewLongRunningJob: A concurrency-safe utility for instrumenting long-running functions with a progress.Reader. - progress.Extend(Reader): A simple wrapper that adds useful methods to a Reader, such as estimating remaining time. - progress.Logger(Reader): Starts a goroutine to log updates from a Reader. - progress.WaitGroup: A drop-in replacement for sync.WaitGroup that implements the Reader interface.
By using this package, you can make your long-running tasks and functions more informative and user-friendly, improving the overall experience for your library consumers.
For those interested in contributing, there are several areas where the functionality of this package could be extended, such as satisfying progress.Reader with just a count, returning a progress.Reader from a slice or channel, creating a pretty terminal progress bar, or experimenting with passing return types through.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewReaderFromCount ¶
NewReaderFromCount returns an implementation of Reader from just a count callback. It will regularly call the count function to determine the latest progress count. Note that `count` must be safe to call at any time. It will be considered complete when the callback returns equal numbers.
Types ¶
type LongRunningJob ¶
type LongRunningJob struct { ReaderWrapper // contains filtered or unexported fields }
func NewLongRunningJob ¶
func NewLongRunningJob() LongRunningJob
NewLongRunningJob returns a basic task tracker. All methods are safe to call concurrently. It already uses Extend to provide utility methods.
func (LongRunningJob) AddWork ¶
func (t LongRunningJob) AddWork(items uint64)
AddWork adds to the total work.
func (LongRunningJob) Complete ¶
func (t LongRunningJob) Complete()
func (LongRunningJob) Count ¶
func (t LongRunningJob) Count() (uint64, uint64)
Count returns the finished work and total work. Note that the task itself counts as 1 piece of work.
func (LongRunningJob) DoneChan ¶
func (t LongRunningJob) DoneChan() (chan struct{}, bool)
func (LongRunningJob) FinishWork ¶
func (t LongRunningJob) FinishWork(items uint64)
FinishWork progresses the job. When the amount of finished work equals or exceed the total work, the job is marked as completed.
type Reader ¶
type Reader interface { // DoneChan returns a channel that will be closed upon completion. // The boolean return specifies whether the channel is already closed. DoneChan() (chan struct{}, bool) // Count returns the amount of items processed and the total amount. // The amount of items is guaranteed to be less than or equal to the total. // The total amount is guaranteed to be at least 1. // If they are equal, it is guaranteed that InProgress will return false. // Both values may increase at any time and are guaranteed never to decrease. // Count must be safe to be called concurrently. Count() (uint64, uint64) }
Reader is an interface that all progress trackers should satisfy. This allows for other libraries to work with progress trackers generally, and for library developers to create compatible progress trackers.
type ReaderWrapper ¶
type ReaderWrapper struct { Reader // contains filtered or unexported fields }
func Extend ¶
func Extend(t Reader) ReaderWrapper
Extend adds some nice utility methods to a Reader. Reader implementations can just embed this.
func (ReaderWrapper) InProgress ¶
func (s ReaderWrapper) InProgress() bool
InProgress will return false when the tracker is complete. It is debounced to try and return false for 200ms. After 200ms it will finally return true.
func (ReaderWrapper) PerSecond ¶
func (s ReaderWrapper) PerSecond() float64
PerSecond returns the throughput since this tracker began.
func (ReaderWrapper) Percentage ¶
func (s ReaderWrapper) Percentage() float64
Percentage returns the current progress as a percentage between 0 and 1.
func (ReaderWrapper) Remaining ¶
func (s ReaderWrapper) Remaining() time.Duration
Remaining estimates the time remaining until completion.
type WaitGroup ¶
type WaitGroup struct { LongRunningJob // contains filtered or unexported fields }
WaitGroup is a dropin replacement for sync.WaitGroup that provides progress tracking.