()
| 156 | } |
| 157 | |
| 158 | func (d *downstream) mainLoop() error { |
| 159 | lastAmountChanges := int64(0) |
| 160 | recheckInterval := 1700 |
| 161 | if !d.sync.Options.Polling { |
| 162 | recheckInterval = 500 |
| 163 | } |
| 164 | |
| 165 | var ( |
| 166 | changeTimer time.Time |
| 167 | ) |
| 168 | for { |
| 169 | select { |
| 170 | case <-d.sync.ctx.Done(): |
| 171 | return nil |
| 172 | case <-time.After(time.Duration(recheckInterval) * time.Millisecond): |
| 173 | break |
| 174 | } |
| 175 | |
| 176 | // Check for changes remotely |
| 177 | ctx, cancel := context.WithTimeout(d.sync.ctx, time.Minute*10) |
| 178 | changeAmount, err := d.client.ChangesCount(ctx, &remote.Empty{}) |
| 179 | cancel() |
| 180 | if err != nil { |
| 181 | return errors.Wrap(err, "count changes") |
| 182 | } |
| 183 | |
| 184 | // start waiting timer |
| 185 | if changeAmount.Amount > 0 && lastAmountChanges == 0 { |
| 186 | changeTimer = time.Now().Add(waitForMoreChangesTimeout) |
| 187 | } |
| 188 | |
| 189 | // Compare change amount |
| 190 | if lastAmountChanges > 0 && (time.Now().After(changeTimer) || changeAmount.Amount > 25000 || changeAmount.Amount == lastAmountChanges) { |
| 191 | d.sync.fileIndex.fileMapMutex.Lock() |
| 192 | changes, err := d.collectChanges(false) |
| 193 | d.sync.fileIndex.fileMapMutex.Unlock() |
| 194 | if err != nil { |
| 195 | return errors.Wrap(err, "collect changes") |
| 196 | } |
| 197 | |
| 198 | err = d.applyChanges(changes, false) |
| 199 | if err != nil { |
| 200 | return errors.Wrap(err, "apply changes") |
| 201 | } |
| 202 | |
| 203 | lastAmountChanges = 0 |
| 204 | changeTimer = time.Time{} |
| 205 | } else { |
| 206 | lastAmountChanges = changeAmount.Amount |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func (d *downstream) shouldKeep(change *remote.Change) bool { |
| 212 | // Is a delete change? |
nothing calls this directly
no test coverage detected