oncewait

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2022 License: MIT Imports: 1 Imported by: 2 modules, 1 package

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.

README

oncewait

Package oncewait offers slightly more synced sync.Once and its factory.

Usage

func ExampleOnceWaiter(){
    once := New()

    processFunc := func() {
        fmt.Println("start heavy process.")

        time.Sleep(time.Second) // do something

        fmt.Println("process finished.")
    }

    var wg sync.WaitGroup

    wg.Add(1)
    go func() {
        once.Do(processFunc)
        fmt.Println("done.")
        wg.Done()
    }()

    wg.Add(1)
    go func() {
        once.Do(processFunc)
        fmt.Println("done.")
        wg.Done()
    }()

    wg.Wait()

    // Output:
    // start heavy process.
    // process finished.
    // done.
    // done.
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

Factory offers OnceWaiter which is identified according to key string.

Example
package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/daichitakahashi/oncewait"
)

func main() {
	var factory oncewait.Factory
	const key = "example"

	processFunc := func() {
		defer factory.Refresh(key)
		fmt.Println("start heavy process.")

		time.Sleep(time.Second) // do something

		fmt.Println("process finished.")
	}

	var wg sync.WaitGroup

	for i := 0; i < 2; i++ {
		wg.Add(1)
		go func() {
			factory.Get(key).Do(processFunc)
			fmt.Println("done.")
			wg.Done()
		}()

		wg.Add(1)
		go func() {
			factory.Get(key).Do(processFunc)
			fmt.Println("done.")
			wg.Done()
		}()

		wg.Wait()
	}

}
Output:
start heavy process.
process finished.
done.
done.
start heavy process.
process finished.
done.
done.

func (*Factory) Get

func (f *Factory) Get(key string) *OnceWaiter

Get returns instance of OnceWaiter associated with the key. In the first Get call, new instance is set.

func (*Factory) Refresh

func (f *Factory) Refresh(key string)

Refresh replaces instance of OnceWaiter associated with the key. Usually Refresh should be called inside of Do's function f.

type OnceWaiter

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

OnceWaiter is an object that will perform exactly one action. But wait for finish of first Do.

Example
package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/daichitakahashi/oncewait"
)

func main() {
	once := oncewait.New()

	processFunc := func() {
		fmt.Println("start heavy process.")

		time.Sleep(time.Second) // do something

		fmt.Println("process finished.")
	}

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		once.Do(processFunc)
		fmt.Println("done.")
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		once.Do(processFunc)
		fmt.Println("done.")
		wg.Done()
	}()

	wg.Wait()

}
Output:
start heavy process.
process finished.
done.
done.

func New

func New() *OnceWaiter

New returns fresh OnceWaiter.

func (*OnceWaiter) Do

func (o *OnceWaiter) Do(f func())

Do calls the function f and only if Do is being called for the first time for this instance of OnceWaiter, same as standard package's sync.Once. While function f is executing, second call waits for finish.

Jump to

Keyboard shortcuts

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