From 645232736bcf684af7d04ab3f2ee5aeeebbd3800 Mon Sep 17 00:00:00 2001 From: saurabh-narkhede Date: Fri, 3 Jan 2025 20:17:45 +0530 Subject: [PATCH] use double buffer method --- .../{gdpr_country.go => compliance.go} | 0 .../mysql/{gdpr_country.go => compliance.go} | 3 +- modules/pubmatic/openwrap/geo.go | 14 +++--- .../openwrap/publisherfeature/compliance.go | 47 +++++++++++++++++++ .../openwrap/publisherfeature/gdpr_country.go | 9 ---- .../openwrap/publisherfeature/reloader.go | 24 +--------- 6 files changed, 56 insertions(+), 41 deletions(-) rename modules/pubmatic/openwrap/cache/gocache/{gdpr_country.go => compliance.go} (100%) rename modules/pubmatic/openwrap/database/mysql/{gdpr_country.go => compliance.go} (91%) create mode 100644 modules/pubmatic/openwrap/publisherfeature/compliance.go delete mode 100644 modules/pubmatic/openwrap/publisherfeature/gdpr_country.go diff --git a/modules/pubmatic/openwrap/cache/gocache/gdpr_country.go b/modules/pubmatic/openwrap/cache/gocache/compliance.go similarity index 100% rename from modules/pubmatic/openwrap/cache/gocache/gdpr_country.go rename to modules/pubmatic/openwrap/cache/gocache/compliance.go diff --git a/modules/pubmatic/openwrap/database/mysql/gdpr_country.go b/modules/pubmatic/openwrap/database/mysql/compliance.go similarity index 91% rename from modules/pubmatic/openwrap/database/mysql/gdpr_country.go rename to modules/pubmatic/openwrap/database/mysql/compliance.go index 4d9a2b1030..775f66e9d3 100644 --- a/modules/pubmatic/openwrap/database/mysql/gdpr_country.go +++ b/modules/pubmatic/openwrap/database/mysql/compliance.go @@ -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 @@ -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 } diff --git a/modules/pubmatic/openwrap/geo.go b/modules/pubmatic/openwrap/geo.go index 10788456d8..13f97f1c33 100644 --- a/modules/pubmatic/openwrap/geo.go +++ b/modules/pubmatic/openwrap/geo.go @@ -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{}, } @@ -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) diff --git a/modules/pubmatic/openwrap/publisherfeature/compliance.go b/modules/pubmatic/openwrap/publisherfeature/compliance.go new file mode 100644 index 0000000000..4a898c9904 --- /dev/null +++ b/modules/pubmatic/openwrap/publisherfeature/compliance.go @@ -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 +} diff --git a/modules/pubmatic/openwrap/publisherfeature/gdpr_country.go b/modules/pubmatic/openwrap/publisherfeature/gdpr_country.go deleted file mode 100644 index 706e330555..0000000000 --- a/modules/pubmatic/openwrap/publisherfeature/gdpr_country.go +++ /dev/null @@ -1,9 +0,0 @@ -package publisherfeature - -// IsCountryGDPREnabled returns true if country is gdpr enabled -func (fe *feature) IsCountryGDPREnabled(countryCode string) bool { - fe.RLock() - defer fe.RUnlock() - _, enabled := fe.gdprCountryCodes[countryCode] - return enabled -} diff --git a/modules/pubmatic/openwrap/publisherfeature/reloader.go b/modules/pubmatic/openwrap/publisherfeature/reloader.go index 9b39c952d4..ce92f32a19 100644 --- a/modules/pubmatic/openwrap/publisherfeature/reloader.go +++ b/modules/pubmatic/openwrap/publisherfeature/reloader.go @@ -29,7 +29,7 @@ type feature struct { bidRecovery bidRecovery appLovinMultiFloors appLovinMultiFloors impCountingMethod impCountingMethod - gdprCountryCodes map[string]struct{} + gdprCountryCodes gdprCountryCodes } var fe *feature @@ -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 @@ -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()) - } -}