Commit 5196a107 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Improve performance of user device login operations

parent d4845414
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p
### Fixed
- Blocking users now works correctly when there was no prior friend relationship in place.
- Correctly assign cursor data in paginated leaderboard records list queries.
- Improve performance of user device login operations.

## [1.2.0] - 2017-11-06
### Added
+32 −4
Original line number Diff line number Diff line
@@ -529,14 +529,42 @@ func (a *authenticationService) loginDevice(authReq *AuthenticateRequest) (strin
	var userID string
	var handle string
	var disabledAt int64
	err := a.db.QueryRow("SELECT u.id, u.handle, u.disabled_at FROM users u, user_device ud WHERE ud.id = $1 AND u.id = ud.user_id",
		deviceID).
		Scan(&userID, &handle, &disabledAt)

	tx, err := a.db.Begin()
	if err != nil {
		a.logger.Error("Could not begin transaction in device login", zap.Error(err))
		return "", "", 0, errorCouldNotLogin, RUNTIME_EXCEPTION
	}
	defer func() {
		if err != nil {
			if e := tx.Rollback(); e != nil {
				a.logger.Error("Could not rollback transaction in device login", zap.Error(e))
			}
		} else {
			if e := tx.Commit(); e != nil {
				a.logger.Error("Could not commit transaction in device login", zap.Error(e))
			}
		}
	}()

	// Look up user ID by device.
	err = tx.QueryRow("SELECT user_id FROM user_device WHERE id = $1", deviceID).Scan(&userID)
	if err != nil {
		if err == sql.ErrNoRows {
			return "", "", 0, errorIDNotFound, USER_NOT_FOUND
		} else {
			a.logger.Error("Could not look up user ID in device login", zap.Error(err))
			return "", "", 0, errorCouldNotLogin, RUNTIME_EXCEPTION
		}
	}

	// Look up user information by ID.
	err = tx.QueryRow("SELECT handle, disabled_at FROM users WHERE id = $1", userID).Scan(&handle, &disabledAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return "", "", 0, errorIDNotFound, USER_NOT_FOUND
		} else {
			a.logger.Warn(errorCouldNotLogin, zap.String("profile", "device"), zap.Error(err))
			a.logger.Error("Could not look up user data in device login", zap.Error(err))
			return "", "", 0, errorCouldNotLogin, RUNTIME_EXCEPTION
		}
	}
+28 −0
Original line number Diff line number Diff line
@@ -27,3 +27,31 @@ local function fail(context, payload)
  error("fail")
end
nk.register_rpc(fail, "client_rpc_fail")

-- Create a leaderboard and insert 15 test records.
-- Expects as input {"leaderboard_id": "<...>"}
local function generate_leaderboard(context, payload)
  local leaderboard_id = nk.json_decode(payload)["leaderboard_id"]
  nk.leaderboard_create(leaderboard_id, "desc")
  for i = 1, 15 do
    nk.leaderboard_submit_set(leaderboard_id, i, nk.uuid_v4())
  end
end
nk.register_rpc(generate_leaderboard, "generate_leaderboard")

-- Generate 15 notifications for the user calling.
local function generate_notifications(context, payload)
  local notifications = {}
  for i = 1, 15 do
    table.insert(notifications, {
      Persistent = true,
      UserId = context.UserId,
      Subject = "test " .. i,
      Content = {
        test = i
      }
    })
  end
  nk.notifications_send_id(notifications)
end
nk.register_rpc(generate_notifications, "generate_notifications")