mirror of
https://github.com/fosrl/olm.git
synced 2026-03-02 08:46:44 +00:00
Add ping for connectivity monitoring
This commit is contained in:
38
common.go
38
common.go
@@ -60,6 +60,7 @@ var (
|
|||||||
peerMonitor *peermonitor.PeerMonitor
|
peerMonitor *peermonitor.PeerMonitor
|
||||||
stopHolepunch chan struct{}
|
stopHolepunch chan struct{}
|
||||||
stopRegister chan struct{}
|
stopRegister chan struct{}
|
||||||
|
stopPing chan struct{}
|
||||||
olmToken string
|
olmToken string
|
||||||
gerbilServerPubKey string
|
gerbilServerPubKey string
|
||||||
)
|
)
|
||||||
@@ -383,3 +384,40 @@ func FindAvailableUDPPort(minPort, maxPort uint16) (uint16, error) {
|
|||||||
|
|
||||||
return 0, fmt.Errorf("no available UDP ports found in range %d-%d", minPort, maxPort)
|
return 0, fmt.Errorf("no available UDP ports found in range %d-%d", minPort, maxPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendPing(olm *websocket.Client) error {
|
||||||
|
err := olm.SendMessage("olm/ping", map[string]interface{}{
|
||||||
|
"timestamp": time.Now().Unix(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to send ping message: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Debug("Sent ping message")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func keepSendingPing(olm *websocket.Client) {
|
||||||
|
// Send ping immediately on startup
|
||||||
|
if err := sendPing(olm); err != nil {
|
||||||
|
logger.Error("Failed to send initial ping: %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Info("Sent initial ping message")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up ticker for one minute intervals
|
||||||
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stopPing:
|
||||||
|
logger.Info("Stopping ping messages")
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := sendPing(olm); err != nil {
|
||||||
|
logger.Error("Failed to send periodic ping: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -135,6 +135,7 @@ func main() {
|
|||||||
|
|
||||||
stopHolepunch = make(chan struct{})
|
stopHolepunch = make(chan struct{})
|
||||||
stopRegister = make(chan struct{})
|
stopRegister = make(chan struct{})
|
||||||
|
stopPing = make(chan struct{})
|
||||||
|
|
||||||
// if PANGOLIN_ENDPOINT, OLM_ID, and OLM_SECRET are set as environment variables, they will be used as default values
|
// if PANGOLIN_ENDPOINT, OLM_ID, and OLM_SECRET are set as environment variables, they will be used as default values
|
||||||
endpoint = os.Getenv("PANGOLIN_ENDPOINT")
|
endpoint = os.Getenv("PANGOLIN_ENDPOINT")
|
||||||
@@ -473,6 +474,7 @@ func main() {
|
|||||||
logger.Debug("Public key: %s", publicKey)
|
logger.Debug("Public key: %s", publicKey)
|
||||||
|
|
||||||
go keepSendingRegistration(olm, publicKey.String())
|
go keepSendingRegistration(olm, publicKey.String())
|
||||||
|
go keepSendingPing(olm)
|
||||||
|
|
||||||
logger.Info("Sent registration message")
|
logger.Info("Sent registration message")
|
||||||
return nil
|
return nil
|
||||||
@@ -509,6 +511,13 @@ func main() {
|
|||||||
close(stopRegister)
|
close(stopRegister)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-stopPing:
|
||||||
|
// Channel already closed
|
||||||
|
default:
|
||||||
|
close(stopPing)
|
||||||
|
}
|
||||||
|
|
||||||
uapi.Close()
|
uapi.Close()
|
||||||
dev.Close()
|
dev.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user