Skip to content

Commit

Permalink
Separate interceptors OnTx and AroundTx (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilKabakchiev authored Oct 28, 2019
1 parent 0870f60 commit 0ef84dd
Show file tree
Hide file tree
Showing 26 changed files with 2,806 additions and 491 deletions.
13 changes: 13 additions & 0 deletions docs/development/interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,16 @@ func (c *DeleteInterceptor)OnTxDelete(f storage.InterceptDeleteOnTxFunc) storage
}

```

**Note:** Similarly to `CreateInterceptor`, `UpdatteInterceptor` and `DeleteInterceptor`, if one wants to just have the `AroundTx`
logic, one should implement the respective `CreateAroundTxInterceptor`, `UpdateAroundTxInterceptor` or `DeleteAroundTxInterceptor`
as well as its respective provider - `CreateAroundTxInterceptorProvider`, `UpdateAroundTxInterceptorProvider` or `DeleteAroundTxInterceptorProvider`.

**Note:** Similarly to `CreateInterceptor`, `UpdatteInterceptor` and `DeleteInterceptor`, if one wants to just have the `OnTx`
logic, one should implement the respective `CreateOnTxInterceptor`, `UpdateOnTxInterceptor` or `DeleteOnTxInterceptor`
as well as its respective provider - `CreateOnTxInterceptorProvider`, `UpdateOnTxInterceptorProvider` or `DeleteOnTxInterceptorProvider`.

**Only `CreateOnTxInterceptor`, `UpdateOnTxInterceptor` and `DeleteOnTxInterceptor` interceptors will be triggered if one uses the storage `InTransaction` method directly.
This means that `CreateAroundTxInterceptor`, `UpdateAroundTxInterceptor`, `DeleteAroundTxInterceptor`,`CreateInterceptor`, `UpdateInterceptor` and `DeleteInterceptor`
interceptors will not be triggered when using `storage.InTransaction` direcrly. The reason for that is because `InTransaction` starts a database transaction and while in
transaction we don't want to execute any `AroundTx` logic or interceptors that have `AroundTx` logic.**
131 changes: 112 additions & 19 deletions pkg/sm/sm.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ func New(ctx context.Context, cancel context.CancelFunc, cfg *config.Settings) (
WithDeleteInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerDeleteCatalogInterceptorProvider{
CatalogLoader: catalog.Load,
}).Register().
WithCreateInterceptorProvider(types.PlatformType, &interceptors.GenerateCredentialsInterceptorProvider{}).Register().
WithCreateInterceptorProvider(types.VisibilityType, &interceptors.VisibilityCreateNotificationsInterceptorProvider{}).Register().
WithUpdateInterceptorProvider(types.VisibilityType, &interceptors.VisibilityUpdateNotificationsInterceptorProvider{}).Register().
WithDeleteInterceptorProvider(types.VisibilityType, &interceptors.VisibilityDeleteNotificationsInterceptorProvider{}).Register().
WithCreateInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsCreateInterceptorProvider{}).Before(interceptors.BrokerCreateCatalogInterceptorName).Register().
WithUpdateInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsUpdateInterceptorProvider{}).Before(interceptors.BrokerUpdateCatalogInterceptorName).Register().
WithDeleteInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsDeleteInterceptorProvider{}).After(interceptors.BrokerDeleteCatalogInterceptorName).Register()
WithCreateAroundTxInterceptorProvider(types.PlatformType, &interceptors.GenerateCredentialsInterceptorProvider{}).Register().
WithCreateOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityCreateNotificationsInterceptorProvider{}).Register().
WithUpdateOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityUpdateNotificationsInterceptorProvider{}).Register().
WithDeleteOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityDeleteNotificationsInterceptorProvider{}).Register().
WithCreateOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsCreateInterceptorProvider{}).Before(interceptors.BrokerCreateCatalogInterceptorName).Register().
WithUpdateOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsUpdateInterceptorProvider{}).Before(interceptors.BrokerUpdateCatalogInterceptorName).Register().
WithDeleteOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsDeleteInterceptorProvider{}).After(interceptors.BrokerDeleteCatalogInterceptorName).Register()

return smb, nil
}
Expand Down Expand Up @@ -240,6 +240,40 @@ func (smb *ServiceManagerBuilder) RegisterNotificationReceiversFilter(filterFunc
smb.Notificator.RegisterFilter(filterFunc)
}

func (smb *ServiceManagerBuilder) WithCreateAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.CreateAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithCreateOnTxInterceptorProvider(objectType types.ObjectType, provider storage.CreateOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithCreateInterceptorProvider(objectType types.ObjectType, provider storage.CreateInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
Expand All @@ -251,10 +285,41 @@ func (smb *ServiceManagerBuilder) WithCreateInterceptorProvider(objectType types
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateInterceptorProvider(objectType, storage.OrderedCreateInterceptorProvider{
CreateInterceptorProvider: provider,
InterceptorOrder: order,
})
smb.Storage.AddCreateInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithUpdateAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.UpdateAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithUpdateOnTxInterceptorProvider(objectType types.ObjectType, provider storage.UpdateOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
Expand All @@ -271,10 +336,41 @@ func (smb *ServiceManagerBuilder) WithUpdateInterceptorProvider(objectType types
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateInterceptorProvider(objectType, storage.OrderedUpdateInterceptorProvider{
UpdateInterceptorProvider: provider,
InterceptorOrder: order,
})
smb.Storage.AddUpdateInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithDeleteAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.DeleteAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}

func (smb *ServiceManagerBuilder) WithDeleteOnTxInterceptorProvider(objectType types.ObjectType, provider storage.DeleteOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
Expand All @@ -291,10 +387,7 @@ func (smb *ServiceManagerBuilder) WithDeleteInterceptorProvider(objectType types
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteInterceptorProvider(objectType, storage.OrderedDeleteInterceptorProvider{
DeleteInterceptorProvider: provider,
InterceptorOrder: order,
})
smb.Storage.AddDeleteInterceptorProvider(objectType, provider, order)
return smb
},
}
Expand Down
Loading

0 comments on commit 0ef84dd

Please sign in to comment.