HOUSEKEEPING: Initializes a new changeCache. lastSequence is the last known database sequence assigned. notifyChangeFunc is an optional function that will be called to notify of channel changes. After calling Init(), you must call .Start() to start using the cache, otherwise it will be in a locked s
(ctx context.Context, dbContext *DatabaseContext, channelCache ChannelCache, notifyChange func(context.Context, channels.Set), options *CacheOptions, metaKeys *base.MetadataKeys)
| 150 | // and callers will block on trying to obtain the lock. |
| 151 | |
| 152 | func (c *changeCache) Init(ctx context.Context, dbContext *DatabaseContext, channelCache ChannelCache, notifyChange func(context.Context, channels.Set), options *CacheOptions, metaKeys *base.MetadataKeys) error { |
| 153 | c.db = dbContext |
| 154 | c.logCtx = ctx |
| 155 | |
| 156 | c.notifyChangeFunc = notifyChange |
| 157 | c.receivedSeqs = make(map[uint64]struct{}) |
| 158 | c.terminator = make(chan bool) |
| 159 | c.initTime = time.Now() |
| 160 | c.skippedSeqs = NewSkippedSequenceSkiplist() |
| 161 | c.lastAddPendingTime = time.Now().UnixNano() |
| 162 | c.sgCfgPrefix = dbContext.MetadataKeys.SGCfgPrefix(c.db.Options.GroupID) |
| 163 | c.metaKeys = metaKeys |
| 164 | |
| 165 | // init cache options |
| 166 | if options != nil { |
| 167 | c.options = *options |
| 168 | } else { |
| 169 | c.options = DefaultCacheOptions() |
| 170 | } |
| 171 | |
| 172 | c.channelCache = channelCache |
| 173 | |
| 174 | base.InfofCtx(ctx, base.KeyCache, "Initializing changes cache for %s with options %+v", base.UD(c.db.Name), c.options) |
| 175 | |
| 176 | heap.Init(&c.pendingLogs) |
| 177 | |
| 178 | // background tasks that perform housekeeping duties on the cache |
| 179 | bgt, err := NewBackgroundTask(ctx, "InsertPendingEntries", c.InsertPendingEntries, c.options.CachePendingSeqMaxWait/2, c.terminator) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | c.backgroundTasks = append(c.backgroundTasks, bgt) |
| 184 | |
| 185 | bgt, err = NewBackgroundTask(ctx, "CleanSkippedSequenceQueue", c.CleanSkippedSequenceQueue, c.options.CacheSkippedSeqMaxWait/2, c.terminator) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | c.backgroundTasks = append(c.backgroundTasks, bgt) |
| 190 | |
| 191 | // Lock the cache -- not usable until .Start() called. This fixes the DCP startup race condition documented in SG #3558. |
| 192 | c.lock.Lock() |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | func (c *changeCache) Start(initialSequence uint64) error { |
| 197 |