2 Copyright 2012 Google Inc.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 // Package idempotency provides a duplicate function call suppression
18 // mechanism. It is a lightly modified version of groupcache's
19 // singleflight package that does not forget keys until explicitly
25 // call is an in-flight or completed Once call
32 // Group represents a class of work and forms a namespace in which
33 // units of work can be executed with duplicate suppression.
35 mu sync.Mutex // protects m
36 m map[string]*call // lazily initialized
39 // Once executes and returns the results of the given function, making
40 // sure that only one execution for a given key happens until the
41 // key is explicitly forgotten. If a duplicate comes in, the duplicate
42 // caller waits for the original to complete and receives the same results.
43 func (g *Group) Once(key string, fn func() (interface{}, error)) (interface{}, error) {
46 g.m = make(map[string]*call)
48 if c, ok := g.m[key]; ok {
69 // Forget forgets a key, allowing the next call for the key to execute
71 func (g *Group) Forget(key string) {