Skip to content

Commit 222b01a

Browse files
Add function for ping data
1 parent e323d22 commit 222b01a

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

workerpool.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,40 @@ type Client interface {
1010
Write(data []byte) error
1111
}
1212

13+
type pingConfig struct {
14+
interval time.Duration
15+
dataFunc func() []byte
16+
}
17+
1318
type PingManager struct {
14-
clients map[int64]map[Client]time.Duration
19+
clients map[int64]map[Client]pingConfig
1520
clientPingMap map[Client]int64
1621
mutex sync.Mutex
1722
}
1823

1924
// NewPingManager creates a new PingManager instance.
2025
func NewPingManager() *PingManager {
2126
return &PingManager{
22-
clients: make(map[int64]map[Client]time.Duration),
27+
clients: make(map[int64]map[Client]pingConfig),
2328
clientPingMap: make(map[Client]int64),
2429
}
2530
}
2631

2732
// 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) {
2934
// Calculate the next ping time in seconds
3035
timeToPing := time.Now().Add(pingInterval).Unix()
3136

3237
m.mutex.Lock()
3338
defer m.mutex.Unlock()
3439
if m.clients[timeToPing] == nil {
35-
m.clients[timeToPing] = make(map[Client]time.Duration)
40+
m.clients[timeToPing] = make(map[Client]pingConfig)
3641
}
3742

38-
m.clients[timeToPing][client] = pingInterval
43+
m.clients[timeToPing][client] = pingConfig{
44+
interval: pingInterval,
45+
dataFunc: dataFunc,
46+
}
3947
m.clientPingMap[client] = timeToPing
4048
}
4149

@@ -61,17 +69,17 @@ func (m *PingManager) Reset(client Client) {
6169

6270
nextPingTime, ok := m.clientPingMap[client]
6371
if ok {
64-
pingInterval := m.clients[nextPingTime][client]
72+
pingInfo := m.clients[nextPingTime][client]
6573
delete(m.clients[nextPingTime], client)
6674
if len(m.clients[nextPingTime]) == 0 {
6775
delete(m.clients, nextPingTime)
6876
}
6977

70-
newPingTime := time.Now().Add(pingInterval).Unix()
78+
newPingTime := time.Now().Add(pingInfo.interval).Unix()
7179
if m.clients[newPingTime] == nil {
72-
m.clients[newPingTime] = make(map[Client]time.Duration)
80+
m.clients[newPingTime] = make(map[Client]pingConfig)
7381
}
74-
m.clients[newPingTime][client] = pingInterval
82+
m.clients[newPingTime][client] = pingInfo
7583
m.clientPingMap[client] = newPingTime
7684
}
7785
}
@@ -85,20 +93,20 @@ func (m *PingManager) Start() {
8593
m.mutex.Lock()
8694
clients, ok := m.clients[nowSeconds]
8795
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())
9098
if err != nil {
9199
log.Printf("error pinging client: %v", err)
92100
m.Remove(client)
93101
continue
94102
}
95103

96104
// Reschedule the next ping for the client
97-
timeToPing := time.Now().Add(interval).Unix()
105+
timeToPing := time.Now().Add(pingCfg.interval).Unix()
98106
if m.clients[timeToPing] == nil {
99-
m.clients[timeToPing] = make(map[Client]time.Duration)
107+
m.clients[timeToPing] = make(map[Client]pingConfig)
100108
}
101-
m.clients[timeToPing][client] = interval
109+
m.clients[timeToPing][client] = pingCfg
102110
m.clientPingMap[client] = timeToPing
103111

104112
}

workerpool_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ func TestManager(t *testing.T) {
2424
client1 := &mockClient{}
2525
client2 := &mockClient{}
2626

27-
manager.Add(client1, 2*time.Second)
28-
manager.Add(client2, 1*time.Second)
27+
manager.Add(client1, 2*time.Second, func() []byte {
28+
return []byte("ping")
29+
})
30+
manager.Add(client2, 1*time.Second, func() []byte {
31+
return []byte("ping")
32+
})
2933

3034
go manager.Start()
3135

@@ -48,7 +52,9 @@ func TestManagerReset(t *testing.T) {
4852
manager := workerpool.NewPingManager()
4953

5054
client1 := &mockClient{}
51-
manager.Add(client1, 2*time.Second)
55+
manager.Add(client1, 2*time.Second, func() []byte {
56+
return []byte("ping")
57+
})
5258

5359
go manager.Start()
5460

0 commit comments

Comments
 (0)