Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Fix db error for memory cache (#146)
Browse files Browse the repository at this point in the history
* Fix db error for memory cache

* Update tests & swagger & api
EnoRage authored Aug 3, 2020
1 parent 56f73d9 commit bf61784
Showing 13 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ func SetupTickersAPI(engine *gin.Engine, tickers controllers.TickersController,
engine.GET("v2/market/ticker/:id",
middleware.CacheControl(d, endpoint.GetTickerHandlerV2(tickers)))

engine.GET("v2/market/tickers",
engine.GET("v2/market/tickers/:assets",
middleware.CacheControl(d, endpoint.GetTickersHandlerV2(tickers)))

engine.POST("v1/market/ticker",
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
@@ -125,7 +125,7 @@ func TestSetupTickersAPI(t *testing.T) {
assert.Equal(t, float64(2), givenV2Resp2.Tickers[0].Change24h)
assert.Equal(t, "coinmarketcap", givenV2Resp2.Tickers[0].Provider)

resp4, err := http.Get(server.URL + "/v2/market/tickers?assets=c60_ta")
resp4, err := http.Get(server.URL + "/v2/market/tickers/c60_ta")
assert.Nil(t, err)

body2, err := ioutil.ReadAll(resp4.Body)
4 changes: 2 additions & 2 deletions api/endpoint/tickers.go
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ func PostTickersHandlerV2(controller controllers.TickersController) func(c *gin.
// @Produce json
// @Tags Tickers
// @Param currency query string empty "currency symbol"
// @Param assets query string true "List of asset ids"
// @Param assets path string true "List of asset ids"
// @Success 200 {object} controllers.TickerResponseV2
// @Router /v2/market/tickers [get]
func GetTickersHandlerV2(controller controllers.TickersController) func(c *gin.Context) {
@@ -123,7 +123,7 @@ func GetTickersHandlerV2(controller controllers.TickersController) func(c *gin.C
defer tx.End()

currency := c.DefaultQuery("currency", watchmarket.DefaultCurrency)
assets := c.Query("assets")
assets := c.Param("assets")
if len(assets) == 0 {
c.JSON(http.StatusBadRequest, errorResponse(errors.E("Invalid request payload")))
return
8 changes: 6 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -69,6 +69,8 @@ func init() {
memoryCache = memory.Init()
c = cron.New(cron.WithChain(cron.Recover(cron.DefaultLogger)))
w = worker.Init(m.RatesAPIs, m.TickersAPIs, database, memoryCache, configuration)
} else {
go postgres.FatalWorker(time.Second*10, *database)
}

r := internal.InitRedis(configuration.Storage.Redis)
@@ -78,8 +80,6 @@ func init() {
info = infocontroller.NewController(redisCache, chartsPriority, coinInfoPriority, ratesPriority, tickerPriority, m.ChartsAPIs, configuration)
tickers = tickerscontroller.NewController(database, memoryCache, ratesPriority, tickerPriority, configuration)
engine = internal.InitEngine(configuration.RestAPI.Mode)

go postgres.FatalWorker(time.Second*10, *database)
}

func main() {
@@ -93,6 +93,10 @@ func main() {
c.Start()
}

if memoryCache.GetLenOfSavedItems() <= 0 {
panic("no items in memory cache")
}

if err := internal.InitAPI(engine, tickers, charts, info, configuration); err != nil {
panic(err)
}
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -330,7 +330,7 @@ var doc = `{
"type": "string",
"description": "List of asset ids",
"name": "assets",
"in": "query",
"in": "path",
"required": true
}
],
2 changes: 1 addition & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
@@ -310,7 +310,7 @@
"type": "string",
"description": "List of asset ids",
"name": "assets",
"in": "query",
"in": "path",
"required": true
}
],
2 changes: 1 addition & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -366,7 +366,7 @@ paths:
name: currency
type: string
- description: List of asset ids
in: query
in: path
name: assets
required: true
type: string
1 change: 1 addition & 0 deletions services/cache/cache.go
Original file line number Diff line number Diff line change
@@ -11,5 +11,6 @@ type (
Set(key string, data []byte, ctx context.Context) error
GetWithTime(key string, time int64, ctx context.Context) ([]byte, error)
SetWithTime(key string, data []byte, time int64, ctx context.Context) error
GetLenOfSavedItems() int
}
)
5 changes: 5 additions & 0 deletions services/cache/memory/base.go
Original file line number Diff line number Diff line change
@@ -49,3 +49,8 @@ func (i Instance) SetWithTime(key string, data []byte, time int64, ctx context.C
func (i Instance) GetWithTime(key string, time int64, ctx context.Context) ([]byte, error) {
return nil, nil
}

func (i Instance) GetLenOfSavedItems() int {
items := i.Cache.Items()
return len(items)
}
4 changes: 4 additions & 0 deletions services/cache/redis/base.go
Original file line number Diff line number Diff line change
@@ -48,3 +48,7 @@ func (i Instance) Set(key string, data []byte, ctx context.Context) error {
}
return nil
}

func (i Instance) GetLenOfSavedItems() int {
return 0
}
4 changes: 4 additions & 0 deletions services/controllers/charts/base_test.go
Original file line number Diff line number Diff line change
@@ -201,6 +201,10 @@ func (c cacheMock) SetWithTime(key string, data []byte, time int64, ctx context.
return nil
}

func (c cacheMock) GetLenOfSavedItems() int {
return 0
}

func getChartsMock() chartsMock {
cm := chartsMock{}
return cm
4 changes: 4 additions & 0 deletions services/controllers/info/base_test.go
Original file line number Diff line number Diff line change
@@ -97,6 +97,10 @@ func (c cacheMock) SetWithTime(key string, data []byte, time int64, ctx context.
return nil
}

func (c cacheMock) GetLenOfSavedItems() int {
return 0
}

func getChartsMock() chartsMock {
cm := chartsMock{}
return cm
6 changes: 4 additions & 2 deletions services/worker/memory.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,8 @@ func (w Worker) SaveTickersToMemory() {

allTickers, err := w.db.GetAllTickers(ctx)
if err != nil {
panic(err)
logger.Warn("Failed to get tickers: ", err.Error())
return
}

logger.Info("Memory Cache: got tickers From DB", logger.Params{"len": len(allTickers)})
@@ -51,7 +52,8 @@ func (w Worker) SaveRatesToMemory() {

allRates, err := w.db.GetAllRates(ctx)
if err != nil {
panic(err)
logger.Warn("Failed to get rates: ", err.Error())
return
}

logger.Info("Memory Cache: got rates From DB", logger.Params{"len": len(allRates)})

0 comments on commit bf61784

Please sign in to comment.