Skip to content

minitauros/go-workonce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A workonce worker is useful when many goroutines need to perform the same job that is expected to have the same result for all of them. Instead of all the routines executing the work, only one routine is allowed to perform the work while the rest of the routines wait for the result.

Example

package main

import (
	"github.com/minitauros/go-workonce"
)

type WorkResult struct {
}

func main() {
	// Create a work worker that identifies the work uniquely by string and 
	// that runs work that returns a WorkResult.
	worker := workonce.NewWorker[string, WorkResult]()

	// Run the unique piece of work "refresh-login-token". If the work called 
	// "refresh-login-token" is already being executed by another goroutine, the
	// worker will return the WorkResult of the already active routine once it 
	// finishes.
	res, _ := worker.Run("refresh-login-token", func() (WorkResult, error) {
		return WorkResult{}, nil
	})

	// Do something with res.
	doSomethingWith(res)
}

About

Many goroutines want to do the same piece of work. Let one do the work and let the rest wait for the result.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages