Skip to content

Commit

Permalink
Keep views and claims in psql instead of redis 馃挘
Browse files Browse the repository at this point in the history
It was a time bomb for two main reasons:
1. Redis storage would keep on growing indefinetly
2. Redis data can be lost so we'd have no way of recovering previous impressions and transactions.
  • Loading branch information
cscatolini committed Feb 17, 2018
1 parent 26d81e9 commit 7ca6927
Show file tree
Hide file tree
Showing 33 changed files with 493 additions and 698 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ ENV OFFERS_POSTGRES_HOST localhost
ENV OFFERS_POSTGRES_PASSWORD ""
ENV OFFERS_POSTGRES_PORT 8585
ENV OFFERS_POSTGRES_USER offers
ENV OFFERS_REDIS_HOST localhost
ENV OFFERS_REDIS_PASSWORD ""
ENV OFFERS_REDIS_PORT 6333

CMD /app/offers start -v2 -c /app/config/local.yaml
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Offers is a service meant to handle offers/promotions in your games.
### Dependencies
* Go 1.7
* Postgres >= 9.5
* Redis

### Setup
First, set your $GOPATH ([Go Lang](https://golang.org/doc/install)) env variable and add $GOPATH/bin to your $PATH
Expand Down Expand Up @@ -52,12 +51,6 @@ Offers uses PostgreSQL to store offers information. The container takes environm
* `OFFERS_POSTGRES_PASSWORD` - Password of the PostgreSQL Server to connect to;
* `OFFERS_POSTGRES_USER` - PostgreSQL user;

Offers also uses Redis to store offers views and purchases information. The container takes environment variables to specify this connection:

* `OFFERS_REDIS_HOST` - Redis host to connect to;
* `OFFERS_REDIS_PORT` - Redis port to connect to;
* `OFFERS_REDIS_PASSWORD` - Password of the Redis database to connect to;

Offers uses basic auth to restrict access to routes that are not used directly by a client consuming the offers.

* `OFFERS_BASICAUTH_USERNAME` - Basic Auth user;
Expand Down
8 changes: 3 additions & 5 deletions api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ var _ = BeforeSuite(func() {
db, err = oTesting.GetTestDB()
Expect(err).NotTo(HaveOccurred())

err = oTesting.ClearOfferPlayers(db)
Expect(err).NotTo(HaveOccurred())

err = oTesting.LoadFixtures(db)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -62,8 +65,6 @@ var _ = AfterEach(func() {
err := app.DB.(*runner.Tx).Rollback()
Expect(err).NotTo(HaveOccurred())
app.DB = db
status := app.RedisClient.Client.FlushAll()
Expect(status.Err()).NotTo(HaveOccurred())
app.Clock = oTesting.MockClock{
CurrentTime: 1486678000,
}
Expand All @@ -77,9 +78,6 @@ var _ = AfterSuite(func() {
db = nil
}

status := app.RedisClient.Client.FlushAll()
Expect(status.Err()).NotTo(HaveOccurred())

if closer != nil {
closer.Close()
closer = nil
Expand Down
32 changes: 0 additions & 32 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/topfreegames/offers/errors"
"github.com/topfreegames/offers/metadata"
"github.com/topfreegames/offers/models"
"github.com/topfreegames/offers/util"
runner "gopkg.in/mgutz/dat.v2/sqlx-runner"
)

Expand All @@ -39,7 +38,6 @@ type App struct {
Logger logrus.FieldLogger
MaxAge int64
NewRelic newrelic.Application
RedisClient *util.RedisClient
Router *mux.Router
Server *http.Server
Cache *cache.Cache
Expand Down Expand Up @@ -208,11 +206,6 @@ func (a *App) configureApp() error {
return err
}

err = a.configureRedisClient()
if err != nil {
return err
}

err = a.configureNewRelic()
if err != nil {
return err
Expand Down Expand Up @@ -243,31 +236,6 @@ func (a *App) configureCache() {
a.OffersCacheMaxAge = maxAge
}

func (a *App) configureRedisClient() error {
redisHost := a.Config.GetString("redis.host")
redisPort := a.Config.GetInt("redis.port")
redisPass := a.Config.GetString("redis.password")
redisDB := a.Config.GetInt("redis.db")
redisMaxPoolSize := a.Config.GetInt("redis.maxPoolSize")

l := a.Logger.WithFields(logrus.Fields{
"redis.host": redisHost,
"redis.port": redisPort,
"redis.redisDB": redisDB,
"redis.redisMaxPoolSize": redisMaxPoolSize,
})
l.Debug("Connecting to Redis...")
cli, err := util.GetRedisClient(redisHost, redisPort, redisPass, redisDB, redisMaxPoolSize, a.Logger)
if err != nil {
l.WithError(err).Error("Connection to redis failed.")
return err
}
l.Debug("Successful connection to redis.")
a.RedisClient = cli

return nil
}

func (a *App) configureDatabase() error {
db, err := a.getDB()
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions api/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ var _ = Describe("Game Handler", func() {
var obj []map[string]interface{}
err := json.Unmarshal([]byte(recorder.Body.String()), &obj)
Expect(err).NotTo(HaveOccurred())
Expect(obj).To(HaveLen(9))
Expect(obj).To(HaveLen(13))
for i := 0; i < len(obj); i++ {
Expect(obj[i]).To(HaveKey("id"))
Expect(obj[i]).To(HaveKey("name"))
Expand All @@ -220,7 +220,9 @@ var _ = Describe("Game Handler", func() {
})

It("should return empty list if no games", func() {
_, err := app.DB.DeleteFrom("offer_instances").Exec()
_, err := app.DB.DeleteFrom("offer_players").Exec()
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("offer_instances").Exec()
Expect(err).NotTo(HaveOccurred())
_, err = app.DB.DeleteFrom("offers").Exec()
Expect(err).NotTo(HaveOccurred())
Expand Down
6 changes: 2 additions & 4 deletions api/offer_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (h *OfferRequestHandler) getOffers(w http.ResponseWriter, r *http.Request)

var offers map[string][]*models.OfferToReturn
err = mr.WithSegment(models.SegmentModel, func() error {
offers, err = models.GetAvailableOffers(h.App.DB, h.App.RedisClient, h.App.Cache, gameID, playerID, currentTime, h.App.OffersCacheMaxAge, filterAttrs, allowInefficientQueries, mr)
offers, err = models.GetAvailableOffers(h.App.DB, h.App.Cache, gameID, playerID, currentTime, h.App.OffersCacheMaxAge, filterAttrs, allowInefficientQueries, mr)
return err
})

Expand Down Expand Up @@ -152,7 +152,6 @@ func (h *OfferRequestHandler) claimOffer(w http.ResponseWriter, r *http.Request)

contents, alreadyClaimed, nextAt, err := models.ClaimOffer(
h.App.DB,
h.App.RedisClient,
payload.GameID,
payload.OfferInstanceID,
payload.PlayerID,
Expand Down Expand Up @@ -203,7 +202,6 @@ func (h *OfferRequestHandler) viewOffer(w http.ResponseWriter, r *http.Request)

alreadyViewed, nextAt, err := models.ViewOffer(
h.App.DB,
h.App.RedisClient,
payload.GameID,
offerInstanceID,
payload.PlayerID,
Expand Down Expand Up @@ -267,7 +265,7 @@ func (h *OfferRequestHandler) offerInfo(w http.ResponseWriter, r *http.Request)
var err error
var offer *models.OfferToReturn
err = mr.WithSegment(models.SegmentModel, func() error {
offer, err = models.GetOfferInfo(h.App.DB, h.App.RedisClient, gameID, playerID, offerInstanceID, h.App.OffersCacheMaxAge, mr)
offer, err = models.GetOfferInfo(h.App.DB, gameID, playerID, offerInstanceID, h.App.OffersCacheMaxAge, mr)
return err
})

Expand Down
Loading

0 comments on commit 7ca6927

Please sign in to comment.