UltrasonicNXT has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/141247

Change subject: Use dynamic polling interval
......................................................................

Use dynamic polling interval

Should make chat more responsive (by polling more frequently during
conversations, but save server resources (by polling less frequently when
there is not much going on).

Change-Id: If531a5693503b9db42ed5c0e640c359691712764
---
M GetNew.api.php
M MediaWikiChat.js
M MediaWikiChat.php
M MediaWikiChatClass.php
4 files changed, 35 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MediaWikiChat 
refs/changes/47/141247/1

diff --git a/GetNew.api.php b/GetNew.api.php
index 8ca7bae..91ae778 100644
--- a/GetNew.api.php
+++ b/GetNew.api.php
@@ -150,6 +150,8 @@
 
                        $result->addValue( $mName, 'now', MediaWikiChat::now() 
);
 
+                       $result->addValue( $mName, 'interval', 
MediaWikiChat::getInterval() );
+
                        if ( !$user->isAllowed( 'chat' ) ) {
                                $result->addValue( $mName, 'kick', true ); // 
if user has since been blocked from chat, kick them now
                        }
diff --git a/MediaWikiChat.js b/MediaWikiChat.js
index 8ae5098..343d41b 100644
--- a/MediaWikiChat.js
+++ b/MediaWikiChat.js
@@ -5,7 +5,7 @@
        amI: false,
        firstTime: true,
        interval: 7000,
-       newInterval: null,
+       pollInterval: null,
        redoInterval: null,
        userData: [],
        focussed: true,
@@ -199,8 +199,12 @@
                        if ( data.kick ) {
                                $( '#mwchat-type input' ).attr( 'disabled', 
'disabled' );
                                $( '#mwchat-users div input' ).attr( 
'disabled', 'disabled' );
-                               clearInterval( MediaWikiChat.newInterval );
+                               clearInterval( MediaWikiChat.pollInterval );
                                MediaWikiChat.getNew();
+                       }
+
+                       if ( data.interval ) {
+                               MediaWikiChat.restartInterval( data.interval );
                        }
 
                        if ( data.messages || data.kicks || data.blocks || 
data.unblocks ) {
@@ -532,8 +536,7 @@
                                data: { 'action': 'chatsendpm', 'message': $( 
this )[0].value, 'id': toid, 'format': 'json' }
                        } ).done( function() {
                                MediaWikiChat.getNew();
-                               window.clearInterval( MediaWikiChat.newInterval 
);
-                               MediaWikiChat.newInterval = setInterval( 
MediaWikiChat.getNew, MediaWikiChat.interval );
+                               MediaWikiChat.restartInterval();
                        } );
 
                        $( this ).val( '' );
@@ -616,6 +619,14 @@
 
                audio.play();
        },
+
+       restartInterval: function( interval ) {
+               if ( !interval ) {
+                       interval = MediaWikiChat.interval;
+               }
+               window.clearInterval( MediaWikiChat.pollInterval );
+               MediaWikiChat.pollInterval = setInterval( MediaWikiChat.getNew, 
interval );
+       }
 };
 
 $( document ).ready( function() {
@@ -660,6 +671,11 @@
                                }
                        }
                        return false;
+               } else {
+                       if ( message.length == 1 ) {
+                               MediaWikiChat.getNew(); // if the user is 
typing a new message, load replies so they can see any
+                               MediaWikiChat.restartInterval(); // before they 
press enter
+                       }
                }
        } );
 
@@ -667,8 +683,8 @@
 
        setTimeout( MediaWikiChat.getNew, 2500 );
 
-       MediaWikiChat.newInterval = setInterval( MediaWikiChat.getNew, 
MediaWikiChat.interval );
-       MediaWikiChat.redoInterval = setInterval( MediaWikiChat.redoTimestamps, 
MediaWikiChat.interval / 2 );
+       MediaWikiChat.pollInterval = setInterval( MediaWikiChat.getNew, 
MediaWikiChat.interval );
+       MediaWikiChat.redoInterval = setInterval( MediaWikiChat.redoTimestamps, 
10000 );
 
        $( '#mwchat-content' ).click( MediaWikiChat.clearMentions );
 
diff --git a/MediaWikiChat.php b/MediaWikiChat.php
index f66c23e..e312dd2 100644
--- a/MediaWikiChat.php
+++ b/MediaWikiChat.php
@@ -16,7 +16,7 @@
 $wgExtensionCredits['specialpage'][] = array(
        'path' => __FILE__,
        'name' => 'MediaWikiChat',
-       'version' => '2.11.2',
+       'version' => '2.12.0',
        'author' => 'Adam Carter/UltrasonicNXT',
        'url' => 'https://www.mediawiki.org/wiki/Extension:MediaWikiChat',
        'descriptionmsg' => 'chat-desc',
diff --git a/MediaWikiChatClass.php b/MediaWikiChatClass.php
index ec76bc0..06faa9e 100644
--- a/MediaWikiChatClass.php
+++ b/MediaWikiChatClass.php
@@ -129,12 +129,14 @@
        }
 
        /**
-        * Get average milliseconds beteen recent messages. Note: not currently 
in use
+        * Get interval to poll the server from. Based on the average 
milliseconds beteen recent messages.
         *
         * @return Integer: average milliseconds between message sends
         */
        static function getInterval() {
                $dbr = wfGetDB( DB_SLAVE );
+               $maxInterval = 30 * 1000;
+               $minInterval = 5 * 1000;
 
                $res = $dbr->select(
                        'chat',
@@ -161,7 +163,13 @@
                $latestTime = $latest->chat_timestamp;
                $oldestTime = $oldest->chat_timestamp;
 
-               $av = ( $latestTime - $oldestTime ) / 5;
+               $av = ( $latestTime - $oldestTime ) / 10 ; // divide by 5 to 
find average, then half
+
+               if ( $av > $maxInterval ) {
+                       $av = $maxInterval;
+               } elseif ( $minInterval ) {
+                       $av = $minInterval;
+               }
 
                return $av;
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/141247
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If531a5693503b9db42ed5c0e640c359691712764
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MediaWikiChat
Gerrit-Branch: master
Gerrit-Owner: UltrasonicNXT <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to