ipld-eth-beacon-indexer/pkg/beaconclient/incomingsse.go

93 lines
3.1 KiB
Go
Raw Normal View History

// VulcanizeDB
// Copyright © 2022 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This package will handle all event subscriptions that utilize SSE.
package beaconclient
import (
2022-06-21 18:34:10 +00:00
"context"
"encoding/json"
"time"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
)
// This function will capture all the SSE events for a given SseEvents object.
// When new messages come in, it will ensure that they are decoded into JSON.
// If any errors occur, it log the error information.
2022-06-21 20:42:51 +00:00
func handleIncomingSseEvent[P ProcessedEvents](ctx context.Context, eventHandler *SseEvents[P], errMetricInc func(uint64), skipSse bool) {
if !skipSse {
for {
err := eventHandler.SseClient.SubscribeChanRawWithContext(ctx, eventHandler.MessagesCh)
if err != nil {
2022-06-21 20:42:51 +00:00
loghelper.LogEndpoint(eventHandler.Endpoint).WithFields(log.Fields{
"err": err}).Error("We are unable to subscribe to the SSE endpoint")
time.Sleep(3 * time.Second)
continue
}
loghelper.LogEndpoint(eventHandler.Endpoint).Info("Successfully subscribed to the event stream.")
2022-06-21 20:42:51 +00:00
break
}
2022-06-21 20:42:51 +00:00
}
for {
select {
2022-06-21 18:34:10 +00:00
case <-ctx.Done():
2022-06-23 12:50:09 +00:00
close(eventHandler.ProcessCh)
2022-06-21 18:34:10 +00:00
return
case message := <-eventHandler.MessagesCh:
// Message can be nil if its a keep-alive message
if len(message.Data) != 0 {
log.WithFields(log.Fields{"msg": string(message.Data)}).Debug("We are going to send the following message to be processed.")
go processMsg(message.Data, eventHandler.ProcessCh, eventHandler.ErrorCh)
}
case headErr := <-eventHandler.ErrorCh:
log.WithFields(log.Fields{
2022-04-27 18:01:59 +00:00
"endpoint": eventHandler.Endpoint,
"err": headErr.err,
"msg": headErr.msg,
},
).Error("Unable to handle event.")
errMetricInc(1)
}
}
}
// Turn the data object into a Struct.
2022-06-23 11:50:36 +00:00
func processMsg[P ProcessedEvents](msg []byte, processCh chan<- P, errorCh chan<- SseError) {
var msgMarshaled P
err := json.Unmarshal(msg, &msgMarshaled)
if err != nil {
loghelper.LogError(err).Error("Unable to parse message")
2022-06-23 11:50:36 +00:00
errorCh <- SseError{
err: err,
msg: msg,
}
return
}
2022-06-23 11:50:36 +00:00
processCh <- msgMarshaled
2022-06-23 12:07:19 +00:00
log.Debug("Done sending")
}
// Capture all of the event topics.
2022-06-21 20:42:51 +00:00
func (bc *BeaconClient) captureEventTopic(ctx context.Context, skipSse bool) {
log.Info("We are capturing all SSE events")
2022-06-21 20:42:51 +00:00
go handleIncomingSseEvent(ctx, bc.HeadTracking, bc.Metrics.IncrementHeadError, skipSse)
go handleIncomingSseEvent(ctx, bc.ReOrgTracking, bc.Metrics.IncrementReorgError, skipSse)
}