Skip to content

Commit

Permalink
Cache Everything (#215)
Browse files Browse the repository at this point in the history
Enable caching for segments, rules and evaluation
  • Loading branch information
markphelps authored Feb 1, 2020
1 parent 2cb99af commit 86eaa3f
Show file tree
Hide file tree
Showing 48 changed files with 1,994 additions and 487 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Benchmark
on:
pull_request:
branches:
- master
paths-ignore:
- '*.md'
- '.all-contributorsrc'
jobs:

## Benchmarks
test:
name: Benchmark
runs-on: ubuntu-latest

services:
postgres:
image: postgres:11-alpine
ports:
- 5432:5432
env:
POSTGRES_DB: flipt_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ''

steps:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.13.7
id: go

- name: Checkout
uses: actions/checkout@v1

- name: Restore Cache
uses: actions/cache@preview
id: cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ hashFiles('**/go.sum') }}

- name: Benchmark (SQLite)
run: go test -bench=. ./... -run=XXX -v

- name: Benchmark (Postgres)
run: DB_URL="postgres://postgres@localhost:${{ job.services.postgres.ports['5432'] }}/flipt_test?sslmode=disable" go test -bench=. ./... -run=XXX -v
4 changes: 1 addition & 3 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ on:
branches:
- master
paths-ignore:
- 'docs/**'
- '*.md'
- '.all-contributorsrc'
pull_request:
branches:
- master
paths-ignore:
- 'docs/**'
- '*.md'
- '.all-contributorsrc'

Expand All @@ -26,7 +24,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.13.4
go-version: 1.13.7
id: go

- name: Checkout
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Tests
on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
- '.all-contributorsrc'

Expand All @@ -17,7 +16,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.13.4
go-version: 1.13.7
id: go

- name: Check out code into the Go module directory
Expand Down Expand Up @@ -55,7 +54,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.13.4
go-version: 1.13.7
id: go

- name: Checkout
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

* Fixed documentation link in app
* Underlying caching library from golang-lru to go-cache

### Added

* Caching support for segments, rules AND evaluation! [https://github.com/markphelps/flipt/issues/100](https://github.com/markphelps/flipt/issues/100)
* `cache.memory.expiration` configuration option
* `cache.memory.eviction_interval` configuration option

### Removed

* `cache.memory.items` configuration option

## [v0.11.1](https://github.com/markphelps/flipt/releases/tag/v0.11.1) - 2020-01-28

### Changed
Expand Down
18 changes: 9 additions & 9 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/golang-migrate/migrate/database/postgres"
"github.com/golang-migrate/migrate/database/sqlite3"
grpc_gateway "github.com/grpc-ecosystem/grpc-gateway/runtime"
lru "github.com/hashicorp/golang-lru"
"github.com/markphelps/flipt/config"
pb "github.com/markphelps/flipt/rpc"
"github.com/markphelps/flipt/server"
"github.com/markphelps/flipt/storage/cache"
"github.com/markphelps/flipt/storage/db"
_ "github.com/mattn/go-sqlite3"
"github.com/phyber/negroni-gzip/gzip"
Expand Down Expand Up @@ -254,7 +254,7 @@ func run() error {
logger.Debugf("migrations pending: [current version=%d, want version=%d]", v, dbMigrationVersion)

if forceMigrate {
logger.Debugf("force-migrate set; running now")
logger.Info("force-migrate set; running now")
if err := runMigrations(); err != nil {
return fmt.Errorf("running migrations: %w", err)
}
Expand All @@ -279,13 +279,13 @@ func run() error {
)

if cfg.Cache.Memory.Enabled {
cache, err := lru.New(cfg.Cache.Memory.Items)
if err != nil {
return fmt.Errorf("creating in-memory cache: %w", err)
cacher := cache.NewInMemoryCache(cfg.Cache.Memory.Expiration, cfg.Cache.Memory.EvictionInterval, logger)
if cfg.Cache.Memory.Expiration > 0 {
logger.Infof("in-memory cache enabled [expiration: %v, evictionInterval: %v]", cfg.Cache.Memory.Expiration, cfg.Cache.Memory.EvictionInterval)
} else {
logger.Info("in-memory cache enabled with no expiration")
}

logger.Debugf("in-memory cache enabled with size: %d", cfg.Cache.Memory.Items)
serverOpts = append(serverOpts, server.WithCache(cache))
serverOpts = append(serverOpts, server.WithCache(cacher))
}

srv = server.New(logger, builder, sql, serverOpts...)
Expand Down Expand Up @@ -363,7 +363,7 @@ func run() error {
})

r.Use(cors.Handler)
logger.Debugf("CORS enabled with allowed origins: %v", cfg.Cors.AllowedOrigins)
logger.Infof("CORS enabled with allowed origins: %v", cfg.Cors.AllowedOrigins)
}

r.Use(middleware.RequestID)
Expand Down
23 changes: 15 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -35,8 +36,9 @@ type corsConfig struct {
}

type memoryCacheConfig struct {
Enabled bool `json:"enabled"`
Items int `json:"items,omitempty"`
Enabled bool `json:"enabled"`
Expiration time.Duration `json:"expiration"`
EvictionInterval time.Duration `json:"evictionInterval"`
}

type cacheConfig struct {
Expand Down Expand Up @@ -98,8 +100,9 @@ func Default() *Config {

Cache: cacheConfig{
Memory: memoryCacheConfig{
Enabled: false,
Items: 500,
Enabled: false,
Expiration: -1,
EvictionInterval: 10 * time.Minute,
},
},

Expand Down Expand Up @@ -131,8 +134,9 @@ const (
cfgCorsAllowedOrigins = "cors.allowed_origins"

// Cache
cfgCacheMemoryEnabled = "cache.memory.enabled"
cfgCacheMemoryItems = "cache.memory.items"
cfgCacheMemoryEnabled = "cache.memory.enabled"
cfgCacheMemoryExpiration = "cache.memory.expiration"
cfgCacheMemoryEvictionInterval = "cache.memory.eviction_interval"

// Server
cfgServerHost = "server.host"
Expand Down Expand Up @@ -188,8 +192,11 @@ func Load(path string) (*Config, error) {
if viper.IsSet(cfgCacheMemoryEnabled) {
cfg.Cache.Memory.Enabled = viper.GetBool(cfgCacheMemoryEnabled)

if viper.IsSet(cfgCacheMemoryItems) {
cfg.Cache.Memory.Items = viper.GetInt(cfgCacheMemoryItems)
if viper.IsSet(cfgCacheMemoryExpiration) {
cfg.Cache.Memory.Expiration = viper.GetDuration(cfgCacheMemoryExpiration)
}
if viper.IsSet(cfgCacheMemoryEvictionInterval) {
cfg.Cache.Memory.EvictionInterval = viper.GetDuration(cfgCacheMemoryEvictionInterval)
}
}

Expand Down
11 changes: 9 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -52,6 +53,11 @@ func TestLoad(t *testing.T) {
path: "./testdata/config/default.yml",
expected: Default(),
},
{
name: "deprecated defaults",
path: "./testdata/config/deprecated.yml",
expected: Default(),
},
{
name: "configured",
path: "./testdata/config/advanced.yml",
Expand All @@ -69,8 +75,9 @@ func TestLoad(t *testing.T) {
},
Cache: cacheConfig{
Memory: memoryCacheConfig{
Enabled: true,
Items: 5000,
Enabled: true,
Expiration: 5 * time.Minute,
EvictionInterval: 1 * time.Minute,
},
},
Server: serverConfig{
Expand Down
3 changes: 2 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# cache:
# memory:
# enabled: false
# items: 500
# expiration: -1 # Items Do Not Expire
# eviction_interval: 10m # Evict Expired Items Every 10m

# server:
# protocol: http
Expand Down
3 changes: 2 additions & 1 deletion config/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ log:
# cache:
# memory:
# enabled: false
# items: 500
# expiration: -1 # Items Do Not Expire
# eviction_interval: 10m # Evict Expired Items Every 10m

# server:
# protocol: http
Expand Down
3 changes: 2 additions & 1 deletion config/testdata/config/advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ cors:
cache:
memory:
enabled: true
items: 5000
expiration: 5m
eviction_interval: 1m

server:
protocol: https
Expand Down
3 changes: 2 additions & 1 deletion config/testdata/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# cache:
# memory:
# enabled: false
# items: 500
# expiration: -1 # Items Do Not Expire
# eviction_interval: 10m # Evict Expired Items Every 10m

# server:
# protocol: http
Expand Down
4 changes: 4 additions & 0 deletions config/testdata/config/deprecated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cache:
memory:
enabled: false
items: 500
5 changes: 5 additions & 0 deletions examples/prometheus/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ services:
- "8080:8080"
networks:
- flipt_network
environment:
- "FLIPT_LOG_LEVEL=debug"
- "FLIPT_CACHE_MEMORY_ENABLED=true"
- "FLIPT_CACHE_MEMORY_EXPIRATION=1m"
- "FLIPT_CACHE_MEMORY_EVICTION_INTERVAL=2m"
networks:
flipt_network:
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.12.2

github.com/hashicorp/golang-lru v0.5.4
github.com/lib/pq v1.3.0
github.com/markphelps/flipt-grpc-go v0.2.0
github.com/mattn/go-sqlite3 v1.13.0
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/phyber/negroni-gzip v0.0.0-20180113114010-ef6356a5d029
github.com/prometheus/client_golang v1.3.0
github.com/rogpeppe/go-internal v1.5.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.12.1 h1:zCy2xE9ablevUOrUZc3Dl72Dt+ya2F
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.12.2 h1:D0EVSTwQoQOyfY35QNSuPJA4jpZRtkoGYWQMB7XNg5o=
github.com/grpc-ecosystem/grpc-gateway v1.12.2/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
Expand Down Expand Up @@ -277,6 +275,8 @@ github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
2 changes: 1 addition & 1 deletion script/build/release
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -euo pipefail

GORELEASER_VERSION=v0.120.3
GORELEASER_VERSION=v0.125.0

cd "$(dirname "$0")/../.." || exit

Expand Down
2 changes: 1 addition & 1 deletion script/build/snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -euo pipefail

GORELEASER_VERSION=v0.120.3
GORELEASER_VERSION=v0.125.0

cd "$(dirname "$0")/../.." || exit

Expand Down
Loading

0 comments on commit 86eaa3f

Please sign in to comment.