Giter VIP home page Giter VIP logo

singleflight's Introduction

Singleflight

Package singleflight provides a duplicate function call suppression mechanism. It's a generic supported wrapper of golang.org/x/sync/singleflight.

Install

go get -u github.com/hsblhsn/singleflight

Usage

For detailed usage please refer to the golang.org/x/sync/singleflight package documentation.

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/hsblhsn/singleflight"
)

func main() {
	g := singleflight.NewGroup[int]()
	inputs := []int{0, 1, 1, 1, 2, 2, 2, 3, 3, 3}
	for _, v := range inputs {
		go func(input int) {
			key := fmt.Sprintf("key-%d", input)
			result, _, _ := g.Do(key, func() (int, error) {
				// simulate a slow function
				time.Sleep(time.Second)
				log.Println("[SLOW_FUNCTION_CALL]", input)
				return input * input, nil
			})
			log.Println("[OUTPUT]", input, result)
		}(v)
	}
	// wait for all goroutines to finish.
	// in real world this should be done by a sync.WaitGroup or similar.
	time.Sleep(2 * time.Second)
}

Playground

2009/11/10 23:00:01 [SLOW_FUNCTION_CALL] 3
2009/11/10 23:00:01 [OUTPUT] 3 9
2009/11/10 23:00:01 [SLOW_FUNCTION_CALL] 1
2009/11/10 23:00:01 [OUTPUT] 1 1
2009/11/10 23:00:01 [OUTPUT] 1 1
2009/11/10 23:00:01 [OUTPUT] 3 9
2009/11/10 23:00:01 [OUTPUT] 3 9
2009/11/10 23:00:01 [SLOW_FUNCTION_CALL] 0
2009/11/10 23:00:01 [OUTPUT] 0 0
2009/11/10 23:00:01 [SLOW_FUNCTION_CALL] 2
2009/11/10 23:00:01 [OUTPUT] 2 4
2009/11/10 23:00:01 [OUTPUT] 2 4
2009/11/10 23:00:01 [OUTPUT] 2 4
2009/11/10 23:00:01 [OUTPUT] 1 1

So you can see that the slow function is called only once for each unique input. But the output is returned to all callers. That's how singleflight works.

License

No license. Do whatever you want with this code. I am not responsible for anything.

singleflight's People

Contributors

hsblhsn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.