type Function … type RefCounted … type Promise … // NewPromise returns a promise for the future result of calling the // specified function. // // The debug string is used to classify promises in logs and metrics. // It should be drawn from a small set. func NewPromise(debug string, function Function) *Promise { … } type state … const stateIdle … const stateRunning … const stateCompleted … // Cached returns the value associated with a promise. // // It will never cause the value to be generated. // It will return the cached value, if present. func (p *Promise) Cached() interface{ … } // Get returns the value associated with a promise. // // All calls to Promise.Get on a given promise return the // same result but the function is called (to completion) at most once. // // If the value is not yet ready, the underlying function will be invoked. // // If ctx is cancelled, Get returns (nil, Canceled). // If all concurrent calls to Get are cancelled, the context provided // to the function is cancelled. A later call to Get may attempt to // call the function again. func (p *Promise) Get(ctx context.Context, arg interface{ … } // run starts p.function and returns the result. p.mu must be locked. func (p *Promise) run(ctx context.Context, arg interface{ … } // wait waits for the value to be computed, or ctx to be cancelled. p.mu must be locked. func (p *Promise) wait(ctx context.Context) (interface{ … } type EvictionPolicy … const ImmediatelyEvict … const NeverEvict … type Store … // NewStore creates a new store with the given eviction policy. func NewStore(policy EvictionPolicy) *Store { … } // Promise returns a reference-counted promise for the future result of // calling the specified function. // // Calls to Promise with the same key return the same promise, incrementing its // reference count. The caller must call the returned function to decrement // the promise's reference count when it is no longer needed. The returned // function must not be called more than once. // // Once the last reference has been released, the promise is removed from the // store. func (store *Store) Promise(key interface{ … } // Stats returns the number of each type of key in the store. func (s *Store) Stats() map[reflect.Type]int { … } // DebugOnlyIterate iterates through the store and, for each completed // promise, calls f(k, v) for the map key k and function result v. It // should only be used for debugging purposes. func (s *Store) DebugOnlyIterate(f func(k, v interface{ … }