()
| 147 | } |
| 148 | |
| 149 | func (w *PollingClient) pollHeads() { |
| 150 | |
| 151 | w.reqPoll() |
| 152 | |
| 153 | defer close(w.closedCh) |
| 154 | |
| 155 | for { |
| 156 | select { |
| 157 | case <-w.pollReqCh: |
| 158 | head, err := w.queryHeader() |
| 159 | |
| 160 | if err != nil { |
| 161 | w.lg.Info("Error getting latest header", "err", err) |
| 162 | w.scheduleNextPoll(nil) |
| 163 | continue |
| 164 | } |
| 165 | if w.currHead != nil && w.currHead.Hash() == head.Hash() { |
| 166 | w.lg.Trace("No change in head, skipping notifications") |
| 167 | w.scheduleNextPoll(head) |
| 168 | continue |
| 169 | } |
| 170 | |
| 171 | headTime := time.Unix(int64(head.Time), 0) |
| 172 | w.lg.Trace( |
| 173 | "Notifying subscribers of new head", |
| 174 | "height", head.Number, |
| 175 | "headTime", headTime.Format("15:04:05"), |
| 176 | "head", head.Hash(), |
| 177 | ) |
| 178 | w.currHead = head |
| 179 | w.mtx.RLock() |
| 180 | for _, sub := range w.subs { |
| 181 | sub <- head |
| 182 | } |
| 183 | w.mtx.RUnlock() |
| 184 | w.scheduleNextPoll(head) |
| 185 | case <-w.ctx.Done(): |
| 186 | w.Client.Close() |
| 187 | return |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func (w *PollingClient) getLatestHeader() (*types.Header, error) { |
| 193 | ctx, cancel := context.WithTimeout(w.ctx, 5*time.Second) |
no test coverage detected