Handle cases where expireTime is updated

This commit is contained in:
Anant Prakash 2018-07-25 18:26:14 +05:30
parent 59aa8683f1
commit 58996fb131
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 32 additions and 28 deletions

View file

@ -17,13 +17,10 @@ import (
"time" "time"
) )
var ( var defaultTypingTimeout = 10 * time.Second
userExists = struct{}{} // Value denoting user is present in a userSet.
defaultTypingTimeout = 10 * time.Second
)
// userSet is a map of user IDs. // userSet is a map of user IDs to their time of expiry.
type userSet map[string]struct{} type userSet map[string]time.Time
// TypingCache maintains a list of users typing in each room. // TypingCache maintains a list of users typing in each room.
type TypingCache struct { type TypingCache struct {
@ -57,33 +54,40 @@ func (t *TypingCache) GetTypingUsers(roomID string) (users []string) {
func (t *TypingCache) AddTypingUser(userID, roomID string, expire *time.Time) { func (t *TypingCache) AddTypingUser(userID, roomID string, expire *time.Time) {
expireTime := getExpireTime(expire) expireTime := getExpireTime(expire)
if until := time.Until(expireTime); until > 0 { if until := time.Until(expireTime); until > 0 {
t.addUser(userID, roomID) t.addUser(userID, roomID, expireTime)
t.removeUserAfterDuration(userID, roomID, until) t.removeUserAfterTime(userID, roomID, expireTime)
} }
} }
func (t *TypingCache) addUser(userID, roomID string) { // addUser with mutex lock.
func (t *TypingCache) addUser(userID, roomID string, expireTime time.Time) {
t.Lock() t.Lock()
defer t.Unlock()
if t.data[roomID] == nil { if t.data[roomID] == nil {
t.data[roomID] = make(userSet) t.data[roomID] = make(userSet)
} }
t.data[roomID][userID] = userExists t.data[roomID][userID] = expireTime
t.Unlock()
} }
// Creates a go routine which removes the user after d duration has elapsed. // Creates a go routine which removes the user after expireTime has elapsed,
func (t *TypingCache) removeUserAfterDuration(userID, roomID string, d time.Duration) { // only if the expiration is not updated to a later time in cache.
func (t *TypingCache) removeUserAfterTime(userID, roomID string, expireTime time.Time) {
go func() { go func() {
time.Sleep(d) time.Sleep(time.Until(expireTime))
t.removeUser(userID, roomID) t.removeUserIfExpired(userID, roomID)
}() }()
} }
func (t *TypingCache) removeUser(userID, roomID string) { // removeUserIfExpired with mutex lock.
func (t *TypingCache) removeUserIfExpired(userID, roomID string) {
t.Lock() t.Lock()
defer t.Unlock()
if time.Until(t.data[roomID][userID]) <= 0 {
delete(t.data[roomID], userID) delete(t.data[roomID], userID)
t.Unlock() }
} }
func getExpireTime(expire *time.Time) time.Time { func getExpireTime(expire *time.Time) time.Time {

View file

@ -19,7 +19,7 @@ import (
"time" "time"
) )
const defaultInterval = time.Second const longInterval = time.Hour
func TestTypingCache(t *testing.T) { func TestTypingCache(t *testing.T) {
tCache := NewTypingCache() tCache := NewTypingCache()
@ -35,13 +35,13 @@ func TestTypingCache(t *testing.T) {
testGetTypingUsers(t, tCache) testGetTypingUsers(t, tCache)
}) })
t.Run("GetTypingUsersAfterTimeout", func(t *testing.T) { t.Run("removeUserIfExpired", func(t *testing.T) {
testGetTypingUsersAfterTimeout(t, tCache) testRemoveUserIfExpired(t, tCache)
}) })
} }
func testAddTypingUser(t *testing.T, tCache *TypingCache) { func testAddTypingUser(t *testing.T, tCache *TypingCache) {
timeAfterDefaultInterval := time.Now().Add(defaultInterval) timeAfterLongInterval := time.Now().Add(longInterval)
tests := []struct { tests := []struct {
userID string userID string
roomID string roomID string
@ -51,9 +51,8 @@ func testAddTypingUser(t *testing.T, tCache *TypingCache) {
{"user2", "room1", nil}, {"user2", "room1", nil},
{"user3", "room1", nil}, {"user3", "room1", nil},
{"user4", "room1", nil}, {"user4", "room1", nil},
// Override timeout // removeUserIfExpired should not remove the user before expiration time.
{"user1", "room2", &timeAfterDefaultInterval}, {"user1", "room2", &timeAfterLongInterval},
{"user1", "room2", nil},
} }
for _, tt := range tests { for _, tt := range tests {
@ -80,16 +79,17 @@ func testGetTypingUsers(t *testing.T, tCache *TypingCache) {
} }
} }
func testGetTypingUsersAfterTimeout(t *testing.T, tCache *TypingCache) { func testRemoveUserIfExpired(t *testing.T, tCache *TypingCache) {
time.Sleep(defaultInterval)
tests := []struct { tests := []struct {
roomID string roomID string
userID string
wantUsers []string wantUsers []string
}{ }{
{"room2", []string{"user1"}}, {"room2", "user1", []string{"user1"}},
} }
for _, tt := range tests { for _, tt := range tests {
tCache.removeUserIfExpired(tt.userID, tt.roomID)
if gotUsers := tCache.GetTypingUsers(tt.roomID); !reflect.DeepEqual(gotUsers, tt.wantUsers) { if gotUsers := tCache.GetTypingUsers(tt.roomID); !reflect.DeepEqual(gotUsers, tt.wantUsers) {
t.Errorf("TypingCache.GetTypingUsers(%s) = %v, want %v", tt.roomID, gotUsers, tt.wantUsers) t.Errorf("TypingCache.GetTypingUsers(%s) = %v, want %v", tt.roomID, gotUsers, tt.wantUsers)
} }