Skip to content

Commit

Permalink
remove unused fields in notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Dec 12, 2023
1 parent 4c5c480 commit d5c0634
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
50 changes: 22 additions & 28 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,19 @@ type Notifier interface {
GetReadBatchSize() int64
}

type SendersNotificationChannel map[string]chan NotificationPackage

// StandardNotifier represent notification functionality
type StandardNotifier struct {
waitGroup sync.WaitGroup
sendersNotificationsCh map[string]chan NotificationPackage
logger moira.Logger
database moira.Database
scheduler Scheduler
config Config
metrics *metrics.NotifierMetrics
metricSourceProvider *metricSource.SourceProvider
imageStores map[string]moira.ImageStore
sendersNameToType map[string]string
waitGroup sync.WaitGroup
sendersChannel SendersNotificationChannel
logger moira.Logger
database moira.Database
scheduler Scheduler
config Config
metrics *metrics.NotifierMetrics
metricSourceProvider *metricSource.SourceProvider
imageStores map[string]moira.ImageStore
}

// NewNotifier is initializer for StandardNotifier
Expand All @@ -96,29 +97,22 @@ func NewNotifier(
scheduler Scheduler,
) *StandardNotifier {
return &StandardNotifier{
sendersNotificationsCh: make(map[string]chan NotificationPackage),
logger: logger,
database: database,
scheduler: scheduler,
config: config,
metrics: metrics,
metricSourceProvider: metricSourceProvider,
imageStores: imageStoreMap,
sendersNameToType: make(map[string]string),
sendersChannel: make(SendersNotificationChannel),
logger: logger,
database: database,
scheduler: scheduler,
config: config,
metrics: metrics,
metricSourceProvider: metricSourceProvider,
imageStores: imageStoreMap,
}
}

// Send is realization of StandardNotifier Send functionality
func (notifier *StandardNotifier) Send(pkg *NotificationPackage, waitGroup *sync.WaitGroup) {
senderType, ok := notifier.sendersNameToType[pkg.Contact.Type]
if !ok {
notifier.resend(pkg, fmt.Sprintf("Unknown contact type '%s' [%s]", pkg.Contact.Type, pkg))
return
}

ch, found := notifier.sendersNotificationsCh[senderType]
ch, found := notifier.sendersChannel[pkg.Contact.Type]
if !found {
notifier.resend(pkg, fmt.Sprintf("failed to get notification channel from '%s' [%s]", senderType, pkg))
notifier.resend(pkg, fmt.Sprintf("Unknown contact type '%s' [%s]", pkg.Contact.Type, pkg))
return
}

Expand All @@ -143,7 +137,7 @@ func (notifier *StandardNotifier) Send(pkg *NotificationPackage, waitGroup *sync
// GetSenders get hash of registered notifier senders
func (notifier *StandardNotifier) GetSenders() map[string]bool {
hash := make(map[string]bool)
for key := range notifier.sendersNotificationsCh {
for key := range notifier.sendersChannel {
hash[key] = true
}
return hash
Expand Down
1 change: 0 additions & 1 deletion notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ func configureNotifier(t *testing.T, config Config) {

Convey("Should return one sender", t, func() {
So(err, ShouldBeNil)
So(standardNotifier.sendersNameToType["test"], ShouldResemble, senderSettings["type"])
So(standardNotifier.GetSenders(), ShouldResemble, map[string]bool{"test": true})
})
}
Expand Down
8 changes: 3 additions & 5 deletions notifier/registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ func (notifier *StandardNotifier) RegisterSender(senderSettings map[string]inter
senderIdent = senderType
}

notifier.sendersNameToType[senderIdent] = senderType

if !notifier.GetSenders()[senderType] {
eventsChannel := make(chan NotificationPackage)
notifier.sendersNotificationsCh[senderType] = eventsChannel
notifier.sendersChannel[senderType] = eventsChannel
notifier.registerMetrics(senderType)
notifier.runSenders(sender, eventsChannel)
}
Expand All @@ -174,11 +172,11 @@ func (notifier *StandardNotifier) runSenders(sender moira.Sender, eventsChannel

// StopSenders close all sending channels
func (notifier *StandardNotifier) StopSenders() {
for _, ch := range notifier.sendersNotificationsCh {
for _, ch := range notifier.sendersChannel {
close(ch)
}

notifier.sendersNotificationsCh = make(map[string]chan NotificationPackage)
notifier.sendersChannel = make(map[string]chan NotificationPackage)
notifier.logger.Info().Msg("Waiting senders finish...")
notifier.waitGroup.Wait()
notifier.logger.Info().Msg("Moira Notifier Senders stopped")
Expand Down
3 changes: 0 additions & 3 deletions notifier/registrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestRegisterSender(t *testing.T) {

err := standardNotifier.RegisterSender(senderSettings, sender)
So(err, ShouldBeNil)
So(standardNotifier.sendersNameToType["test"], ShouldEqual, senderSettings["type"])
})

Convey("With multiple senders one type", func() {
Expand All @@ -43,7 +42,6 @@ func TestRegisterSender(t *testing.T) {

err := standardNotifier.RegisterSender(senderSettings, sender)
So(err, ShouldBeNil)
So(standardNotifier.sendersNameToType["test_name"], ShouldEqual, senderSettings["type"])
})

senderSettings["name"] = "test_name_2"
Expand All @@ -53,7 +51,6 @@ func TestRegisterSender(t *testing.T) {

err := standardNotifier.RegisterSender(senderSettings, sender)
So(err, ShouldBeNil)
So(standardNotifier.sendersNameToType["test_name_2"], ShouldEqual, senderSettings["type"])
})
})
})
Expand Down

0 comments on commit d5c0634

Please sign in to comment.