fix: 仅向已握手连接推送并拆分调度循环
This commit is contained in:
+14
-19
@@ -299,7 +299,7 @@ func runServe(ctx context.Context, cfg config.Config) error {
|
||||
|
||||
loopCtx, loopCancel := context.WithCancel(ctx)
|
||||
defer loopCancel()
|
||||
go messageLoops(loopCtx, msgApp, memConns, db, hashPool, metricsReg)
|
||||
go messageLoops(loopCtx, msgApp, db, hashPool, metricsReg)
|
||||
|
||||
<-ctx.Done()
|
||||
loopCancel()
|
||||
@@ -316,30 +316,25 @@ func runServe(ctx context.Context, cfg config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func messageLoops(ctx context.Context, msgApp *message.App, conns *message.MemoryConns, db *store.DB, hashPool auth.HashPool, met *metrics.Registry) {
|
||||
t := time.NewTicker(time.Second)
|
||||
func messageLoops(ctx context.Context, msgApp *message.App, db *store.DB, hashPool auth.HashPool, met *metrics.Registry) {
|
||||
msgApp.StartLoops(ctx)
|
||||
t := time.NewTicker(15 * time.Second)
|
||||
defer t.Stop()
|
||||
sample := func() {
|
||||
opCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := metrics.SampleStoreGauges(opCtx, met, db.Read); err != nil {
|
||||
slog.Debug("sample store gauges", "err", err)
|
||||
}
|
||||
metrics.SampleQueues(met, db.Queue.Len(), hashPool.QueueLen())
|
||||
}
|
||||
sample()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
nowMs := time.Now().UnixMilli()
|
||||
if _, err := msgApp.DispatchDue(ctx, nowMs, 100); err != nil {
|
||||
slog.Error("dispatch due", "err", err)
|
||||
}
|
||||
for ep, live := range conns.Snapshot() {
|
||||
if err := msgApp.PushPending(ctx, ep, live.ConnID); err != nil {
|
||||
slog.Debug("push pending", "endpoint", ep, "err", err)
|
||||
}
|
||||
}
|
||||
if err := msgApp.CleanupOnce(ctx, nowMs); err != nil {
|
||||
slog.Error("cleanup once", "err", err)
|
||||
}
|
||||
if err := metrics.SampleStoreGauges(ctx, met, db.Read); err != nil {
|
||||
slog.Debug("sample store gauges", "err", err)
|
||||
}
|
||||
metrics.SampleQueues(met, db.Queue.Len(), hashPool.QueueLen())
|
||||
sample()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user