| 125 | } |
| 126 | |
| 127 | func (p *Processor) Handle(ctx context.Context, notification Notification) (*Payload, error) { |
| 128 | if p.Repository == nil { |
| 129 | return nil, ErrMissingRepository |
| 130 | } |
| 131 | |
| 132 | if notification.MessageID != "" { |
| 133 | state := p.Repository.Get() |
| 134 | if state.LastPushMessageID == notification.MessageID { |
| 135 | p.logf("watch: ignoring duplicate push %s", notification.MessageID) |
| 136 | |
| 137 | return nil, ErrNoNewMessages |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if err := p.checkRateLimitCircuit(p.currentTime()); err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | |
| 145 | startID, err := p.Repository.StartHistoryID(notification.HistoryID) |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | |
| 150 | if startID == 0 { |
| 151 | p.logStaleNotification(notification.HistoryID) |
| 152 | |
| 153 | return nil, ErrNoNewMessages |
| 154 | } |
| 155 | |
| 156 | if sleepErr := p.sleepForFetch(ctx); sleepErr != nil { |
| 157 | return nil, sleepErr |
| 158 | } |
| 159 | |
| 160 | if p.NewSource == nil { |
| 161 | return nil, ErrMissingSourceFactory |
| 162 | } |
| 163 | |
| 164 | source, err := p.NewSource(ctx) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | |
| 169 | if source == nil { |
| 170 | return nil, ErrMissingSource |
| 171 | } |
| 172 | |
| 173 | historyPage, err := source.ListHistory(ctx, startID, p.Config.HistoryMax, p.Config.HistoryTypes) |
| 174 | if err != nil { |
| 175 | if p.IsStaleHistoryError != nil && p.IsStaleHistoryError(err) { |
| 176 | return p.resync(ctx, source, notification) |
| 177 | } |
| 178 | |
| 179 | return nil, p.openRateLimitCircuitIfNeeded(err) |
| 180 | } |
| 181 | |
| 182 | nextHistoryID := notification.HistoryID |
| 183 | if historyPage.HistoryID != "" { |
| 184 | nextHistoryID = historyPage.HistoryID |