Skip to content

Commit

Permalink
delegating locking to SetRank
Browse files Browse the repository at this point in the history
  • Loading branch information
k4lizen committed Jul 31, 2023
1 parent 6e256b1 commit f9d65c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/engines/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ func setResultResponse(link string, response *colly.Response, relay *structures.
log.Trace().Msgf("Got Response %v", link)

relay.Mutex.Lock()
defer relay.Mutex.Unlock()

mapRes, exists := relay.ResultMap[link]

if !exists {
log.Error().Msgf("URL not in map when adding response! Should not be possible. URL: %v", link)
relay.Mutex.Unlock()
return
}

mapRes.Response = response
rank.SetRank(mapRes) //IF I PASS COPY HERE, THAN I CAN UNLOCK EARLIER (MAYBE)

resCopy := *mapRes
rankAddr := &(mapRes.Rank)
relay.Mutex.Unlock()
rank.SetRank(&resCopy, rankAddr, &(relay.Mutex)) //copy contains pointer to response
}

func getPageNum(uri string) int {
Expand Down
26 changes: 23 additions & 3 deletions src/rank/rank.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package rank

import (
"sync"

"github.com/rs/zerolog/log"
"github.com/tminaorg/brzaguza/src/structures"
)

func SetRank(result *structures.Result) {
result.Rank = result.SEPage*100 + result.SEPageRank
// only one call for setResultResponse for a page will ever be performed, so there will never be a read and a write to result.Response at the same time
// thus not locking relay.ResultMap[result.URL] in this function, because the result parameter is a copy is safe. It may, however, read memory that the map sees,
// at the same time that the map is being written to, while this should be fine in this use-case, go may throw an error.
// TLDR: you must mutex.Lock when changing *rankAddr, you probably dont need to mutex.RLock() when reading result
// (in reality even *rankAddr shouldnt need a lock, but go would definately complain about simultanious read/write because of it)
func SetRank(result *structures.Result, rankAddr *int, mutex *sync.RWMutex) {

mutex.RLock()
reqUrl := result.Response.Request.URL.String() //dummy code
mutex.RUnlock()

if reqUrl != result.URL { //dummy code
log.Trace().Msg("Request URL not same as result.URL \\/")
}

rrank := result.SEPage*100 + result.SEPageRank

mutex.Lock()
*rankAddr = rrank
mutex.Unlock()

log.Trace().Msgf("Set rank to %v for %v: %v", result.Rank, result.Title, result.URL)
log.Trace().Msgf("Set rank to %v for %v: %v", rrank, result.Title, result.URL)
}

0 comments on commit f9d65c8

Please sign in to comment.