Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cache into middleware #17

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
eb4731b
Add Cache-Control header
toshinari123 Jan 2, 2024
8bdb787
Fix set Cache-Control header position
toshinari123 Jan 9, 2024
bb7860b
Remove pageship in .gitignore
toshinari123 Jan 9, 2024
2a0ce74
Add default Cache-Control header
kiootic Jan 9, 2024
cfe4e18
model type ContentCache
toshinari123 Jan 10, 2024
05d22c9
Add OnExit in Ristretto to remove mutex entry
toshinari123 Jan 23, 2024
67f07e0
Use io.Reader in contentcache GetContent
toshinari123 Jan 23, 2024
9007458
Add ContentCache to serveFile
toshinari123 Jan 23, 2024
98cea47
Refactor duplicated logic
toshinari123 Jan 24, 2024
df9cb18
Create basic contentcache test
toshinari123 Jan 24, 2024
9ab6eb9
Switch Read to ReadAll
toshinari123 Jan 29, 2024
1455d6d
Test data race and Use sync.Map
toshinari123 Jan 29, 2024
cf4cff8
Upgrade contentCache implementation
toshinari123 Feb 5, 2024
ddb4c1e
Add keyToString as Ristretto doesnt support complex keys
toshinari123 Feb 5, 2024
479e5c3
Test contentcache
toshinari123 Feb 5, 2024
806c78c
Remove caching in sitehandler
toshinari123 Feb 5, 2024
e6d6cff
Write cache middleware
toshinari123 Feb 5, 2024
ae7afd6
Convert middleware.Default to a closure
toshinari123 Feb 5, 2024
0e5500c
Move middleware.go to inside middleware
toshinari123 Feb 5, 2024
9902f91
Move context.go
toshinari123 Feb 5, 2024
e63547f
Add and create contentCache in handler
toshinari123 Feb 5, 2024
05aee89
Fix typo
toshinari123 Feb 5, 2024
d3272ba
Include encoding in contentcache key
toshinari123 Feb 5, 2024
a419840
Refactor to deduceCompression
toshinari123 Feb 5, 2024
d0ff121
Fix do not debug contentcache in handler
toshinari123 Feb 6, 2024
f9bc061
Add contentcachemaxsize to controller
toshinari123 Feb 19, 2024
65b2c06
Write cache middleware test setup
toshinari123 Feb 5, 2024
b9479f7
Write test act and assert
toshinari123 Feb 6, 2024
073004c
Add contentcachemaxsize to pageship serve
toshinari123 Feb 19, 2024
54aea33
Refactor contentcache and cache middleware
toshinari123 Feb 19, 2024
3de84af
Clean up code
toshinari123 Feb 20, 2024
e1a9b27
Fix import cycle
toshinari123 Feb 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*.test
*.out
go.work

*.local
.env
dist/
/book
/book
12 changes: 8 additions & 4 deletions cmd/controller/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func init() {
startCmd.PersistentFlags().Bool("cron", true, "run cron jobs")
startCmd.PersistentFlags().Bool("sites", true, "run sites server")
startCmd.PersistentFlags().String("controller-domain", "", "controller domain")

startCmd.PersistentFlags().Int64("content-cache-max-size", int64(1)<<24, "maximum size of server-side content cache in bytes, default is 16MB")
}

type StartConfig struct {
Expand All @@ -97,8 +99,9 @@ type StartConfig struct {
}

type StartSitesConfig struct {
HostPattern string `mapstructure:"host-pattern"`
HostIDScheme config.HostIDScheme `mapstructure:"host-id-scheme" validate:"hostidscheme"`
HostPattern string `mapstructure:"host-pattern"`
HostIDScheme config.HostIDScheme `mapstructure:"host-id-scheme" validate:"hostidscheme"`
ContentCacheMaxSize int64 `mapstructure:"content-cache-max-size"`
}

type StartControllerConfig struct {
Expand Down Expand Up @@ -157,8 +160,9 @@ func (s *setup) sites(conf StartSitesConfig) error {
domainResolver,
siteResolver,
site.HandlerConfig{
HostPattern: conf.HostPattern,
Middlewares: middleware.Default,
HostPattern: conf.HostPattern,
MiddlewaresFunc: middleware.Default,
ContentCacheMaxSize: conf.ContentCacheMaxSize,
},
)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions cmd/pageship/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func init() {

serveCmd.PersistentFlags().String("default-site", config.DefaultSite, "default site")
serveCmd.PersistentFlags().String("host-pattern", config.DefaultHostPattern, "host match pattern")

serveCmd.PersistentFlags().Int64("content-cache-max-size", int64(1)<<24, "maximum size of server-side content cache in bytes, default is 16MB")
}

func loadSitesConfig(fsys fs.FS) (*config.SitesConfig, error) {
Expand All @@ -47,7 +49,7 @@ func loadSitesConfig(fsys fs.FS) (*config.SitesConfig, error) {
return conf, nil
}

func makeHandler(prefix string, defaultSite string, hostPattern string) (*handler.Handler, error) {
func makeHandler(prefix string, defaultSite string, hostPattern string, contentCacheMaxSize int64) (*handler.Handler, error) {
dir, err := filepath.Abs(prefix)
if err != nil {
return nil, err
Expand Down Expand Up @@ -90,8 +92,9 @@ func makeHandler(prefix string, defaultSite string, hostPattern string) (*handle
handler, err := handler.NewHandler(context.Background(), zapLogger,
domainResolver, siteResolver,
handler.HandlerConfig{
HostPattern: hostPattern,
Middlewares: middleware.Default,
HostPattern: hostPattern,
MiddlewaresFunc: middleware.Default,
ContentCacheMaxSize: contentCacheMaxSize,
})
if err != nil {
return nil, err
Expand All @@ -115,12 +118,14 @@ var serveCmd = &cobra.Command{
defaultSite := viper.GetString("default-site")
hostPattern := viper.GetString("host-pattern")

contentCacheMaxSize := viper.GetInt64("content-cache-max-size")

dir := "."
if len(args) > 0 {
dir = args[0]
}

handler, err := makeHandler(dir, defaultSite, hostPattern)
handler, err := makeHandler(dir, defaultSite, hostPattern, contentCacheMaxSize)
if err != nil {
return fmt.Errorf("failed to setup server: %w", err)
}
Expand Down
Binary file added cmd/pageship/pageship
Binary file not shown.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/MicahParks/keyfunc/v2 v2.1.0
github.com/caddyserver/certmagic v0.17.2
github.com/carlmjohnson/versioninfo v0.22.4
github.com/dgraph-io/ristretto v0.1.1
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.15.0
github.com/foxcpp/go-mockdns v1.0.0
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.8
github.com/go-playground/validator/v10 v10.14.0
Expand Down Expand Up @@ -70,13 +72,14 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.21.1 // indirect
github.com/aws/smithy-go v1.14.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/foxcpp/go-mockdns v1.0.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down Expand Up @@ -111,6 +114,7 @@ require (
github.com/miekg/dns v1.1.50 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
10 changes: 9 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ github.com/aws/smithy-go v1.14.0 h1:+X90sB94fizKjDmwb4vyl2cTTPXTE5E2G/1mjByb0io=
github.com/aws/smithy-go v1.14.0/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -259,6 +258,8 @@ github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/checkpoint-restore/go-criu/v4 v4.1.0/go.mod h1:xUQBLp4RLc5zJtWY++yjOoMoB5lihDt7fai+75m+rGw=
github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M=
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
Expand Down Expand Up @@ -429,8 +430,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dhui/dktest v0.3.10 h1:0frpeeoM9pHouHjhLeZDuDTJ0PqjDTrycaHaMmkJAo8=
github.com/dhui/dktest v0.3.10/go.mod h1:h5Enh0nG3Qbo9WjNFRrwmKUaePEBhXMOygbz3Ww7Sz0=
Expand Down Expand Up @@ -612,6 +617,8 @@ github.com/golang-migrate/migrate/v4 v4.15.2/go.mod h1:f2toGLkYqD3JH+Todi4aZ2Zdb
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -1697,6 +1704,7 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
Expand Down
46 changes: 46 additions & 0 deletions internal/cache/contentcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cache

import (
"net/http"

"github.com/dgraph-io/ristretto"
)

type ContentCache struct {
size int64
*ristretto.Cache
debug bool
}

type Response struct {
Header http.Header
Body []byte
StatusCode int
}

func NewContentCache(contentCacheSize int64, debug bool) (*ContentCache, error) {
size := contentCacheSize
nc := size / 1000
cache, err := ristretto.NewCache(&ristretto.Config{
//NumCounters is 10 times estimated max number of items in cache, as suggested in https://pkg.go.dev/github.com/dgraph-io/[email protected]#Config
NumCounters: nc, //limit / 10 KB small files * 10
MaxCost: size,
BufferItems: 64,
Metrics: debug,
IgnoreInternalCost: true,
})
if err != nil {
return nil, err
}

return &ContentCache{size, cache, debug}, nil
}

func (c *ContentCache) Get(key string) (*Response, bool) {
r, b := c.Cache.Get(key)
return r.(*Response), b
}

func (c *ContentCache) Set(key string, value *Response) {
c.Cache.Set(key, value, int64(len(value.Body)))
}
130 changes: 130 additions & 0 deletions internal/cache/contentcache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package cache

import (
"fmt"
"net/http"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
fmt.Println("testing Get...")
cc, err := NewContentCache(8, true)
assert.Empty(t, err)

k1 := "id1"
k2 := "id2"
k3 := "id3"

_, found := cc.Get(k1)
assert.Equal(t, false, found)
assert.Equal(t, "hit: 0 miss: 1 keys-added: 0 keys-updated: 0 keys-evicted: 0 cost-added: 0 cost-evicted: 0 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 1 hit-ratio: 0.00", cc.Metrics.String())

resp1 := Response{Header: make(http.Header), StatusCode: 200, Body: []byte("a")}
cc.Set(k1, &resp1)
time.Sleep(100 * time.Millisecond) //https://github.com/dgraph-io/ristretto/issues/161
assert.Equal(t, "hit: 0 miss: 1 keys-added: 1 keys-updated: 0 keys-evicted: 0 cost-added: 1 cost-evicted: 0 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 1 hit-ratio: 0.00", cc.Metrics.String())

ccc, found := cc.Get(k1)
assert.Equal(t, true, found)
assert.Equal(t, &resp1, ccc)
assert.Equal(t, "hit: 1 miss: 1 keys-added: 1 keys-updated: 0 keys-evicted: 0 cost-added: 1 cost-evicted: 0 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 2 hit-ratio: 0.50", cc.Metrics.String())

resp2 := Response{Header: make(http.Header), StatusCode: 200, Body: []byte("test")}
cc.Set(k1, &resp2)
time.Sleep(100 * time.Millisecond)
assert.Equal(t, "hit: 1 miss: 1 keys-added: 1 keys-updated: 1 keys-evicted: 0 cost-added: 4 cost-evicted: 0 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 2 hit-ratio: 0.50", cc.Metrics.String())

ccc, found = cc.Get(k1)
assert.Equal(t, true, found)
assert.Equal(t, &resp2, ccc)
assert.Equal(t, "hit: 2 miss: 1 keys-added: 1 keys-updated: 1 keys-evicted: 0 cost-added: 4 cost-evicted: 0 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 3 hit-ratio: 0.67", cc.Metrics.String())

resp3 := Response{Header: make(http.Header), StatusCode: 200, Body: []byte("overflow")}
cc.Set(k2, &resp3)
time.Sleep(100 * time.Millisecond)
assert.Equal(t, "hit: 2 miss: 1 keys-added: 2 keys-updated: 1 keys-evicted: 1 cost-added: 12 cost-evicted: 4 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 3 hit-ratio: 0.67", cc.Metrics.String())

ccc, found = cc.Get(k2)
assert.Equal(t, true, found)
assert.Equal(t, &resp3, ccc)
assert.Equal(t, "hit: 3 miss: 1 keys-added: 2 keys-updated: 1 keys-evicted: 1 cost-added: 12 cost-evicted: 4 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 4 hit-ratio: 0.75", cc.Metrics.String())

resp4 := Response{Header: make(http.Header), StatusCode: 200, Body: []byte("content too big")}
cc.Set(k3, &resp4)
time.Sleep(100 * time.Millisecond)
assert.Equal(t, "hit: 3 miss: 1 keys-added: 2 keys-updated: 1 keys-evicted: 1 cost-added: 12 cost-evicted: 4 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 4 hit-ratio: 0.75", cc.Metrics.String())

_, found = cc.Get(k3)
assert.Equal(t, false, found)
assert.Equal(t, "hit: 3 miss: 2 keys-added: 2 keys-updated: 1 keys-evicted: 1 cost-added: 12 cost-evicted: 4 sets-dropped: 0 sets-rejected: 0 gets-dropped: 0 gets-kept: 0 gets-total: 5 hit-ratio: 0.60", cc.Metrics.String())
}

func TestDataRace(t *testing.T) {
fmt.Println("testing data race...")
var wg sync.WaitGroup
cc, err := NewContentCache(16, true)
assert.Empty(t, err)
k1 := "id1"
k2 := "id2"
wg.Add(1)
go func() {
defer wg.Done()
cc.Set(k1, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("data")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
cc.Set(k2, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("race")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
_, found := cc.Get(k1)
assert.Equal(t, true, found)
_, found = cc.Get(k2)
assert.Equal(t, true, found)
}()
wg.Add(1)
go func() {
defer wg.Done()
cc.Set(k1, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("race")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
cc.Set(k2, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("data")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
_, found := cc.Get(k1)
assert.Equal(t, true, found)
_, found = cc.Get(k2)
assert.Equal(t, true, found)
}()
wg.Add(1)
go func() {
defer wg.Done()
cc.Set(k2, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("data")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
cc.Set(k1, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("race")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
_, found := cc.Get(k1)
assert.Equal(t, true, found)
_, found = cc.Get(k2)
assert.Equal(t, true, found)
}()
wg.Add(1)
go func() {
defer wg.Done()
cc.Set(k2, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("race")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
cc.Set(k1, &Response{Header: make(http.Header), StatusCode: 200, Body: []byte("data")})
time.Sleep(100 * time.Millisecond)
assert.Empty(t, err)
_, found := cc.Get(k1)
assert.Equal(t, true, found)
_, found = cc.Get(k2)
assert.Equal(t, true, found)
}()
wg.Wait()
}
2 changes: 1 addition & 1 deletion internal/handler/controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Controller) makeAPIApp(app *models.App) *apiApp {
}

func (c *Controller) middlewareLoadApp() func(http.Handler) http.Handler {
return middlwareLoadValue(func(r *http.Request) (*models.App, error) {
return middlewareLoadValue(func(r *http.Request) (*models.App, error) {
id := chi.URLParam(r, "app-id")

app, err := c.DB.GetApp(r.Context(), id)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/controller/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Controller) makeAPIDeployment(app *models.App, d db.DeploymentInfo) *ap
}

func (c *Controller) middlewareLoadDeployment() func(http.Handler) http.Handler {
return middlwareLoadValue(func(r *http.Request) (*models.Deployment, error) {
return middlewareLoadValue(func(r *http.Request) (*models.Deployment, error) {
app := get[*models.App](r)
name := chi.URLParam(r, "deployment-name")

Expand Down
2 changes: 1 addition & 1 deletion internal/handler/controller/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Controller) makeAPISite(app *models.App, site db.SiteInfo) *apiSite {
}

func (c *Controller) middlewareLoadSite() func(http.Handler) http.Handler {
return middlwareLoadValue(func(r *http.Request) (*models.Site, error) {
return middlewareLoadValue(func(r *http.Request) (*models.Site, error) {
app := get[*models.App](r)
name := chi.URLParam(r, "site-name")

Expand Down
2 changes: 1 addition & 1 deletion internal/handler/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func set[T any](r *http.Request, value T) *http.Request {
return r.WithContext(context.WithValue(r.Context(), valueContextKey[T]{}, value))
}

func middlwareLoadValue[T any](load func(r *http.Request) (T, error)) func(next http.Handler) http.Handler {
func middlewareLoadValue[T any](load func(r *http.Request) (T, error)) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v, err := load(r)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/site/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type contextValue struct {
Error error
}

func withSiteContext(ctx context.Context) context.Context {
func WithSiteContext(ctx context.Context) context.Context {
return context.WithValue(ctx, contextKey{}, &contextValue{})
}

Expand Down
Loading