Skip to content

Commit

Permalink
Merge pull request #19 from Canva/sasan-s3-sink-add-mutex-to-message
Browse files Browse the repository at this point in the history
Add mutex lock to Message struct
  • Loading branch information
sasanrose authored May 3, 2023
2 parents ee1673b + c54f0fe commit 63ee5d8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion public/service/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"sync"

"github.com/benthosdev/benthos/v4/internal/bloblang/mapping"
"github.com/benthosdev/benthos/v4/internal/bloblang/query"
Expand All @@ -22,7 +23,9 @@ type MessageBatchHandlerFunc func(context.Context, MessageBatch) error
// Message represents a single discrete message passing through a Benthos
// pipeline. It is safe to mutate the message via Set methods, but the
// underlying byte data should not be edited directly.
// The Lock can be used to safely work with a Message concurrently in go routines
type Message struct {
Lock *sync.RWMutex
part *message.Part
}

Expand Down Expand Up @@ -61,11 +64,12 @@ func (b MessageBatch) DeepCopy() MessageBatch {
func NewMessage(content []byte) *Message {
return &Message{
part: message.NewPart(content),
Lock: &sync.RWMutex{},
}
}

func newMessageFromPart(part *message.Part) *Message {
return &Message{part}
return &Message{part: part, Lock: &sync.RWMutex{}}
}

// Copy creates a shallow copy of a message that is safe to mutate with Set
Expand All @@ -74,6 +78,7 @@ func newMessageFromPart(part *message.Part) *Message {
func (m *Message) Copy() *Message {
return &Message{
part: m.part.ShallowCopy(),
Lock: &sync.RWMutex{},
}
}

Expand All @@ -88,6 +93,7 @@ func (m *Message) Copy() *Message {
func (m *Message) DeepCopy() *Message {
return &Message{
part: m.part.DeepCopy(),
Lock: &sync.RWMutex{},
}
}

Expand All @@ -101,6 +107,7 @@ func (m *Message) Context() context.Context {
func (m *Message) WithContext(ctx context.Context) *Message {
return &Message{
part: message.WithContext(ctx, m.part),
Lock: &sync.RWMutex{},
}
}

Expand Down

0 comments on commit 63ee5d8

Please sign in to comment.