-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from bgalehouse/fastpoll
Fastpoll Optimization
- Loading branch information
Showing
13 changed files
with
327 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package cache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/fleetspeak/fleetspeak/src/common" | ||
"github.com/google/fleetspeak/fleetspeak/src/server/db" | ||
) | ||
|
||
var ( | ||
// How long client data should be considered valid for. Variable to support | ||
// unit testing. | ||
MaxAge = 30 * time.Second | ||
|
||
// We occasionally expunge old client data records, to be tidy | ||
// with RAM and prevent what would effectively be a slow memory leak as | ||
// clients come and go. Variable to support unit testing. | ||
expireInterval = 5 * time.Minute | ||
) | ||
|
||
// Clients is a cache of recently connected clients. | ||
type Clients struct { | ||
m map[common.ClientID]*clientEntry | ||
l sync.RWMutex | ||
stop chan struct{} | ||
} | ||
|
||
type clientEntry struct { | ||
u time.Time | ||
d *db.ClientData | ||
} | ||
|
||
// NewClients returns a new cache of client data. | ||
func NewClients() *Clients { | ||
ret := &Clients{ | ||
m: make(map[common.ClientID]*clientEntry), | ||
stop: make(chan struct{}), | ||
} | ||
go ret.expireLoop() | ||
return ret | ||
} | ||
|
||
// Get returns the cached client data, if there is sufficiently fresh data in | ||
// the cache, otherwise nil. | ||
func (c *Clients) Get(id common.ClientID) *db.ClientData { | ||
c.l.RLock() | ||
defer c.l.RUnlock() | ||
|
||
e := c.m[id] | ||
if e == nil || db.Now().Sub(e.u) > MaxAge { | ||
return nil | ||
} | ||
return e.d | ||
} | ||
|
||
// Update updates or sets the cached data for a particular client. If data is | ||
// nil, it clears the data for the client. | ||
func (c *Clients) Update(id common.ClientID, data *db.ClientData) { | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
|
||
if data == nil { | ||
delete(c.m, id) | ||
} else { | ||
c.m[id] = &clientEntry{ | ||
u: db.Now(), | ||
d: data, | ||
} | ||
} | ||
} | ||
|
||
// Clear empties the cache, removing all entries. | ||
func (c *Clients) Clear() { | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
|
||
c.m = make(map[common.ClientID]*clientEntry) | ||
} | ||
|
||
// Stop releases the resources required for background cache maintenence. The | ||
// cache should not be used once Stop has been called. | ||
func (c *Clients) Stop() { | ||
close(c.stop) | ||
} | ||
|
||
// Size returns the current size taken up by the cache, this is a count of | ||
// client records, some of which may no longer be up to date. | ||
func (c *Clients) Size() int { | ||
c.l.RLock() | ||
defer c.l.RUnlock() | ||
return len(c.m) | ||
} | ||
|
||
func (c *Clients) expireLoop() { | ||
t := time.NewTicker(expireInterval) | ||
defer t.Stop() | ||
|
||
for { | ||
select { | ||
case <-t.C: | ||
c.expire() | ||
case <-c.stop: | ||
return | ||
} | ||
} | ||
} | ||
|
||
// expire prunes the cache to clean out clients that are no longer up to date. | ||
func (c *Clients) expire() { | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
|
||
for k, e := range c.m { | ||
if db.Now().Sub(e.u) > MaxAge { | ||
delete(c.m, k) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2017 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/fleetspeak/fleetspeak/src/common" | ||
"github.com/google/fleetspeak/fleetspeak/src/server/db" | ||
"github.com/google/fleetspeak/fleetspeak/src/server/internal/ftime" | ||
) | ||
|
||
func TestClients(t *testing.T) { | ||
oi := expireInterval | ||
expireInterval = time.Second | ||
defer func() { | ||
expireInterval = oi | ||
}() | ||
|
||
// We'd use sertesting.FakeNow, but that creates a dependency loop. So we | ||
// do it directly. | ||
otime := ftime.Now | ||
fakeTime := int64(20000) | ||
ftime.Now = func() time.Time { | ||
return time.Unix(atomic.LoadInt64(&fakeTime), 0).UTC() | ||
} | ||
defer func() { | ||
ftime.Now = otime | ||
}() | ||
|
||
c := NewClients() | ||
defer c.Stop() | ||
|
||
id1, _ := common.StringToClientID("0000000000000001") | ||
id2, _ := common.StringToClientID("0000000000000002") | ||
|
||
for _, id := range []common.ClientID{id1, id2} { | ||
got := c.Get(id) | ||
if got != nil { | ||
t.Errorf("Get(%v) = %v, expected nil", id, got) | ||
} | ||
} | ||
|
||
c.Update(id1, &db.ClientData{ | ||
Key: []byte("key 1"), | ||
}) | ||
|
||
atomic.StoreInt64(&fakeTime, 20029) | ||
|
||
got := c.Get(id1) | ||
if got == nil || !bytes.Equal(got.Key, []byte("key 1")) { | ||
t.Errorf("Get(%v) = %v, expected {Key: \"key 1\"}", id1, got) | ||
} | ||
got = c.Get(id2) | ||
if got != nil { | ||
t.Errorf("Get(%v) = %v, expected nil", id2, got) | ||
} | ||
|
||
// Set key 2, make sure that clearing it works. | ||
c.Update(id2, &db.ClientData{ | ||
Key: []byte("key 2"), | ||
}) | ||
s := c.Size() | ||
if s != 2 { | ||
t.Errorf("Expected cache size of 2, got Size: %d", s) | ||
} | ||
c.Update(id2, nil) | ||
got = c.Get(id2) | ||
if got != nil { | ||
t.Errorf("Get(%v) = %v, expected nil", id2, got) | ||
} | ||
// A second clear should not panic. | ||
c.Update(id2, nil) | ||
|
||
// Advance the clock enough to expire id1, wait for expire to run. | ||
atomic.StoreInt64(&fakeTime, 20031) | ||
time.Sleep(2 * time.Second) | ||
|
||
// Everything should be nil. | ||
for _, id := range []common.ClientID{id1, id2} { | ||
got := c.Get(id) | ||
if got != nil { | ||
t.Errorf("Get(%v) = %v, expected nil", id, got) | ||
} | ||
} | ||
|
||
// We shouldn't be using any extra ram. | ||
s = c.Size() | ||
if s != 0 { | ||
t.Errorf("Expected empty cache, got Size: %d", s) | ||
} | ||
} |
Oops, something went wrong.