@@ -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+
2735const (
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 ,
0 commit comments