Repository: etcd-io/etcd
File: cache/cache.go (Go)

Code before:
	"sync"
	"time"

	pb "go.etcd.io/etcd/api/v3/etcdserverpb"
	clientv3 "go.etcd.io/etcd/client/v3"
)

var (
	// TODO: add gap-free replay for arbitrary startRevs and drop this guard.
	// Returned when an option combination isn’t yet handled by the cache (e.g. WithPrevKV, WithProgressNotify for Watch(), WithCountOnly for Get()).
	ErrUnsupportedRequest = errors.New("cache: unsupported request parameters")
	// Returned when the requested key or key‑range is invalid (empty or reversed) or lies outside c.prefix.
	ErrKeyRangeInvalid = errors.New("cache: invalid or out‑of‑range key range")
)

// Cache buffers a single etcd Watch for a given key‐prefix and fan‑outs local watchers.
type Cache struct {
	prefix      string // prefix is the key-prefix this shard is responsible for ("" = root).
	cfg         Config // immutable runtime configuration
	watcher     clientv3.Watcher
	kv          clientv3.KV
	demux       *demux        // demux fans incoming events out to active watchers and manages resync.
	store       *store        // last‑observed snapshot
	mu          sync.Mutex    // protects isReady & readyChan
	isReady     bool          // true once snapshot loaded & watch confirmed
	readyChan   chan struct{} // closed exactly once per ready transition
	stop        context.CancelFunc
	waitGroup   sync.WaitGroup
	internalCtx context.Context
}

// New builds a cache shard that watches only the requested prefix.
// For the root cache pass "".
func New(client *clientv3.Client, prefix string, opts ...Option) (*Cache, error) {
	cfg := defaultConfig()
	for _, opt := range opts {
		opt(&cfg)
	}

	if cfg.HistoryWindowSize <= 0 {
		return nil, fmt.Errorf("invalid HistoryWindowSize %d (must be > 0)", cfg.HistoryWindowSize)
	}

	internalCtx, cancel := context.WithCancel(context.Background())

	cache := &Cache{
		prefix:      prefix,
		cfg:         cfg,
		watcher:     client.Watcher,
		kv:          client.KV,
		store:       newStore(),

Review comment (performance):
Please move it to separate structure like in https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apiserver/pkg/storage/cacher/ready.go

Fact that you have comments to explain fields just means that there are too many fields and you need to build hierarchy. 

Code after:
	"sync"
	"time"

	pb "go.etcd.io/etcd/api/v3/etcdserverpb"
	clientv3 "go.etcd.io/etcd/client/v3"
)

var (
	// TODO: add gap-free replay for arbitrary startRevs and drop this guard.
	// Returned when an option combination isn’t yet handled by the cache (e.g. WithPrevKV, WithProgressNotify for Watch(), WithCountOnly for Get()).
	ErrUnsupportedRequest = errors.New("cache: unsupported request parameters")
	// Returned when the requested key or key‑range is invalid (empty or reversed) or lies outside c.prefix.
	ErrKeyRangeInvalid = errors.New("cache: invalid or out‑of‑range key range")
)

// Cache buffers a single etcd Watch for a given key‐prefix and fan‑outs local watchers.
type Cache struct {
	prefix      string // prefix is the key-prefix this shard is responsible for ("" = root).
	cfg         Config // immutable runtime configuration
	watcher     clientv3.Watcher
	kv          clientv3.KV
	demux       *demux // demux fans incoming events out to active watchers and manages resync.
	store       *store // last‑observed snapshot
	ready       *ready
	stop        context.CancelFunc
	waitGroup   sync.WaitGroup
	internalCtx context.Context
}

// New builds a cache shard that watches only the requested prefix.
// For the root cache pass "".
func New(client *clientv3.Client, prefix string, opts ...Option) (*Cache, error) {
	cfg := defaultConfig()
	for _, opt := range opts {
		opt(&cfg)
	}

	if cfg.HistoryWindowSize <= 0 {
		return nil, fmt.Errorf("invalid HistoryWindowSize %d (must be > 0)", cfg.HistoryWindowSize)
	}

	internalCtx, cancel := context.WithCancel(context.Background())

	cache := &Cache{
		prefix:      prefix,
		cfg:         cfg,
		watcher:     client.Watcher,
		kv:          client.KV,
		store:       newStore(),
		ready:       newReady(),
		stop:        cancel,