Add mutex api wrapper

This commit is contained in:
Christian Schaible 2024-07-30 13:17:02 +02:00
parent 5d8aa9ee94
commit 23f99b0668
3 changed files with 29 additions and 2 deletions

View file

@ -197,7 +197,6 @@ Python) will be extracted into separate repositories.
- Finalizing messaging schema
- Check if fields and constraints are correct and compatible with expected use cases
- Check that routing api specific parameters are independent of AuditEventLog
- Clarify if a mutex is needed? Should we provide a MutexMessagingApi?
- Clarify if `client.go` file can be used for licence / legal reasons
- Extraction of python / java configurations and code
- Clean up repo (delete main.go, etc. files)

View file

@ -42,7 +42,7 @@ type LegacyAuditApi struct {
validator *ProtobufValidator
}
// NewLegacyAuditApi can be used to initialize the audit log api with LegacyAuditLogConnectionDetails.
// NewLegacyAuditApi can be used to initialize the audit log api.
//
// Note: The NewLegacyAuditApi method will be deprecated and replaced with "newRoutableAuditApi" once the new audit log routing is implemented
func NewLegacyAuditApi(

View file

@ -2,10 +2,12 @@ package messaging
import (
"context"
"errors"
"fmt"
"github.com/Azure/go-amqp"
"log/slog"
"strings"
"sync"
"time"
)
@ -28,6 +30,32 @@ type Api interface {
Send(ctx context.Context, topic string, data []byte, contentType string, applicationProperties map[string]any) error
}
// MutexApi is wrapper around an API implementation that controls mutual exclusive access to the api.
type MutexApi struct {
mutex *sync.Mutex
api *Api
}
func NewMutexApi(api *Api) (*Api, error) {
if api == nil {
return nil, errors.New("api is nil")
}
mutexApi := MutexApi{
mutex: &sync.Mutex{},
api: api,
}
var genericApi Api = &mutexApi
return &genericApi, nil
}
// Send implements Api.Send
func (m *MutexApi) Send(ctx context.Context, topic string, data []byte, contentType string, applicationProperties map[string]any) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return (*m.api).Send(ctx, topic, data, contentType, applicationProperties)
}
// AmqpConfig provides AMQP connection related parameters.
type AmqpConfig struct {
URL string