func startPStoreLoops( stats *datastructs.ApplicationStatuses, consumerBuilders []*pstore.ConsumerWithMetricsBuilder, logger *log.Logger, maybeNilCoordBuilder coordinatorBuilderType) []*totalCountType { result := make([]*totalCountType, len(consumerBuilders)) for i := range result { pstoreHandler := newPStoreHandler( stats.ApplicationList(), consumerBuilders[i], maybeNilCoordBuilder) result[i] = pstoreHandler.TotalCount() var attributes pstore.ConsumerAttributes pstoreHandler.Attributes(&attributes) refreshRate := *fPStoreUpdateFrequency if attributes.RollUpSpan > 0 { refreshRate = attributes.RollUpSpan } if err := pstoreHandler.RegisterMetrics(); err != nil { log.Fatal(err) } go func(handler *pstoreHandlerType, refreshRate time.Duration) { // persistent storage writing goroutine. Write every 30s by default. // Notice that this single goroutine handles all the persistent // storage writing as multiple goroutines must not access the // pstoreHandler instance. accessing pstoreHandler metrics is the // one exception to this rule. for { metricStore := stats.Store() writeTime := time.Now() handler.StartVisit() metricStore.VisitAllEndpoints(handler) handler.EndVisit(metricStore) writeDuration := time.Now().Sub(writeTime) if writeDuration < refreshRate { time.Sleep(refreshRate - writeDuration) } } }(pstoreHandler, refreshRate) } return result }