Skip to content

Commit d980924

Browse files
authored
Sample active calls when waiting for shutdown. (#352)
1 parent 485002a commit d980924

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

pkg/service/service.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121
"net/http"
2222
"net/http/pprof"
23+
"slices"
2324
"sync/atomic"
2425
"time"
2526

@@ -40,7 +41,7 @@ import (
4041
)
4142

4243
type sipServiceStopFunc func()
43-
type sipServiceActiveCallsFunc func() int
44+
type sipServiceActiveCallsFunc func() sip.ActiveCalls
4445

4546
type Service struct {
4647
conf *config.Config
@@ -152,14 +153,18 @@ func (s *Service) Run() error {
152153
if !s.killed.Load() {
153154
shutdownTicker := time.NewTicker(5 * time.Second)
154155
defer shutdownTicker.Stop()
155-
var activeCalls int
156156

157157
for !s.killed.Load() {
158-
activeCalls = s.sipServiceActiveCalls()
159-
if activeCalls == 0 {
158+
st := s.sipServiceActiveCalls()
159+
if st.Total() == 0 {
160160
break
161161
}
162-
s.log.Infow("waiting for calls to finish", "calls", activeCalls)
162+
slices.Sort(st.SampleIDs)
163+
s.log.Infow("waiting for calls to finish",
164+
"inbound", st.Inbound,
165+
"outbound", st.Outbound,
166+
"sample", st.SampleIDs,
167+
)
163168
<-shutdownTicker.C
164169
}
165170
}

pkg/sip/service.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,57 @@ func NewService(region string, conf *config.Config, mon *stats.Monitor, log logg
100100
return s, nil
101101
}
102102

103-
func (s *Service) ActiveCalls() int {
103+
type ActiveCalls struct {
104+
Inbound int
105+
Outbound int
106+
SampleIDs []string
107+
}
108+
109+
func (st ActiveCalls) Total() int {
110+
return st.Outbound + st.Inbound
111+
}
112+
113+
func sampleMap[K comparable, V any](limit int, m map[K]V, sample func(v V) string) ([]string, int) {
114+
total := len(m)
115+
var out []string
116+
for _, v := range m {
117+
if s := sample(v); s != "" {
118+
out = append(out, s)
119+
}
120+
limit--
121+
if limit <= 0 {
122+
break
123+
}
124+
}
125+
return out, total
126+
}
127+
128+
func (s *Service) ActiveCalls() ActiveCalls {
129+
st := ActiveCalls{}
130+
104131
s.cli.cmu.Lock()
105-
activeClientCalls := len(s.cli.activeCalls)
132+
samples, total := sampleMap(5, s.cli.activeCalls, func(v *outboundCall) string {
133+
if v == nil || v.cc == nil {
134+
return "<nil>"
135+
}
136+
return v.cc.callID
137+
})
138+
st.Outbound = total
139+
st.SampleIDs = append(st.SampleIDs, samples...)
106140
s.cli.cmu.Unlock()
107141

108142
s.srv.cmu.Lock()
109-
activeServerCalls := len(s.srv.activeCalls)
143+
samples, total = sampleMap(5, s.srv.activeCalls, func(v *inboundCall) string {
144+
if v == nil || v.cc == nil {
145+
return "<nil>"
146+
}
147+
return v.cc.callID
148+
})
149+
st.Inbound = total
150+
st.SampleIDs = append(st.SampleIDs, samples...)
110151
s.srv.cmu.Unlock()
111152

112-
return activeClientCalls + activeServerCalls
153+
return st
113154
}
114155

115156
func (s *Service) Stop() {

0 commit comments

Comments
 (0)