Skip to content

Commit

Permalink
use double buffer method
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-saurabh-narkhede committed Jan 3, 2025
1 parent 94fa135 commit 6452327
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (db *mySqlDB) GetGDPRCountryCodes() (map[string]struct{}, error) {
}
defer rows.Close()

// Map to store the country codes
// map to store the country codes
countryCodes := make(map[string]struct{})
for rows.Next() {
var countryCode string
Expand All @@ -28,7 +28,6 @@ func (db *mySqlDB) GetGDPRCountryCodes() (map[string]struct{}, error) {
countryCodes[countryCode] = struct{}{}
}

//TO-DO - partial error is allowed?
if err = rows.Err(); err != nil {
return nil, err
}
Expand Down
14 changes: 6 additions & 8 deletions modules/pubmatic/openwrap/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type geoHandler struct {
geoService netacuity.NetAcuity
}

// NewGeoHandler initializes and returns a new GeoHandler.
func NewGeoHandler() *geoHandler {
// NewGeo initializes and returns a new GeoHandler.
func NewGeo() *geoHandler {
return &geoHandler{
geoService: netacuity.NetAcuity{},
}
Expand All @@ -64,30 +64,28 @@ const (
)

// Handler for /geo endpoint
func (handler *geoHandler) Handle(w http.ResponseWriter, r *http.Request) {
func (handler *geoHandler) Handler(w http.ResponseWriter, r *http.Request) {
var pubIdStr string
metricEngine := ow.GetMetricEngine()
metricLabels := metrics.Labels{RType: models.EndpointGeo, RequestStatus: prometheus.RequestStatusOK}
defer func() {
metricEngine.RecordRequest(metricLabels)
if r := recover(); r != nil {
metricEngine.RecordOpenWrapServerPanicStats(ow.cfg.Server.HostName, "HandleGeoEndpoint")
glog.Errorf("stacktrace:[%s], error:[%v], pubid:[%s]", string(debug.Stack()), r, pubIdStr)
return
}
}()
metricEngine.RecordRequest(metrics.Labels{RType: models.EndpointGeo, RequestStatus: prometheus.RequestStatusOK})

pubIdStr = r.URL.Query().Get(models.PublisherID)
_, err := strconv.Atoi(pubIdStr)
if err != nil {
glog.Errorf("[geo] error:[invalid pubid passed:%s], [requestType]:%v [url]:%v [origin]:%v [referer]:%v", err.Error(), models.EndpointGeo,
r.URL.RequestURI(), r.Header.Get(OriginHeaderKey), r.Header.Get(RefererKey))

//TO-Do keep this stat?
metricEngine.RecordBadRequests(models.EndpointGeo, pubIdStr, -1)
w.WriteHeader(http.StatusBadRequest)
metricLabels.RequestStatus = prometheus.RequestStatusBadInput
return
}
metricEngine.RecordPublisherRequests(models.EndpointGeo, pubIdStr, "")

ip := util.GetIP(r)
w.Header().Set(headerContentType, headerContentTypeValue)
Expand Down
47 changes: 47 additions & 0 deletions modules/pubmatic/openwrap/publisherfeature/compliance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package publisherfeature

import (
"github.com/golang/glog"
"github.com/prebid/prebid-server/v2/modules/pubmatic/openwrap/models"
)

type gdprCountryCodes struct {
codes [2]map[string]struct{}
index int
}

func newGDPRCountryCodes() gdprCountryCodes {
return gdprCountryCodes{
codes: [2]map[string]struct{}{
make(map[string]struct{}),
make(map[string]struct{}),
},
index: 0,
}
}

func (fe *feature) updateGDPRCountryCodes() {
var err error
//fetch gdpr countrycodes
gdprCountryCodes, errorGDPRCountryUpdate := fe.cache.GetGDPRCountryCodes()
if errorGDPRCountryUpdate != nil {
err = models.ErrorWrap(err, errorGDPRCountryUpdate)
}
// assign fetched codes to the inactive map
if gdprCountryCodes != nil {
fe.gdprCountryCodes.codes[fe.gdprCountryCodes.index^1] = gdprCountryCodes
}

// toggle the index to make the updated map active
fe.gdprCountryCodes.index ^= 1
if err != nil {
glog.Error(err.Error())
}
}

// IsCountryGDPREnabled returns true if country is gdpr enabled
func (fe *feature) IsCountryGDPREnabled(countryCode string) bool {
codes := fe.gdprCountryCodes.codes[fe.gdprCountryCodes.index]
_, enabled := codes[countryCode]
return enabled
}
9 changes: 0 additions & 9 deletions modules/pubmatic/openwrap/publisherfeature/gdpr_country.go

This file was deleted.

24 changes: 2 additions & 22 deletions modules/pubmatic/openwrap/publisherfeature/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type feature struct {
bidRecovery bidRecovery
appLovinMultiFloors appLovinMultiFloors
impCountingMethod impCountingMethod
gdprCountryCodes map[string]struct{}
gdprCountryCodes gdprCountryCodes
}

var fe *feature
Expand Down Expand Up @@ -63,7 +63,7 @@ func New(config Config) *feature {
enabledPublisherProfile: make(map[int]map[string]models.ApplovinAdUnitFloors),
},
impCountingMethod: newImpCountingMethod(),
gdprCountryCodes: make(map[string]struct{}),
gdprCountryCodes: newGDPRCountryCodes(),
}
})
return fe
Expand Down Expand Up @@ -129,23 +129,3 @@ func (fe *feature) updateFeatureConfigMaps() {
glog.Error(err.Error())
}
}

func (fe *feature) updateGDPRCountryCodes() {
var err error
//fetch gdpr countrycodes
gdprCountryCodes, errorGDPRCountryUpdate := fe.cache.GetGDPRCountryCodes()
if errorGDPRCountryUpdate != nil {
err = models.ErrorWrap(err, errorGDPRCountryUpdate)
}

//set updated countrycodes in map
fe.Lock()
if gdprCountryCodes != nil {
fe.gdprCountryCodes = gdprCountryCodes
}
fe.Unlock()

if err != nil {
glog.Error(err.Error())
}
}

0 comments on commit 6452327

Please sign in to comment.