Resolves #5. Return Redis connection to the pool after commands are executed

This commit is contained in:
ErickSkrauch 2019-05-01 02:10:11 +03:00
parent ed0b9bb040
commit 6515e3e5bd
2 changed files with 28 additions and 14 deletions

View File

@ -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

View File

@ -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) {