Skip to content

Commit

Permalink
notifier gateway code added mutex lock unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
nxpharryt committed Nov 9, 2024
1 parent 4153234 commit a38634b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions notifiergateway/notifiergateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"strconv"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -857,6 +858,9 @@ func RunStaleHealthReportRecordsRemoverService(stopService chan bool) {
}

go func() {
// possible race condition
var mutex sync.Mutex

for {
select {
case <-stopService:
Expand All @@ -865,7 +869,7 @@ func RunStaleHealthReportRecordsRemoverService(stopService chan bool) {

default:
log.Println(">>> Stale Health Report Record Remover - Processing Invoked <<<")
removeInactiveInstancesFromServiceDiscovery()
removeInactiveInstancesFromServiceDiscovery(&mutex)
time.Sleep(time.Duration(freq) * time.Second)
}
}
Expand All @@ -879,7 +883,7 @@ func RunStaleHealthReportRecordsRemoverService(stopService chan bool) {
// (this function is to be run from goroutine in for loop so it continuously checks for removal needs,
//
// suggest 60 second wait before re-invoke this function from for loop)
func removeInactiveInstancesFromServiceDiscovery() {
func removeInactiveInstancesFromServiceDiscovery(mutex *sync.Mutex) {
if items, e := model.ListInactiveInstancesFromDataStore(); e != nil {
// error encountered
log.Println("!!! Remove Health Report Stale Records Failed: " + e.Error() + " !!!")
Expand All @@ -891,6 +895,9 @@ func removeInactiveInstancesFromServiceDiscovery() {
keys := []*dynamodb.DynamoDBTableKeys{}
pk := fmt.Sprintf("%s#%s#service#discovery#host#health", "corems", "all")

mutex.Lock()
defer mutex.Unlock()

for _, v := range items {
if v != nil {
if e := sdDeregisterInstance(v.NamespaceId, v.ServiceId, v.InstanceId, v.AwsRegion); e == nil {
Expand Down

0 comments on commit a38634b

Please sign in to comment.