Skip to content

Commit 636f67a

Browse files
authored
Return detailed status for the service health. (#355)
1 parent 95d1324 commit 636f67a

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

pkg/service/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func (s *Service) GetMediaProcessor(_ []livekit.SIPFeature) media.PCM16Processor
187187
return nil
188188
}
189189

190-
func (s *Service) CanAccept() bool {
191-
return s.mon.CanAccept()
190+
func (s *Service) Health() stats.HealthStatus {
191+
return s.mon.Health()
192192
}
193193

194194
func (s *Service) RegisterCreateSIPParticipantTopic() error {

pkg/sip/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *Client) CreateSIPParticipant(ctx context.Context, req *rpc.InternalCrea
129129
}
130130

131131
func (c *Client) createSIPParticipant(ctx context.Context, req *rpc.InternalCreateSIPParticipantRequest) (resp *rpc.InternalCreateSIPParticipantResponse, retErr error) {
132-
if !c.mon.CanAccept() {
132+
if c.mon.Health() != stats.HealthOK {
133133
return nil, siperrors.ErrUnavailable
134134
}
135135
if req.CallTo == "" {

pkg/stats/healthstatus_string.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/stats/monitor.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ func (m *Monitor) Start(conf *config.Config) error {
216216
Help: "Whether node can accept new requests",
217217
ConstLabels: prometheus.Labels{"node_id": conf.NodeID},
218218
}, func() float64 {
219-
c := m.CanAccept()
220-
if c {
219+
if m.Health() == HealthOK {
221220
return 1
222221
}
223222
return 0
@@ -246,14 +245,29 @@ func (m *Monitor) Stop() {
246245
m.metrics = nil
247246
}
248247

249-
func (m *Monitor) CanAccept() bool {
250-
if !m.started.IsBroken() ||
251-
m.shutdown.IsBroken() ||
252-
m.cpu.GetCPUIdle() < m.cpu.NumCPU()*(1-m.maxUtilization) {
253-
return false
254-
}
248+
//go:generate stringer -type HealthStatus -trimprefix Health
249+
250+
type HealthStatus int
251+
252+
const (
253+
HealthOK HealthStatus = iota
254+
HealthNotStarted
255+
HealthStopped
256+
HealthUnderLoad
257+
HealthDisabled
258+
)
255259

256-
return true
260+
func (m *Monitor) Health() HealthStatus {
261+
if !m.started.IsBroken() {
262+
return HealthNotStarted
263+
}
264+
if m.shutdown.IsBroken() {
265+
return HealthStopped
266+
}
267+
if m.cpu.GetCPUIdle() < m.cpu.NumCPU()*(1-m.maxUtilization) {
268+
return HealthUnderLoad
269+
}
270+
return HealthOK
257271
}
258272

259273
func (m *Monitor) IdleCPU() float64 {

0 commit comments

Comments
 (0)