Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Onces ¶
type Onces struct {
// contains filtered or unexported fields
}
Onces is an object that will try to (if asked) perform a successful action only if a specified interval has elapsed since the last successful action. An Onces must not be copied after first use. An Onces may be used by multiple goroutines simultaneously.
Example ¶
package main
import (
"fmt"
"sync"
"time"
"github.com/dushaoshuai/xsync"
)
func main() {
onces := xsync.OnceInterval(time.Second)
var wg sync.WaitGroup
for i := 0; i < 10_000_000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
onces.Do(func() error {
fmt.Println("Only once this second:", time.Now().Second())
return nil
})
}()
}
wg.Wait()
// Sample Output:
// Only once this second: 30
// Only once this second: 31
// Only once this second: 32
// Only once this second: 33
// Only once this second: 34
// Only once this second: 35
// Only once this second: 36
// Only once this second: 37
}
func OnceInterval ¶
OnceInterval returns a new Onces. It will panic if interval is less than or equal to 0.
Click to show internal directories.
Click to hide internal directories.