// run is the workhorse of the stream pump system. It facilitates the triggering // of open streams by closing a new channel every time the input pump sends. func run() { for { select { case _, more := <-pump: log.Debug(ctx, "sse pump") prev := nextTick nextTick = make(chan struct{}) // trigger all listeners by closing the nextTick channel close(prev) if !more { return } case <-ctx.Done(): pump = nil return } } }
// NewLedgerClosePump starts a background proc that continually watches the // history database provided. The watch is stopped after the provided context // is cancelled. // // Every second, the proc spawned by calling this func will check to see // if a new ledger has been imported (by ruby-horizon as of 2015-04-30, but // should eventually end up being in this project). If a new ledger is seen // the the channel returned by this function emits func NewLedgerClosePump(ctx context.Context, db *sqlx.DB) <-chan struct{} { result := make(chan struct{}) go func() { var lastSeenLedger int32 for { select { case <-time.After(1 * time.Second): var latestLedger int32 row := db.QueryRow(MaxHistoryLedger) err := row.Scan(&latestLedger) if err != nil { log.Warn(ctx, "Failed to check latest ledger", err) break } if latestLedger > lastSeenLedger { log.Debugf(ctx, "saw new ledger: %d, prev: %d", latestLedger, lastSeenLedger) select { case result <- struct{}{}: lastSeenLedger = latestLedger default: log.Debug(ctx, "ledger pump channel is blocked. waiting...") } } else if latestLedger < lastSeenLedger { log.Warn(ctx, "latest ledger went backwards! reseting ledger pump") lastSeenLedger = 0 } case <-ctx.Done(): log.Info(ctx, "canceling ledger pump") close(result) return } } }() return result }
// NewLedgerClosePump starts a background proc that continually watches the // history database provided. The watch is stopped after the provided context // is cancelled. // // Every second, the proc spawned by calling this func will check to see // if a new ledger has been imported (by ruby-horizon as of 2015-04-30, but // should eventually end up being in this project). If a new ledger is seen // the the channel returned by this function emits func NewLedgerClosePump(ctx context.Context, q *history.Q) <-chan struct{} { result := make(chan struct{}) go func() { var lastSeenLedger int32 for { select { case <-time.After(1 * time.Second): var latestLedger int32 err := q.LatestLedger(&latestLedger) if err != nil { log.Warn("Failed to check latest ledger", err) continue } if latestLedger > lastSeenLedger { log.Debugf("saw new ledger: %d, prev: %d", latestLedger, lastSeenLedger) select { case result <- struct{}{}: lastSeenLedger = latestLedger default: log.Debug("ledger pump channel is blocked. waiting...") } } else if latestLedger < lastSeenLedger { log.Warn("latest ledger went backwards! reseting ledger pump") lastSeenLedger = 0 } case <-ctx.Done(): log.Info("canceling ledger pump") close(result) return } } }() return result }
func (i *System) run() { for _ = range i.tick.C { log.Debug("ticking ingester") i.runOnce() } }