Skip to content

Commit 2d2ba19

Browse files
committed
fix: maybe fix performance issues
1 parent 3ad0934 commit 2d2ba19

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

task/pingJob.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/influxdata/influxdb-client-go/v2/api/write"
16+
mctypes "github.com/iverly/go-mcping/api/types"
1617
"github.com/iverly/go-mcping/mcping"
1718
"go.mongodb.org/mongo-driver/v2/bson"
1819
"go.mongodb.org/mongo-driver/v2/mongo"
@@ -24,6 +25,13 @@ type PingResult struct {
2425
playerCount int
2526
}
2627

28+
// serverPinger abstracts mcping's unexported pinger type so it can be reused
29+
// across calls without being re-created (and re-allocating its DNS resolver)
30+
// on every tick.
31+
type serverPinger interface {
32+
PingWithTimeout(host string, port uint16, timeout time.Duration) (*mctypes.PingResponse, error)
33+
}
34+
2735
const (
2836
influxQueueSize = 500
2937
bulkFlushSize = 300
@@ -86,6 +94,8 @@ func (j *PingJob) runServerLoop(ctx context.Context, server data.PingableServer)
8694
notifyChan := websocket.GlobalHub.RegisterServerNotify(server.IP)
8795
defer websocket.GlobalHub.UnregisterServerNotify(server.IP)
8896

97+
pinger := mcping.NewPinger()
98+
8999
var customInterval time.Duration
90100
if server.Interval > 0 {
91101
customInterval = time.Duration(server.Interval) * time.Second
@@ -105,14 +115,14 @@ func (j *PingJob) runServerLoop(ctx context.Context, server data.PingableServer)
105115
ticker := time.NewTicker(getCurrentInterval())
106116
defer ticker.Stop()
107117

108-
j.pingServer(server)
118+
j.pingServer(server, pinger)
109119

110120
lastInterval := getCurrentInterval()
111121

112122
for {
113123
select {
114124
case <-ticker.C:
115-
j.pingServer(server)
125+
j.pingServer(server, pinger)
116126

117127
newInterval := getCurrentInterval()
118128
if newInterval != lastInterval {
@@ -130,7 +140,7 @@ func (j *PingJob) runServerLoop(ctx context.Context, server data.PingableServer)
130140
lastInterval = newInterval
131141

132142
if newInterval < lastInterval {
133-
j.pingServer(server)
143+
j.pingServer(server, pinger)
134144
}
135145
}
136146

@@ -266,9 +276,7 @@ func StartDBWriter(ctx context.Context) {
266276
}()
267277
}
268278

269-
func (j *PingJob) pingServer(server data.PingableServer) {
270-
pinger := mcping.NewPinger()
271-
279+
func (j *PingJob) pingServer(server data.PingableServer, pinger serverPinger) {
272280
host, port := parseAddress(server.IP)
273281
resp, err := pinger.PingWithTimeout(
274282
host,

websocket/websocket.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ func (h *Hub) writeJSONLocked(conn *websocket.Conn, v interface{}) error {
155155

156156
func (h *Hub) SendToServer(ip string, message interface{}) {
157157
h.mu.RLock()
158-
conns := h.subscriptions[ip]
158+
subs := h.subscriptions[ip]
159+
conns := make([]*websocket.Conn, 0, len(subs))
160+
for conn := range subs {
161+
conns = append(conns, conn)
162+
}
159163
h.mu.RUnlock()
160164

161-
for conn := range conns {
165+
for _, conn := range conns {
162166
if err := h.writeJSONLocked(conn, message); err != nil {
163167
h.Unregister(conn)
164168
_ = conn.Close()

0 commit comments

Comments
 (0)