github.com/nilium/gitlab-runner@v12.5.0+incompatible/commands/health_helper.go (about)

     1  package commands
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"gitlab.com/gitlab-org/gitlab-runner/common"
     9  )
    10  
    11  type healthData struct {
    12  	failures  int
    13  	lastCheck time.Time
    14  }
    15  
    16  type healthHelper struct {
    17  	healthy     map[string]*healthData
    18  	healthyLock sync.Mutex
    19  }
    20  
    21  func (mr *healthHelper) getHealth(id string) *healthData {
    22  	if mr.healthy == nil {
    23  		mr.healthy = map[string]*healthData{}
    24  	}
    25  	health := mr.healthy[id]
    26  	if health == nil {
    27  		health = &healthData{
    28  			lastCheck: time.Now(),
    29  		}
    30  		mr.healthy[id] = health
    31  	}
    32  	return health
    33  }
    34  
    35  func (mr *healthHelper) isHealthy(id string) bool {
    36  	mr.healthyLock.Lock()
    37  	defer mr.healthyLock.Unlock()
    38  
    39  	health := mr.getHealth(id)
    40  	if health.failures < common.HealthyChecks {
    41  		return true
    42  	}
    43  
    44  	if time.Since(health.lastCheck) > common.HealthCheckInterval*time.Second {
    45  		logrus.Errorln("Runner", id, "is not healthy, but will be checked!")
    46  		health.failures = 0
    47  		health.lastCheck = time.Now()
    48  		return true
    49  	}
    50  
    51  	return false
    52  }
    53  
    54  func (mr *healthHelper) makeHealthy(id string, healthy bool) {
    55  	mr.healthyLock.Lock()
    56  	defer mr.healthyLock.Unlock()
    57  
    58  	health := mr.getHealth(id)
    59  	if healthy {
    60  		health.failures = 0
    61  		health.lastCheck = time.Now()
    62  	} else {
    63  		health.failures++
    64  		if health.failures >= common.HealthyChecks {
    65  			logrus.Errorln("Runner", id, "is not healthy and will be disabled!")
    66  		}
    67  	}
    68  }