2022-05-18 16:12:54 +00:00
|
|
|
// 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/>.
|
2022-04-26 17:57:01 +00:00
|
|
|
// This package will handle all event subscriptions that utilize SSE.
|
|
|
|
|
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-29 01:39:56 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-04-26 17:57:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-09 21:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
|
2022-05-06 15:03:15 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-04-26 17:57:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
shutdownWaitInterval = time.Duration(5) * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-09-29 01:39:56 +00:00
|
|
|
func handleIncomingSseEvent[P ProcessedEvents](eventHandler *SseEvents[P], errMetricInc func(uint64), idleTimeout time.Duration) {
|
2022-05-12 13:52:13 +00:00
|
|
|
go func() {
|
|
|
|
errG := new(errgroup.Group)
|
|
|
|
errG.Go(func() error {
|
2022-09-29 01:39:56 +00:00
|
|
|
err := eventHandler.Connect()
|
2022-05-12 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err := errG.Wait(); err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
"endpoint": eventHandler.Endpoint,
|
|
|
|
}).Error("Unable to subscribe to the SSE endpoint.")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
loghelper.LogEndpoint(eventHandler.Endpoint).Info("Successfully subscribed to the event stream.")
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-05-12 13:52:13 +00:00
|
|
|
|
|
|
|
}()
|
2022-09-29 01:39:56 +00:00
|
|
|
|
|
|
|
// TODO(telackey): Doesn't there need to be a check here that the handler hasn't been shutdown?
|
2022-04-26 17:57:01 +00:00
|
|
|
for {
|
2022-09-29 01:39:56 +00:00
|
|
|
var idleTimer *time.Timer = nil
|
|
|
|
var idleTimerC <-chan time.Time = nil
|
|
|
|
if idleTimeout > 0 {
|
|
|
|
idleTimer = time.NewTimer(idleTimeout)
|
|
|
|
idleTimerC = idleTimer.C
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
select {
|
|
|
|
case message := <-eventHandler.MessagesCh:
|
2022-09-29 01:39:56 +00:00
|
|
|
if nil != idleTimer {
|
|
|
|
idleTimer.Stop()
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
// Message can be nil if its a keep-alive message
|
|
|
|
if len(message.Data) != 0 {
|
2022-05-17 20:05:15 +00:00
|
|
|
log.WithFields(log.Fields{"msg": string(message.Data)}).Debug("We are going to send the following message to be processed.")
|
2022-04-26 17:57:01 +00:00
|
|
|
go processMsg(message.Data, eventHandler.ProcessCh, eventHandler.ErrorCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
case headErr := <-eventHandler.ErrorCh:
|
2022-09-29 01:39:56 +00:00
|
|
|
if nil != idleTimer {
|
|
|
|
idleTimer.Stop()
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
log.WithFields(log.Fields{
|
2022-04-27 18:01:59 +00:00
|
|
|
"endpoint": eventHandler.Endpoint,
|
|
|
|
"err": headErr.err,
|
|
|
|
"msg": headErr.msg,
|
2022-04-26 17:57:01 +00:00
|
|
|
},
|
|
|
|
).Error("Unable to handle event.")
|
2022-05-17 20:05:15 +00:00
|
|
|
errMetricInc(1)
|
2022-09-29 01:39:56 +00:00
|
|
|
|
|
|
|
case <-idleTimerC:
|
|
|
|
err := errors.New("SSE idle timeout")
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": eventHandler.Endpoint,
|
|
|
|
"err": err,
|
|
|
|
"msg": err.Error(),
|
|
|
|
},
|
|
|
|
).Error("TIMEOUT - Attempting to resubscribe")
|
|
|
|
errMetricInc(1)
|
|
|
|
eventHandler.Disconnect()
|
|
|
|
err = eventHandler.Connect()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to re-subscribe.", err)
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the data object into a Struct.
|
|
|
|
func processMsg[P ProcessedEvents](msg []byte, processCh chan<- *P, errorCh chan<- *SseError) {
|
|
|
|
var msgMarshaled P
|
|
|
|
err := json.Unmarshal(msg, &msgMarshaled)
|
|
|
|
if err != nil {
|
2022-04-27 14:28:42 +00:00
|
|
|
loghelper.LogError(err).Error("Unable to parse message")
|
2022-04-26 17:57:01 +00:00
|
|
|
errorCh <- &SseError{
|
|
|
|
err: err,
|
|
|
|
msg: msg,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
processCh <- &msgMarshaled
|
|
|
|
}
|
2022-04-27 14:28:42 +00:00
|
|
|
|
|
|
|
// Capture all of the event topics.
|
|
|
|
func (bc *BeaconClient) captureEventTopic() {
|
|
|
|
log.Info("We are capturing all SSE events")
|
2022-09-29 01:39:56 +00:00
|
|
|
go handleIncomingSseEvent(bc.HeadTracking, bc.Metrics.IncrementHeadError, time.Second*30)
|
|
|
|
go handleIncomingSseEvent(bc.ReOrgTracking, bc.Metrics.IncrementReorgError, 0)
|
2022-04-27 14:28:42 +00:00
|
|
|
}
|