Skip to content

Commit 9eb9038

Browse files
committed
Avoid publishing data events during transaction
Signed-off-by: Derek McGowan <[email protected]>
1 parent 86530c0 commit 9eb9038

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

core/metadata/db.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,20 @@ func (m *DB) Update(fn func(*bolt.Tx) error) error {
270270
return err
271271
}
272272

273+
// Publisher returns an event publisher if one is configured
274+
// and the current context is not inside a transaction.
275+
func (m *DB) Publisher(ctx context.Context) events.Publisher {
276+
_, ok := ctx.Value(transactionKey{}).(*bolt.Tx)
277+
if ok {
278+
// Do no publish events within a transaction
279+
return nil
280+
}
281+
if m.dbopts.publisher != nil {
282+
return m.dbopts.publisher
283+
}
284+
return nil
285+
}
286+
273287
// RegisterMutationCallback registers a function to be called after a metadata
274288
// mutations has been performed.
275289
//

core/metadata/images.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func (s *imageStore) Create(ctx context.Context, image images.Image) (images.Ima
165165
return images.Image{}, err
166166
}
167167

168-
if s.db.dbopts.publisher != nil {
169-
if err := s.db.dbopts.publisher.Publish(ctx, "/images/create", &eventstypes.ImageCreate{
168+
if publisher := s.db.Publisher(ctx); publisher != nil {
169+
if err := publisher.Publish(ctx, "/images/create", &eventstypes.ImageCreate{
170170
Name: image.Name,
171171
Labels: image.Labels,
172172
}); err != nil {
@@ -266,8 +266,8 @@ func (s *imageStore) Update(ctx context.Context, image images.Image, fieldpaths
266266
return images.Image{}, err
267267
}
268268

269-
if s.db.dbopts.publisher != nil {
270-
if err := s.db.dbopts.publisher.Publish(ctx, "/images/update", &eventstypes.ImageUpdate{
269+
if publisher := s.db.Publisher(ctx); publisher != nil {
270+
if err := publisher.Publish(ctx, "/images/update", &eventstypes.ImageUpdate{
271271
Name: updated.Name,
272272
Labels: updated.Labels,
273273
}); err != nil {
@@ -333,8 +333,8 @@ func (s *imageStore) Delete(ctx context.Context, name string, opts ...images.Del
333333
return err
334334
}
335335

336-
if s.db.dbopts.publisher != nil {
337-
if err := s.db.dbopts.publisher.Publish(ctx, "/images/delete", &eventstypes.ImageDelete{
336+
if publisher := s.db.Publisher(ctx); publisher != nil {
337+
if err := publisher.Publish(ctx, "/images/delete", &eventstypes.ImageDelete{
338338
Name: name,
339339
}); err != nil {
340340
return err

core/metadata/snapshot.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ func (s *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...s
279279
return nil, err
280280
}
281281

282-
if s.db.dbopts.publisher != nil {
283-
if err := s.db.dbopts.publisher.Publish(ctx, "/snapshot/prepare", &eventstypes.SnapshotPrepare{
282+
if publisher := s.db.Publisher(ctx); publisher != nil {
283+
if err := publisher.Publish(ctx, "/snapshot/prepare", &eventstypes.SnapshotPrepare{
284284
Key: key,
285285
Parent: parent,
286286
Snapshotter: s.name,
@@ -634,8 +634,8 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
634634
return err
635635
}
636636

637-
if s.db.dbopts.publisher != nil {
638-
if err := s.db.dbopts.publisher.Publish(ctx, "/snapshot/commit", &eventstypes.SnapshotCommit{
637+
if publisher := s.db.Publisher(ctx); publisher != nil {
638+
if err := publisher.Publish(ctx, "/snapshot/commit", &eventstypes.SnapshotCommit{
639639
Key: key,
640640
Name: name,
641641
Snapshotter: s.name,
@@ -704,8 +704,8 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
704704
return err
705705
}
706706

707-
if s.db.dbopts.publisher != nil {
708-
return s.db.dbopts.publisher.Publish(ctx, "/snapshot/remove", &eventstypes.SnapshotRemove{
707+
if publisher := s.db.Publisher(ctx); publisher != nil {
708+
return publisher.Publish(ctx, "/snapshot/remove", &eventstypes.SnapshotRemove{
709709
Key: key,
710710
Snapshotter: s.name,
711711
})

0 commit comments

Comments
 (0)