Skip to content

Commit

Permalink
add session store unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
adwski committed May 16, 2024
1 parent e0e2b37 commit f2ec549
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 7 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/enescakir/emoji v1.0.0
github.com/go-playground/validator/v10 v10.20.0
github.com/go-redis/redismock/v9 v9.2.0
github.com/go-resty/resty/v2 v2.12.0
github.com/gofrs/uuid/v5 v5.1.0
github.com/golang-jwt/jwt/v5 v5.2.1
Expand All @@ -26,6 +27,7 @@ require (
github.com/labstack/echo-jwt/v4 v4.2.0
github.com/labstack/echo/v4 v4.12.0
github.com/labstack/gommon v0.4.2
github.com/mattn/go-isatty v0.0.20
github.com/minio/minio-go/v7 v7.0.70
github.com/minio/sha256-simd v1.0.1
github.com/redis/go-redis/v9 v9.5.1
Expand Down Expand Up @@ -75,7 +77,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw=
github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0=
github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA=
github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
Expand Down Expand Up @@ -195,6 +197,12 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
Expand Down Expand Up @@ -348,6 +356,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
18 changes: 12 additions & 6 deletions internal/session/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
ristrettoMaxCost = 1 << 30 // maximum cost of cache (1GB).
ristrettoBufferItems = 64 // number of keys per Get buffer.
ristrettoDefaultCost = 1

minTTL = 300 * time.Second
)

var (
Expand Down Expand Up @@ -57,6 +59,13 @@ type Config struct {
}

func NewStore(cfg *Config) (*Store, error) {
ttl := cfg.TTL
if ttl < minTTL {
ttl = minTTL
}
if len(cfg.Name) == 0 {
return nil, fmt.Errorf("store name is required")
}
u, err := url.Parse(cfg.RedisDSN)
if err != nil {
return nil, fmt.Errorf("cannot parse redis DSN: %w", err)
Expand All @@ -65,14 +74,11 @@ func NewStore(cfg *Config) (*Store, error) {
if errDB != nil {
return nil, fmt.Errorf("cannot parse redis DB number: %w", errDB)
}
cache, errRi := ristretto.NewCache(&ristretto.Config{
cache, _ := ristretto.NewCache(&ristretto.Config{ // error will not happen
NumCounters: ristrettoNumCounters,
MaxCost: ristrettoMaxCost, // basically size of cache if cost of each elem is 1
BufferItems: ristrettoBufferItems,
})
if errRi != nil {
return nil, fmt.Errorf("cannot initialize ristretto cache: %w", errRi)
}
pass, _ := u.User.Password() // empty if not ok
r := redis.NewClient(&redis.Options{
Addr: u.Host,
Expand All @@ -88,12 +94,12 @@ func NewStore(cfg *Config) (*Store, error) {
cache: cache,
enc: jsoniter.ConfigCompatibleWithStandardLibrary,
name: []byte(cfg.Name),
ttl: cfg.TTL,
ttl: ttl,

// Local cache ttl is half of redis ttl.
// Because of this after half-time we goto redis and update expiration,
// since we don't want to lose session in redis.
cacheTTL: cfg.TTL / 2, //nolint:mnd // explained above, no need for constant
cacheTTL: ttl / 2, //nolint:mnd // explained above, no need for constant
}, nil
}

Expand Down
Loading

0 comments on commit f2ec549

Please sign in to comment.