mirror of
https://github.com/openfaas/faas.git
synced 2025-06-18 03:56:37 +00:00
Add feature: scale from zero to 1 replicas
This change allows functions to be "idled" or scaled to zero replicas and then be invoked later on. There is a penalty to scaling up - the API gateway proxy will block until the function is ready. A cache is included to off-set the calls to upstream API to check on readiness along with unit tests. Testing via scaling to zero replicas and then invoking function. On Swarm I observed 3 seconds on an Intel Nuc i5 for scaling back from zero replicas. Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
committed by
Alex Ellis
parent
31810a4cf2
commit
e67d45caa1
72
gateway/handlers/function_cache_test.go
Normal file
72
gateway/handlers/function_cache_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_LastRefreshSet(t *testing.T) {
|
||||
before := time.Now()
|
||||
|
||||
fnName := "echo"
|
||||
|
||||
cache := FunctionCache{
|
||||
Cache: make(map[string]*FunctionMeta),
|
||||
Expiry: time.Millisecond * 1,
|
||||
}
|
||||
|
||||
if cache.Cache == nil {
|
||||
t.Errorf("Expected cache map to be initialized")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
cache.Set(fnName, 1)
|
||||
|
||||
if _, exists := cache.Cache[fnName]; !exists {
|
||||
t.Errorf("Expected entry to exist after setting %s", fnName)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if cache.Cache[fnName].LastRefresh.Before(before) {
|
||||
t.Errorf("Expected LastRefresh for function to have been after start of test")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_CacheExpiresIn1MS(t *testing.T) {
|
||||
fnName := "echo"
|
||||
|
||||
cache := FunctionCache{
|
||||
Cache: make(map[string]*FunctionMeta),
|
||||
Expiry: time.Millisecond * 1,
|
||||
}
|
||||
|
||||
cache.Set(fnName, 1)
|
||||
time.Sleep(time.Millisecond * 2)
|
||||
|
||||
_, hit := cache.Get(fnName)
|
||||
|
||||
wantHit := false
|
||||
|
||||
if hit != wantHit {
|
||||
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_CacheGivesHitWithLongExpiry(t *testing.T) {
|
||||
fnName := "echo"
|
||||
|
||||
cache := FunctionCache{
|
||||
Cache: make(map[string]*FunctionMeta),
|
||||
Expiry: time.Millisecond * 500,
|
||||
}
|
||||
|
||||
cache.Set(fnName, 1)
|
||||
|
||||
_, hit := cache.Get(fnName)
|
||||
wantHit := true
|
||||
|
||||
if hit != wantHit {
|
||||
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user