From 4fa1b3faf4485f0b0723aedea1568f9ae4e9a59e Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 10 Jan 2020 16:43:48 -0800 Subject: [PATCH] Don't join a game that's been idle for more than two weeks The two weeks is configurable. But the idea is somebody creates a game with a random name but it's never joined. Eventually the name is used again and the first to connect joins the existing game that will never progress. I could just prune from the DB, but this is less work ongoing. --- xwords4/relay/crefmgr.cpp | 6 +++--- xwords4/relay/dbmgr.cpp | 17 ++++++++++++----- xwords4/relay/dbmgr.h | 8 +++++--- xwords4/relay/xwrelay.conf_tmplate | 8 ++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index c3d76202e..12fbbfd0b 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -254,9 +254,9 @@ CRefMgr::getMakeCookieRef( const char* cookie, int nPlayersH, int nPlayersT, wantsPublic, connNameBuf, sizeof(connNameBuf), &alreadyHere, &cid ); if ( !*seenSeed ) { - cid = m_db->FindOpen( cookie, langCode, nPlayersT, nPlayersH, - wantsPublic, connNameBuf, sizeof(connNameBuf), - &alreadyHere ); + cid = m_db->FindRecentOpen( cookie, langCode, nPlayersT, nPlayersH, + wantsPublic, connNameBuf, sizeof(connNameBuf), + &alreadyHere ); } if ( cid > 0 ) { diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 4c7252202..936a932ea 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -66,6 +66,12 @@ DBMgr::DBMgr() m_useB64 = tmp != 0; logf( XW_LOGINFO, "%s: m_useB64=%d", __func__, m_useB64 ); + if ( !RelayConfigs::GetConfigs()->GetValueFor( "NEWGAME_INTERVAL", + m_interval, + sizeof(m_interval) ) ) { + assert(0); + } + pthread_key_create( &m_conn_key, destr_function ); pthread_mutex_init( &m_haveNoMessagesMutex, NULL ); @@ -330,20 +336,21 @@ DBMgr::SeenSeed( const char* cookie, unsigned short seed, } CookieID -DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH, - bool wantsPublic, char* connNameBuf, int bufLen, - int* nPlayersHP ) +DBMgr::FindRecentOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH, + bool wantsPublic, char* connNameBuf, int bufLen, + int* nPlayersHP ) { QueryBuilder qb; qb.appendQueryf("SELECT cid, connName, sum_array(nPerDevice) FROM " GAMES_TABLE " WHERE NOT dead" + " AND now() - interval '%s' < ANY(mtimes) " " AND room ILIKE $$" " AND lang = $$" " AND nTotal = $$" " AND $$ <= nTotal-sum_array(nPerDevice)" " AND $$ = pub" - " LIMIT 1") + " LIMIT 1", m_interval ) .appendParam(cookie) .appendParam(lang) .appendParam(nPlayersT) @@ -366,7 +373,7 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH, PQclear( result ); logf( XW_LOGINFO, "%s=>%d", __func__, cid ); return cid; -} /* FindOpen */ +} /* FindRecentOpen */ bool DBMgr::AllDevsAckd( const char* const connName ) diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 0d1a40e50..42471b5ff 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -93,9 +93,9 @@ class DBMgr { char* connNameBuf, int bufLen, int* nPlayersHP, CookieID* cid ); - CookieID FindOpen( const char* cookie, int lang, int nPlayersT, - int nPlayersH, bool wantsPublic, - char* connNameBuf, int bufLen, int* nPlayersHP ); + CookieID FindRecentOpen( const char* cookie, int lang, int nPlayersT, + int nPlayersH, bool wantsPublic, + char* connNameBuf, int bufLen, int* nPlayersHP ); bool AllDevsAckd( const char* const connName ); DevIDRelay RegisterDevice( const DevID* host ); @@ -196,6 +196,8 @@ class DBMgr { pthread_key_t m_conn_key; bool m_useB64; + char m_interval[64]; + pthread_mutex_t m_haveNoMessagesMutex; set m_haveNoMessagesDevID; set m_haveNoMessagesConnname; diff --git a/xwords4/relay/xwrelay.conf_tmplate b/xwords4/relay/xwrelay.conf_tmplate index fce4469fd..0074575ba 100644 --- a/xwords4/relay/xwrelay.conf_tmplate +++ b/xwords4/relay/xwrelay.conf_tmplate @@ -76,6 +76,14 @@ DB_PORT=5432 LOGLEVEL=0 LOGFILE_PATH=./xwrelay.log +# How long after an unjoined game has last been heard from will we +# refuse to add another device to it? The problem I'm addressing is +# that with 16-bit ints being used as room names on Android sometimes +# a new game si generated with the same room as a game whose creating +# device has been in a landfill for years. Passed into a sql interval +# statement, e.g. WHERE ctime >= now() - interval '2 weeks' +NEWGAME_INTERVAL=14 days + # Delay sending packets so devices/emulators on the same machine as # relay have a bit more natural experience # SEND_DELAY_MILLIS=500