Skip to content

Commit c923d79

Browse files
chore(broadcast): cleanup of unused code
1 parent 656a7fc commit c923d79

File tree

6 files changed

+1
-113
lines changed

6 files changed

+1
-113
lines changed

broadcast.go

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ type broadcastServer struct {
3636
logger *slog.Logger
3737
}
3838

39-
func (srv *Server) GetStats() broadcast.Metrics {
40-
return srv.broadcastSrv.manager.GetStats()
41-
}
42-
4339
func newBroadcastServer(serverOpts *serverOptions) *broadcastServer {
4440
h := fnv.New32a()
4541
_, _ = h.Write([]byte(serverOpts.listenAddr))

broadcast/manager.go

-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type Manager interface {
2020
AddHandler(method string, handler any)
2121
Close() error
2222
ResetState()
23-
GetStats() Metrics
2423
}
2524

2625
type manager struct {
@@ -163,20 +162,3 @@ func (mgr *manager) Close() error {
163162
func (mgr *manager) ResetState() {
164163
mgr.state.reset()
165164
}
166-
167-
func (mgr *manager) GetStats() Metrics {
168-
m := mgr.state.getStats()
169-
return Metrics{
170-
TotalNum: uint64(m.totalMsgs),
171-
Dropped: m.droppedMsgs,
172-
FinishedReqs: struct {
173-
Total uint64
174-
Succesful uint64
175-
Failed uint64
176-
}{
177-
Total: m.numReqs,
178-
Succesful: m.finishedReqs,
179-
Failed: m.numReqs - m.finishedReqs,
180-
},
181-
}
182-
}

broadcast/metrics.go

-11
This file was deleted.

broadcast/shard.go

-55
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,10 @@ import (
1111

1212
const NumShards = 16
1313

14-
type shardMetrics struct {
15-
totalMsgs uint64
16-
numMsgs uint64
17-
droppedMsgs uint64
18-
numBroadcastMsgs uint64
19-
droppedBroadcastMsgs uint64
20-
numReqs uint64
21-
finishedReqs uint64
22-
lifetimes [][]time.Time
23-
avgLifetime time.Duration
24-
minLifetime time.Duration
25-
maxLifetime time.Duration
26-
}
27-
2814
type shard struct {
2915
mut sync.RWMutex
3016
id int
3117
parentCtx context.Context
32-
metrics shardMetrics
3318
procs map[uint64]*BroadcastProcessor
3419
reqTTL time.Duration
3520
router Router
@@ -62,7 +47,6 @@ func createShards(ctx context.Context, shardBuffer, sendBuffer int, router Route
6247
}
6348

6449
func (s *shard) handleMsg(msg *Content) shardResponse {
65-
//s.metrics.numMsgs++
6650
// Optimization: first check with a read lock if the processor already exists
6751
if p, ok := s.getProcessor(msg.BroadcastID); ok {
6852
return s.process(p, msg)
@@ -120,7 +104,6 @@ func (s *shard) process(p *BroadcastProcessor, msg *Content) shardResponse {
120104
// must check if the req is done to prevent deadlock
121105
select {
122106
case <-p.ctx.Done():
123-
//s.metrics.droppedMsgs++
124107
return shardResponse{
125108
err: AlreadyProcessedErr{},
126109
}
@@ -138,11 +121,9 @@ func (s *shard) process(p *BroadcastProcessor, msg *Content) shardResponse {
138121
}
139122

140123
func (s *shard) handleBMsg(msg *Msg) {
141-
//s.metrics.numBroadcastMsgs++
142124
if req, ok := s.getProcessor(msg.BroadcastID); ok {
143125
select {
144126
case <-req.ctx.Done():
145-
//s.metrics.droppedBroadcastMsgs++
146127
case req.broadcastChan <- msg:
147128
}
148129
}
@@ -178,7 +159,6 @@ func (s *shard) addProcessor(msg *Content) (*BroadcastProcessor, bool) {
178159
// check size of s.reqs. If too big, then perform necessary cleanup.
179160
// should only affect the current shard and not the others.
180161
ctx, cancel := context.WithTimeout(s.parentCtx, s.reqTTL)
181-
//req := &BroadcastRequest{
182162
var logger *slog.Logger
183163
if s.logger != nil {
184164
logger = s.logger.With(logging.BroadcastID(msg.BroadcastID))
@@ -229,38 +209,3 @@ func (s *shard) gc(nextGC time.Duration) {
229209
}
230210
s.procs = newReqs
231211
}
232-
233-
func (s *shard) getStats() shardMetrics {
234-
s.mut.RLock()
235-
defer s.mut.RUnlock()
236-
s.metrics.numReqs = uint64(len(s.procs))
237-
s.metrics.lifetimes = make([][]time.Time, len(s.procs))
238-
s.metrics.totalMsgs = s.metrics.numBroadcastMsgs + s.metrics.numMsgs
239-
minLifetime := 100 * time.Hour
240-
maxLifetime := time.Duration(0)
241-
totalLifetime := time.Duration(0)
242-
i := 0
243-
for _, req := range s.procs {
244-
select {
245-
case <-req.ctx.Done():
246-
s.metrics.finishedReqs++
247-
default:
248-
}
249-
s.metrics.lifetimes[i] = []time.Time{req.started, req.ended}
250-
lifetime := req.ended.Sub(req.started)
251-
if lifetime > 0 {
252-
if lifetime < minLifetime {
253-
minLifetime = lifetime
254-
}
255-
if lifetime > maxLifetime {
256-
maxLifetime = lifetime
257-
}
258-
totalLifetime += lifetime
259-
}
260-
i++
261-
}
262-
s.metrics.minLifetime = minLifetime
263-
s.metrics.maxLifetime = maxLifetime
264-
s.metrics.avgLifetime = totalLifetime
265-
return s.metrics
266-
}

broadcast/state.go

-24
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,6 @@ func (s *BroadcastState) getShard(i uint16) *shard {
9696
return s.shards[i]
9797
}
9898

99-
func (state *BroadcastState) getStats() shardMetrics {
100-
m := shardMetrics{
101-
lifetimes: make([][]time.Time, 0),
102-
}
103-
for _, shard := range state.shards {
104-
metric := shard.getStats()
105-
m.totalMsgs += metric.totalMsgs
106-
m.numMsgs += metric.numMsgs
107-
m.droppedMsgs += metric.droppedMsgs
108-
m.numBroadcastMsgs += metric.numBroadcastMsgs
109-
m.droppedBroadcastMsgs += metric.droppedBroadcastMsgs
110-
m.numReqs += metric.numReqs
111-
m.finishedReqs += metric.finishedReqs
112-
m.lifetimes = append(m.lifetimes, metric.lifetimes...)
113-
m.avgLifetime += metric.avgLifetime
114-
m.maxLifetime += metric.maxLifetime
115-
m.minLifetime += metric.minLifetime
116-
}
117-
if m.numReqs > 0 {
118-
m.avgLifetime /= time.Duration(m.numReqs)
119-
}
120-
return m
121-
}
122-
12399
type shardResponse struct {
124100
err error
125101
reqCtx context.Context

tests/broadcast/broadcast_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestSimpleBroadcastTo(t *testing.T) {
147147

148148
func TestSimpleBroadcastCancel(t *testing.T) {
149149
numSrvs := 3
150-
numReqs := 1000
150+
numReqs := 10
151151
_, srvAddrs, srvCleanup, err := createSrvs(numSrvs)
152152
if err != nil {
153153
t.Error(err)

0 commit comments

Comments
 (0)