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

process incoming messages asynchronously with a pool #16

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
49 changes: 30 additions & 19 deletions core/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,22 @@ func (s *Synchronizer) Start(ctx context.Context) error {
}

func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
// process incoming messages
for {
data, err := s.readDataFunc(s.conn)
if err != nil {
return fmt.Errorf("cannot read server data: %w", err)
}
// incoming message pool
inPool, err := ants.NewPoolWithFunc(10, func(i interface{}) {
data := i.([]byte)
// unmarshal message
var generic domain.Generic
err = json.Unmarshal(data, &generic)
err := json.Unmarshal(data, &generic)
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err))
continue
return
}
logger.L().Debug("received message", helpers.Interface("event", generic.Event.Value()),
helpers.String("msgid", generic.MsgId), helpers.Int("depth", generic.Depth))
// check message depth and ID
if generic.Depth > maxMessageDepth {
logger.L().Ctx(ctx).Error("message depth too high", helpers.Int("depth", generic.Depth))
continue
return
}
// store in context
ctx := utils.ContextFromGeneric(ctx, generic)
Expand All @@ -155,7 +152,7 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err),
helpers.Interface("event", generic.Event.Value()))
continue
return
}
id := domain.KindName{
Kind: msg.Kind,
Expand All @@ -167,15 +164,15 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
logger.L().Ctx(ctx).Error("error handling message", helpers.Error(err),
helpers.Interface("event", msg.Event.Value()),
helpers.String("id", id.String()))
continue
return
}
case domain.EventNewChecksum:
var msg domain.NewChecksum
err = json.Unmarshal(data, &msg)
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err),
helpers.Interface("event", generic.Event.Value()))
continue
return
}
id := domain.KindName{
Kind: msg.Kind,
Expand All @@ -187,15 +184,15 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
logger.L().Ctx(ctx).Error("error handling message", helpers.Error(err),
helpers.Interface("event", msg.Event.Value()),
helpers.String("id", id.String()))
continue
return
}
case domain.EventObjectDeleted:
var msg domain.ObjectDeleted
err = json.Unmarshal(data, &msg)
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err),
helpers.Interface("event", generic.Event.Value()))
continue
return
}
id := domain.KindName{
Kind: msg.Kind,
Expand All @@ -207,15 +204,15 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
logger.L().Ctx(ctx).Error("error handling message", helpers.Error(err),
helpers.Interface("event", msg.Event.Value()),
helpers.String("id", id.String()))
continue
return
}
case domain.EventPatchObject:
var msg domain.PatchObject
err = json.Unmarshal(data, &msg)
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err),
helpers.Interface("event", generic.Event.Value()))
continue
return
}
id := domain.KindName{
Kind: msg.Kind,
Expand All @@ -227,15 +224,15 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
logger.L().Ctx(ctx).Error("error handling message", helpers.Error(err),
helpers.Interface("event", msg.Event.Value()),
helpers.String("id", id.String()))
continue
return
}
case domain.EventPutObject:
var msg domain.PutObject
err = json.Unmarshal(data, &msg)
if err != nil {
logger.L().Ctx(ctx).Error("cannot unmarshal message", helpers.Error(err),
helpers.Interface("event", generic.Event.Value()))
continue
return
}
id := domain.KindName{
Kind: msg.Kind,
Expand All @@ -247,9 +244,23 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
logger.L().Ctx(ctx).Error("error handling message", helpers.Error(err),
helpers.Interface("event", msg.Event.Value()),
helpers.String("id", id.String()))
continue
return
}
}
})
if err != nil {
logger.L().Ctx(ctx).Fatal("unable to create incoming message pool", helpers.Error(err))
}
// process incoming messages
for {
data, err := s.readDataFunc(s.conn)
if err != nil {
return fmt.Errorf("cannot read server data: %w", err)
}
err = inPool.Invoke(data)
if err != nil {
return fmt.Errorf("invoke inPool: %w", err)
}
}
}

Expand Down
Loading