@@ -10,32 +10,40 @@ type Client interface {
10
10
Write (data []byte ) error
11
11
}
12
12
13
+ type pingConfig struct {
14
+ interval time.Duration
15
+ dataFunc func () []byte
16
+ }
17
+
13
18
type PingManager struct {
14
- clients map [int64 ]map [Client ]time. Duration
19
+ clients map [int64 ]map [Client ]pingConfig
15
20
clientPingMap map [Client ]int64
16
21
mutex sync.Mutex
17
22
}
18
23
19
24
// NewPingManager creates a new PingManager instance.
20
25
func NewPingManager () * PingManager {
21
26
return & PingManager {
22
- clients : make (map [int64 ]map [Client ]time. Duration ),
27
+ clients : make (map [int64 ]map [Client ]pingConfig ),
23
28
clientPingMap : make (map [Client ]int64 ),
24
29
}
25
30
}
26
31
27
32
// Add registers a client to be pinged.
28
- func (m * PingManager ) Add (client Client , pingInterval time.Duration ) {
33
+ func (m * PingManager ) Add (client Client , pingInterval time.Duration , dataFunc func () [] byte ) {
29
34
// Calculate the next ping time in seconds
30
35
timeToPing := time .Now ().Add (pingInterval ).Unix ()
31
36
32
37
m .mutex .Lock ()
33
38
defer m .mutex .Unlock ()
34
39
if m .clients [timeToPing ] == nil {
35
- m .clients [timeToPing ] = make (map [Client ]time. Duration )
40
+ m .clients [timeToPing ] = make (map [Client ]pingConfig )
36
41
}
37
42
38
- m.clients [timeToPing ][client ] = pingInterval
43
+ m.clients [timeToPing ][client ] = pingConfig {
44
+ interval : pingInterval ,
45
+ dataFunc : dataFunc ,
46
+ }
39
47
m .clientPingMap [client ] = timeToPing
40
48
}
41
49
@@ -61,17 +69,17 @@ func (m *PingManager) Reset(client Client) {
61
69
62
70
nextPingTime , ok := m .clientPingMap [client ]
63
71
if ok {
64
- pingInterval := m.clients [nextPingTime ][client ]
72
+ pingInfo := m.clients [nextPingTime ][client ]
65
73
delete (m .clients [nextPingTime ], client )
66
74
if len (m .clients [nextPingTime ]) == 0 {
67
75
delete (m .clients , nextPingTime )
68
76
}
69
77
70
- newPingTime := time .Now ().Add (pingInterval ).Unix ()
78
+ newPingTime := time .Now ().Add (pingInfo . interval ).Unix ()
71
79
if m .clients [newPingTime ] == nil {
72
- m .clients [newPingTime ] = make (map [Client ]time. Duration )
80
+ m .clients [newPingTime ] = make (map [Client ]pingConfig )
73
81
}
74
- m.clients [newPingTime ][client ] = pingInterval
82
+ m.clients [newPingTime ][client ] = pingInfo
75
83
m .clientPingMap [client ] = newPingTime
76
84
}
77
85
}
@@ -85,20 +93,20 @@ func (m *PingManager) Start() {
85
93
m .mutex .Lock ()
86
94
clients , ok := m .clients [nowSeconds ]
87
95
if ok {
88
- for client , interval := range clients {
89
- err := client .Write ([] byte ( "ping" ))
96
+ for client , pingCfg := range clients {
97
+ err := client .Write (pingCfg . dataFunc ( ))
90
98
if err != nil {
91
99
log .Printf ("error pinging client: %v" , err )
92
100
m .Remove (client )
93
101
continue
94
102
}
95
103
96
104
// Reschedule the next ping for the client
97
- timeToPing := time .Now ().Add (interval ).Unix ()
105
+ timeToPing := time .Now ().Add (pingCfg . interval ).Unix ()
98
106
if m .clients [timeToPing ] == nil {
99
- m .clients [timeToPing ] = make (map [Client ]time. Duration )
107
+ m .clients [timeToPing ] = make (map [Client ]pingConfig )
100
108
}
101
- m.clients [timeToPing ][client ] = interval
109
+ m.clients [timeToPing ][client ] = pingCfg
102
110
m .clientPingMap [client ] = timeToPing
103
111
104
112
}
0 commit comments