github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/vndr/godl/singleflight/singleflight.go (about)

     1  // Copyright 2013 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package singleflight provides a duplicate function call suppression
     6  // mechanism.
     7  package singleflight
     8  
     9  import "sync"
    10  
    11  // call is an in-flight or completed singleflight.Do call
    12  type call struct {
    13  	wg sync.WaitGroup
    14  
    15  	// These fields are written once before the WaitGroup is done
    16  	// and are only read after the WaitGroup is done.
    17  	val interface{}
    18  	err error
    19  
    20  	// These fields are read and written with the singleflight
    21  	// mutex held before the WaitGroup is done, and are read but
    22  	// not written after the WaitGroup is done.
    23  	dups  int
    24  	chans []chan<- Result
    25  }
    26  
    27  // Group represents a class of work and forms a namespace in
    28  // which units of work can be executed with duplicate suppression.
    29  type Group struct {
    30  	mu sync.Mutex       // protects m
    31  	m  map[string]*call // lazily initialized
    32  }
    33  
    34  // Result holds the results of Do, so they can be passed
    35  // on a channel.
    36  type Result struct {
    37  	Val    interface{}
    38  	Err    error
    39  	Shared bool
    40  }
    41  
    42  // Do executes and returns the results of the given function, making
    43  // sure that only one execution is in-flight for a given key at a
    44  // time. If a duplicate comes in, the duplicate caller waits for the
    45  // original to complete and receives the same results.
    46  func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error) {
    47  	g.mu.Lock()
    48  	if g.m == nil {
    49  		g.m = make(map[string]*call)
    50  	}
    51  	if c, ok := g.m[key]; ok {
    52  		c.dups++
    53  		g.mu.Unlock()
    54  		c.wg.Wait()
    55  		return c.val, c.err
    56  	}
    57  	c := new(call)
    58  	c.wg.Add(1)
    59  	g.m[key] = c
    60  	g.mu.Unlock()
    61  
    62  	g.doCall(c, key, fn)
    63  	return c.val, c.err
    64  }
    65  
    66  // DoChan is like Do but returns a channel that will receive the
    67  // results when they are ready.
    68  func (g *Group) DoChan(key string, fn func() (interface{}, error)) <-chan Result {
    69  	ch := make(chan Result, 1)
    70  	g.mu.Lock()
    71  	if g.m == nil {
    72  		g.m = make(map[string]*call)
    73  	}
    74  	if c, ok := g.m[key]; ok {
    75  		c.dups++
    76  		c.chans = append(c.chans, ch)
    77  		g.mu.Unlock()
    78  		return ch
    79  	}
    80  	c := &call{chans: []chan<- Result{ch}}
    81  	c.wg.Add(1)
    82  	g.m[key] = c
    83  	g.mu.Unlock()
    84  
    85  	go g.doCall(c, key, fn)
    86  
    87  	return ch
    88  }
    89  
    90  // doCall handles the single call for a key.
    91  func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) {
    92  	c.val, c.err = fn()
    93  	c.wg.Done()
    94  
    95  	g.mu.Lock()
    96  	delete(g.m, key)
    97  	for _, ch := range c.chans {
    98  		ch <- Result{c.val, c.err, c.dups > 0}
    99  	}
   100  	g.mu.Unlock()
   101  }
   102  
   103  // Forget tells the singleflight to forget about a key.  Future calls
   104  // to Do for this key will call the function rather than waiting for
   105  // an earlier call to complete.
   106  func (g *Group) Forget(key string) {
   107  	g.mu.Lock()
   108  	delete(g.m, key)
   109  	g.mu.Unlock()
   110  }