proto-socket/go/heartbeat_timer.go
toki d0754a353a refactor: rename toki_socket to proto_socket across all languages
- Rename package/module from toki_socket to proto_socket in Dart, Kotlin, Python
- Update crosstest implementations to use renamed packages
- Add new proto_socket skill, deprecate add-toki-socket-crosstest-language skill
- Update domain rules for all languages
- Update documentation (README, PORTING_GUIDE, PROTOCOL, VERSIONING)
2026-05-02 07:19:12 +09:00

38 lines
570 B
Go

package proto_socket
import (
"sync"
"time"
)
type HeartbeatTimer struct {
mu sync.Mutex
timer *time.Timer
callback func()
}
func NewHeartbeatTimer(d time.Duration, cb func()) *HeartbeatTimer {
h := &HeartbeatTimer{callback: cb}
h.Reset(d)
return h
}
func (h *HeartbeatTimer) Reset(d time.Duration) {
h.mu.Lock()
defer h.mu.Unlock()
if h.timer != nil {
h.timer.Stop()
}
h.timer = time.AfterFunc(d, h.callback)
}
func (h *HeartbeatTimer) Stop() {
h.mu.Lock()
defer h.mu.Unlock()
if h.timer != nil {
h.timer.Stop()
h.timer = nil
}
}