Only init branch activity services for sql-server

This commit is contained in:
Neil Macneale IV
2025-10-21 10:18:24 -07:00
parent c05a869f83
commit 7869a2381c
2 changed files with 27 additions and 8 deletions

View File

@@ -763,6 +763,14 @@ func ConfigureServices(
}
controller.Register(RunClusterRemoteSrv)
branchActivityInit := &svcs.AnonService{
InitF: func(ctx context.Context) error {
doltdb.BranchActivityInit(ctx)
return nil
},
}
controller.Register(branchActivityInit)
// We still have some startup to do from this point, and we do not run
// the SQL server until we are fully booted. We also want to stop the
// SQL server as the first thing we stop. However, if startup fails

View File

@@ -51,21 +51,23 @@ var (
branchReadTimes map[string]time.Time
branchWriteTimes map[string]time.Time
systemStartTime time.Time
activityChan chan branchActivityEvent
activityChan *chan branchActivityEvent
)
func init() {
func BranchActivityInit(ctx context.Context) {
systemStartTime = time.Now()
branchReadTimes = make(map[string]time.Time)
branchWriteTimes = make(map[string]time.Time)
activityChan = make(chan branchActivityEvent, 64) // lifetime in buffer will be very short.
ac := make(chan branchActivityEvent, 64) // lifetime in buffer will be very short.
activityChan = &ac
// Start background goroutine to process events
go func() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for {
select {
case event := <-activityChan:
case event := <-(*activityChan):
branchActivityMutex.Lock()
if event.eventType == READ {
if existing, exists := branchReadTimes[event.branch]; !exists || event.timestamp.After(existing) {
@@ -86,12 +88,15 @@ func init() {
// BranchActivityReadEvent records when a branch is read/accessed
func BranchActivityReadEvent(ctx context.Context, branch string) {
if activityChan == nil {
return
}
if ctx.Value(StatsSessionContextKey) != nil {
return
}
select {
case activityChan <- branchActivityEvent{
case (*activityChan) <- branchActivityEvent{
branch: branch,
timestamp: time.Now(),
eventType: READ,
@@ -103,12 +108,15 @@ func BranchActivityReadEvent(ctx context.Context, branch string) {
// BranchActivityWriteEvent records when a branch is written/updated
func BranchActivityWriteEvent(ctx context.Context, branch string) {
if activityChan == nil {
return
}
if ctx.Value(StatsSessionContextKey) != nil {
return
}
select {
case activityChan <- branchActivityEvent{
case (*activityChan) <- branchActivityEvent{
branch: branch,
timestamp: time.Now(),
eventType: WRITE,
@@ -120,10 +128,13 @@ func BranchActivityWriteEvent(ctx context.Context, branch string) {
// GetBranchActivity returns activity data for all branches (tracked and untracked)
func GetBranchActivity(ctx context.Context, ddb *DoltDB) ([]BranchActivityData, error) {
if activityChan == nil {
return nil, nil
}
branchActivityMutex.RLock()
defer branchActivityMutex.RUnlock()
// Get all branches from the database
branchRefs, err := ddb.GetBranches(ctx)
if err != nil {
return nil, err