Skip to content

Commit eab9d75

Browse files
committed
fix: comment punctuation and shutdown of ticker
1 parent 7233c25 commit eab9d75

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

aperture.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func run() error {
151151

152152
errChan := make(chan error)
153153
a := NewAperture(cfg)
154-
if err := a.Start(errChan); err != nil {
154+
if err := a.Start(errChan, interceptor.ShutdownChannel()); err != nil {
155155
return fmt.Errorf("unable to start aperture: %v", err)
156156
}
157157

@@ -192,9 +192,9 @@ func NewAperture(cfg *Config) *Aperture {
192192
}
193193

194194
// Start sets up the proxy server and starts it.
195-
func (a *Aperture) Start(errChan chan error) error {
195+
func (a *Aperture) Start(errChan chan error, shutdown <-chan struct{}) error {
196196
// Start the prometheus exporter.
197-
err := StartPrometheusExporter(a.cfg.Prometheus)
197+
err := StartPrometheusExporter(a.cfg.Prometheus, shutdown)
198198
if err != nil {
199199
return fmt.Errorf("unable to start the prometheus "+
200200
"exporter: %v", err)

hashmail_server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,9 @@ func (sa *streamActivity) Record(baseID string) {
809809
// ClassifyAndReset categorizes each tracked stream based on its recent read
810810
// rate and returns aggregate counts of active, standby, and in-use sessions.
811811
// A stream is classified as:
812-
// - In-use: if read rate ≥ 0.5 reads/sec
813-
// - Standby: if 0 < read rate < 0.5 reads/sec
814-
// - Active: if read rate > 0 (includes standby and in-use)
812+
// - In-use: if read rate ≥ 0.5 reads/sec.
813+
// - Standby: if 0 < read rate < 0.5 reads/sec.
814+
// - Active: if read rate > 0 (includes standby and in-use).
815815
func (sa *streamActivity) ClassifyAndReset() (active, standby, inuse int) {
816816
sa.Lock()
817817
defer sa.Unlock()

hashmail_server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ func setupAperture(t *testing.T) {
186186
}
187187
aperture := NewAperture(apertureCfg)
188188
errChan := make(chan error)
189-
require.NoError(t, aperture.Start(errChan))
189+
shutdown := make(chan struct{})
190+
require.NoError(t, aperture.Start(errChan, shutdown))
190191

191192
// Any error while starting?
192193
select {

prometheus.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@ var (
1616
Name: "mailbox_count",
1717
})
1818

19-
// activeSessions tracks the active session count for mailbox
19+
// activeSessions tracks the active session count for mailbox.
2020
activeSessions = prometheus.NewGauge(prometheus.GaugeOpts{
2121
Namespace: "hashmail",
2222
Name: "mailbox_active_sessions",
2323
Help: "Number of active sessions",
2424
})
2525

26-
// standbySessions tracks the standby session count for mailbox
26+
// standbySessions tracks the standby session count for mailbox.
2727
standbySessions = prometheus.NewGauge(prometheus.GaugeOpts{
2828
Namespace: "hashmail",
2929
Name: "mailbox_standby_sessions",
3030
Help: "Number of standby sessions",
3131
})
3232

33-
// inUseSessions tracks the in-use session count for mailbox
33+
// inUseSessions tracks the in-use session count for mailbox.
3434
inUseSessions = prometheus.NewGauge(prometheus.GaugeOpts{
3535
Namespace: "hashmail",
3636
Name: "mailbox_inuse_sessions",
3737
Help: "Number of in-use sessions",
3838
})
3939
)
4040

41-
// streamActivityTracker handles the calculation of session statistics
41+
// streamActivityTracker handles the calculation of session statistics.
4242
var streamActivityTracker = newStreamActivity()
4343

4444
// PrometheusConfig is the set of configuration data that specifies if
@@ -56,7 +56,9 @@ type PrometheusConfig struct {
5656
// StartPrometheusExporter registers all relevant metrics with the Prometheus
5757
// library, then launches the HTTP server that Prometheus will hit to scrape
5858
// our metrics.
59-
func StartPrometheusExporter(cfg *PrometheusConfig) error {
59+
func StartPrometheusExporter(cfg *PrometheusConfig,
60+
shutdown <-chan struct{}) error {
61+
6062
// If we're not active, then there's nothing more to do.
6163
if !cfg.Enabled {
6264
return nil
@@ -68,16 +70,23 @@ func StartPrometheusExporter(cfg *PrometheusConfig) error {
6870
prometheus.MustRegister(standbySessions)
6971
prometheus.MustRegister(inUseSessions)
7072

71-
// Periodically update session classification metrics from internal tracker
73+
// Periodically update session classification metrics from internal tracker.
7274
go func() {
7375
ticker := time.NewTicker(10 * time.Second)
7476
defer ticker.Stop()
7577

76-
for range ticker.C {
77-
active, standby, inuse := streamActivityTracker.ClassifyAndReset()
78-
activeSessions.Set(float64(active))
79-
standbySessions.Set(float64(standby))
80-
inUseSessions.Set(float64(inuse))
78+
for {
79+
select {
80+
case <-ticker.C:
81+
active, standby, inuse :=
82+
streamActivityTracker.ClassifyAndReset()
83+
activeSessions.Set(float64(active))
84+
standbySessions.Set(float64(standby))
85+
inUseSessions.Set(float64(inuse))
86+
case <-shutdown:
87+
log.Infof("Shutting down Prometheus session metrics updater")
88+
return
89+
}
8190
}
8291
}()
8392

0 commit comments

Comments
 (0)