Skip to content

Commit

Permalink
implement client heartbeat #460.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Dec 30, 2024
1 parent 5169e0a commit 05d44ec
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 6 deletions.
16 changes: 16 additions & 0 deletions server/internal/config/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Session struct {
ImplicitHosting bool
InactiveCursors bool
MercifulReconnect bool
HeartbeatInterval int
APIToken string

CookieEnabled bool
Expand Down Expand Up @@ -67,6 +68,11 @@ func (Session) Init(cmd *cobra.Command) error {
return err
}

cmd.PersistentFlags().Int("session.heartbeat_interval", 120, "interval in seconds for sending heartbeat messages")
if err := viper.BindPFlag("session.heartbeat_interval", cmd.PersistentFlags().Lookup("session.heartbeat_interval")); err != nil {
return err
}

cmd.PersistentFlags().String("session.api_token", "", "API token for interacting with external services")
if err := viper.BindPFlag("session.api_token", cmd.PersistentFlags().Lookup("session.api_token")); err != nil {
return err
Expand Down Expand Up @@ -112,6 +118,11 @@ func (Session) InitV2(cmd *cobra.Command) error {
return err
}

cmd.PersistentFlags().Int("heartbeat_interval", 120, "heartbeat interval in seconds")
if err := viper.BindPFlag("heartbeat_interval", cmd.PersistentFlags().Lookup("heartbeat_interval")); err != nil {
return err
}

return nil
}

Expand All @@ -125,6 +136,7 @@ func (s *Session) Set() {
s.ImplicitHosting = viper.GetBool("session.implicit_hosting")
s.InactiveCursors = viper.GetBool("session.inactive_cursors")
s.MercifulReconnect = viper.GetBool("session.merciful_reconnect")
s.HeartbeatInterval = viper.GetInt("session.heartbeat_interval")
s.APIToken = viper.GetString("session.api_token")

s.CookieEnabled = viper.GetBool("session.cookie.enabled")
Expand Down Expand Up @@ -156,4 +168,8 @@ func (s *Session) SetV2() {
s.ControlProtection = viper.GetBool("control_protection")
log.Warn().Msg("you are using v2 configuration 'NEKO_CONTROL_PROTECTION' which is deprecated, please use 'NEKO_SESSION_CONTROL_PROTECTION' instead")
}
if viper.IsSet("heartbeat_interval") {
s.HeartbeatInterval = viper.GetInt("heartbeat_interval")
log.Warn().Msg("you are using v2 configuration 'NEKO_HEARTBEAT_INTERVAL' which is deprecated, please use 'NEKO_SESSION_HEARTBEAT_INTERVAL' instead")
}
}
4 changes: 4 additions & 0 deletions server/internal/http/legacy/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const (
SYSTEM_ERROR = "system/error"
)

const (
CLIENT_HEARTBEAT = "client/heartbeat"
)

const (
SIGNAL_OFFER = "signal/offer"
SIGNAL_ANSWER = "signal/answer"
Expand Down
9 changes: 5 additions & 4 deletions server/internal/http/legacy/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ type Message struct {
}

type SystemInit struct {
Event string `json:"event"`
Locks map[string]string `json:"locks"`
ImplicitHosting bool `json:"implicit_hosting"`
FileTransfer bool `json:"file_transfer"`
Event string `json:"event"`
Locks map[string]string `json:"locks"`
ImplicitHosting bool `json:"implicit_hosting"`
FileTransfer bool `json:"file_transfer"`
HeartbeatInterval int `json:"heartbeat_interval"`
}

type SystemMessage struct {
Expand Down
5 changes: 5 additions & 0 deletions server/internal/http/legacy/wstobackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (s *session) wsToBackend(msg []byte) error {
}

switch header.Event {
// Client Events
case oldEvent.CLIENT_HEARTBEAT:
// do nothing
return nil

// Signal Events
case oldEvent.SIGNAL_OFFER:
request := &oldMessage.SignalOffer{}
Expand Down
3 changes: 2 additions & 1 deletion server/internal/http/legacy/wstoclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ func (s *session) wsToClient(msg []byte) error {
ImplicitHosting: request.Settings.ImplicitHosting,
Locks: locks,
// TODO: hack - we don't know if file transfer is enabled, we would need to check the global config.
FileTransfer: viper.GetBool("filetransfer.enabled") || (viper.GetBool("legacy") && viper.GetBool("file_transfer_enabled")),
FileTransfer: viper.GetBool("filetransfer.enabled") || (viper.GetBool("legacy") && viper.GetBool("file_transfer_enabled")),
HeartbeatInterval: request.Settings.HeartbeatInterval,
})

case event.SYSTEM_ADMIN:
Expand Down
1 change: 1 addition & 0 deletions server/internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func New(config *config.Session) *SessionManagerCtx {
ImplicitHosting: config.ImplicitHosting,
InactiveCursors: config.InactiveCursors,
MercifulReconnect: config.MercifulReconnect,
HeartbeatInterval: config.HeartbeatInterval,
},
tokens: make(map[string]string),
sessions: make(map[string]*SessionCtx),
Expand Down
4 changes: 4 additions & 0 deletions server/internal/websocket/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type MessageHandlerCtx struct {
func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketMessage) bool {
var err error
switch data.Event {
// Client Events
case event.CLIENT_HEARTBEAT:
// do nothing

// System Events
case event.SYSTEM_LOGS:
payload := &message.SystemLogs{}
Expand Down
3 changes: 2 additions & 1 deletion server/internal/websocket/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ const maxPayloadLogLength = 10_000
var nologEvents = []string{
// don't log twice
event.SYSTEM_LOGS,
// don't log heartbeat
// don't log heartbeats
event.SYSTEM_HEARTBEAT,
event.CLIENT_HEARTBEAT,
// don't log every cursor update
event.SESSION_CURSORS,
}
Expand Down
4 changes: 4 additions & 0 deletions server/pkg/types/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const (
SYSTEM_HEARTBEAT = "system/heartbeat"
)

const (
CLIENT_HEARTBEAT = "client/heartbeat"
)

const (
SIGNAL_REQUEST = "signal/request"
SIGNAL_RESTART = "signal/restart"
Expand Down
1 change: 1 addition & 0 deletions server/pkg/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Settings struct {
ImplicitHosting bool `json:"implicit_hosting"`
InactiveCursors bool `json:"inactive_cursors"`
MercifulReconnect bool `json:"merciful_reconnect"`
HeartbeatInterval int `json:"heartbeat_interval"`

// plugin scope
Plugins PluginSettings `json:"plugins"`
Expand Down

0 comments on commit 05d44ec

Please sign in to comment.