From 6515e3e5bd8d3053726702355377a174be0d78f2 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 1 May 2019 02:10:11 +0300 Subject: [PATCH] Resolves #5. Return Redis connection to the pool after commands are executed --- CHANGELOG.md | 1 + db/redis.go | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b177718..fd819b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - `/textures` request no longer proxies request to Mojang in a case when there is no information about the skin, but there is a cape. +- [#5](https://github.com/elyby/chrly/issues/5): Return Redis connection to the pool after commands are executed ### Removed - `hash` field from `/textures` response because the game doesn't use it and calculates hash by getting the filename diff --git a/db/redis.go b/db/redis.go index 0a8253b..977c134 100644 --- a/db/redis.go +++ b/db/redis.go @@ -27,8 +27,6 @@ type RedisFactory struct { connection *pool.Pool } -// TODO: maybe we should manually return connection to the pool? - // TODO: Why isn't a pointer used here? func (f RedisFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error) { return f.createInstance() @@ -102,38 +100,53 @@ type redisDb struct { const accountIdToUsernameKey = "hash:username-to-account-id" const mojangUsernameToUuidKey = "hash:mojang-username-to-uuid" -// TODO: return connection to the pool after usage func (db *redisDb) FindByUsername(username string) (*model.Skin, error) { - return findByUsername(username, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return findByUsername(username, conn) } func (db *redisDb) FindByUserId(id int) (*model.Skin, error) { - return findByUserId(id, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return findByUserId(id, conn) } func (db *redisDb) Save(skin *model.Skin) error { - return save(skin, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return save(skin, conn) } func (db *redisDb) RemoveByUserId(id int) error { - return removeByUserId(id, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return removeByUserId(id, conn) } func (db *redisDb) RemoveByUsername(username string) error { - return removeByUsername(username, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return removeByUsername(username, conn) } func (db *redisDb) GetUuid(username string) (string, error) { - return findMojangUuidByUsername(username, db.getConn()) + conn, _ := db.conn.Get() + defer db.conn.Put(conn) + + return findMojangUuidByUsername(username, conn) } func (db *redisDb) StoreUuid(username string, uuid string) { - storeMojangUuid(username, uuid, db.getConn()) -} - -func (db *redisDb) getConn() util.Cmder { conn, _ := db.conn.Get() - return conn + defer db.conn.Put(conn) + + storeMojangUuid(username, uuid, conn) } func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {