beacon/light/api: update

This commit is contained in:
Felix Lange 2024-03-22 13:17:09 +01:00
parent 48ea323c3f
commit 4ab086bf95

View file

@ -517,13 +517,7 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func()
// startEventStream establishes an event stream. This will keep retrying until the stream has been // startEventStream establishes an event stream. This will keep retrying until the stream has been
// established. It can only return nil when the context is canceled. // established. It can only return nil when the context is canceled.
func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadEventListener) *eventsource.Stream { func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadEventListener) *eventsource.Stream {
for initial := true; ; initial = false { for retry := true; retry; retry = ctxSleep(ctx, 5*time.Second) {
if !initial {
if ctxSleep(ctx, 5*time.Second) {
return nil
}
}
path := "/eth/v1/events?topics=head&topics=light_client_optimistic_update&topics=light_client_finality_update" path := "/eth/v1/events?topics=head&topics=light_client_optimistic_update&topics=light_client_finality_update"
req, err := http.NewRequestWithContext(ctx, "GET", api.url+path, nil) req, err := http.NewRequestWithContext(ctx, "GET", api.url+path, nil)
if err != nil { if err != nil {
@ -540,15 +534,16 @@ func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadE
} }
return stream return stream
} }
return nil
} }
func ctxSleep(ctx context.Context, timeout time.Duration) (interrupted bool) { func ctxSleep(ctx context.Context, timeout time.Duration) (ok bool) {
timer := time.NewTimer(timeout) timer := time.NewTimer(timeout)
defer timer.Stop() defer timer.Stop()
select { select {
case <-timer.C: case <-timer.C:
return false
case <-ctx.Done():
return true return true
case <-ctx.Done():
return false
} }
} }