Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ping messages to keep alive the ws #17

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/asyncapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ components:
description: depth of the message exchange, used to break recursion
event:
type: string
description: type of event
description: type of event, always append new types to not break compatibility
enum:
- newChecksum
- objectAdded
Expand All @@ -220,6 +220,7 @@ components:
- getObject
- patchObject
- putObject
- ping
kind:
type: object
description: unambiguously identifies a resource
Expand Down
23 changes: 23 additions & 0 deletions core/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"time"

"github.com/gobwas/ws/wsutil"
"github.com/kubescape/go-logger"
Expand Down Expand Up @@ -111,6 +112,10 @@ func (s *Synchronizer) VerifyObjectCallback(ctx context.Context, id domain.KindN
func (s *Synchronizer) Start(ctx context.Context) error {
identifiers := utils.ClientIdentifierFromContext(ctx)
logger.L().Ctx(ctx).Info("starting sync", helpers.String("account", identifiers.Account), helpers.String("cluster", identifiers.Cluster))
if s.isClient {
// send ping
go s.sendPing(ctx)
}
// adapter events
err := s.adapter.Start(ctx)
if err != nil {
Expand Down Expand Up @@ -427,6 +432,24 @@ func (s *Synchronizer) sendPatchObject(ctx context.Context, id domain.KindName,
return nil
}

func (s *Synchronizer) sendPing(ctx context.Context) {
event := domain.EventPing
msg := domain.Generic{
Event: &event,
}
data, err := json.Marshal(msg)
if err != nil {
logger.L().Fatal("marshal ping message", helpers.Error(err))
}
for {
err = s.outPool.Invoke(data)
if err != nil {
logger.L().Ctx(ctx).Error("invoke outPool on ping message", helpers.Error(err))
}
time.Sleep(50 * time.Second)
}
}

func (s *Synchronizer) sendPutObject(ctx context.Context, id domain.KindName, object []byte) error {
event := domain.EventPutObject
depth := ctx.Value(domain.ContextKeyDepth).(int)
Expand Down
4 changes: 3 additions & 1 deletion domain/Event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
EventGetObject
EventPatchObject
EventPutObject
EventPing
)

// Value returns the value of the enum.
Expand All @@ -21,7 +22,7 @@ func (op Event) Value() any {
return EventValues[op]
}

var EventValues = []any{"newChecksum", "objectAdded", "objectDeleted", "objectModified", "getObject", "patchObject", "putObject"}
var EventValues = []any{"newChecksum", "objectAdded", "objectDeleted", "objectModified", "getObject", "patchObject", "putObject", "ping"}
var ValuesToEvent = map[any]Event{
EventValues[EventNewChecksum]: EventNewChecksum,
EventValues[EventObjectAdded]: EventObjectAdded,
Expand All @@ -30,4 +31,5 @@ var ValuesToEvent = map[any]Event{
EventValues[EventGetObject]: EventGetObject,
EventValues[EventPatchObject]: EventPatchObject,
EventValues[EventPutObject]: EventPutObject,
EventValues[EventPing]: EventPing,
}
Loading