Skip to content

Commit

Permalink
feat(chore): adapt to storages usage of protobuf
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
  • Loading branch information
mohammed90 committed Jul 21, 2024
1 parent b0a36db commit 32d57e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/darkweak/souin

go 1.22.1

replace github.com/darkweak/storages/core => github.com/mohammed90/storages/core v0.0.0-20240721061337-aa1a8e19b6a0

require (
github.com/caddyserver/caddy/v2 v2.8.4
github.com/cespare/xxhash/v2 v2.2.0
Expand All @@ -13,6 +15,7 @@ require (
github.com/prometheus/client_model v0.5.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
)

Expand Down Expand Up @@ -109,7 +112,6 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
google.golang.org/protobuf v1.34.1 // indirect
howett.net/plist v1.0.0 // indirect
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns=
github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654=
github.com/darkweak/storages/core v0.0.4 h1:rpFTiN+CHwdlXThS28mRpvsSfuMVqWcOe/xtC5A2AQg=
github.com/darkweak/storages/core v0.0.4/go.mod h1:wLp1cOAB4WUd46BBOtV4Lwot4GD+8fZbtIw6QM7fYuc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
Expand Down Expand Up @@ -293,6 +291,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mohammed90/storages/core v0.0.0-20240721061337-aa1a8e19b6a0 h1:3+/Al2dNDNZtlJbSkaFB2AOyezJRA+SbNVEwnFLS2Mg=
github.com/mohammed90/storages/core v0.0.0-20240721061337-aa1a8e19b6a0/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
Expand Down Expand Up @@ -576,8 +576,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
27 changes: 14 additions & 13 deletions pkg/api/souin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package api

import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -14,6 +12,8 @@ import (
"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/souin/pkg/surrogate/providers"
"github.com/darkweak/storages/core"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

// SouinAPI object contains informations related to the endpoints
Expand Down Expand Up @@ -71,8 +71,9 @@ func (s *SouinAPI) BulkDelete(key string, purge bool) {
for _, current := range s.storers {
if b := current.Get(core.MappingKeyPrefix + key); len(b) > 0 {
var mapping core.StorageMapper
if e := gob.NewDecoder(bytes.NewBuffer(b)).Decode(&mapping); e == nil {
for k := range mapping.Mapping {

if e := proto.Unmarshal(b, &mapping); e == nil {

Check failure on line 75 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

cannot use &mapping (value of type *core.StorageMapper) as protoreflect.ProtoMessage value in argument to proto.Unmarshal: *core.StorageMapper does not implement protoreflect.ProtoMessage (missing method ProtoReflect)
for k := range mapping.GetMapping() {

Check failure on line 76 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

mapping.GetMapping undefined (type core.StorageMapper has no field or method GetMapping)
current.Delete(k)
}
}
Expand All @@ -82,7 +83,7 @@ func (s *SouinAPI) BulkDelete(key string, purge bool) {
} else {
newFreshTime := time.Now()
for k, v := range mapping.Mapping {
v.FreshTime = newFreshTime
v.FreshTime = timestamppb.New(newFreshTime)

Check failure on line 86 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

cannot use timestamppb.New(newFreshTime) (value of type *timestamppb.Timestamp) as time.Time value in assignment
mapping.Mapping[k] = v
}
}
Expand Down Expand Up @@ -156,29 +157,29 @@ func (s *SouinAPI) purgeMapping() {
infiniteStoreDuration := storageToInfiniteTTLMap[current.Name()]
values := current.MapKeys(core.MappingKeyPrefix)
for k, v := range values {
var mapping core.StorageMapper
e := gob.NewDecoder(bytes.NewBuffer([]byte(v))).Decode(&mapping)
var mapping *core.StorageMapper

e := proto.Unmarshal([]byte(v), mapping)

Check failure on line 162 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

cannot use mapping (variable of type *core.StorageMapper) as protoreflect.ProtoMessage value in argument to proto.Unmarshal: *core.StorageMapper does not implement protoreflect.ProtoMessage (missing method ProtoReflect)
if e != nil {
current.Delete(core.MappingKeyPrefix + k)
continue
}

updated := false
for key, val := range mapping.Mapping {
if now.Sub(val.FreshTime) > 0 && now.Sub(val.StaleTime) > 0 {
delete(mapping.Mapping, key)
for key, val := range mapping.GetMapping() {

Check failure on line 169 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

mapping.GetMapping undefined (type *core.StorageMapper has no field or method GetMapping)
if now.Sub(val.FreshTime.AsTime()) > 0 && now.Sub(val.StaleTime.AsTime()) > 0 {
delete(mapping.GetMapping(), key)

Check failure on line 171 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

mapping.GetMapping undefined (type *core.StorageMapper has no field or method GetMapping)
updated = true
}
}

if updated {
buf := new(bytes.Buffer)
e = gob.NewEncoder(buf).Encode(mapping)
v, e := proto.Marshal(mapping)

Check failure on line 177 in pkg/api/souin.go

View workflow job for this annotation

GitHub Actions / Caddy

cannot use mapping (variable of type *core.StorageMapper) as protoreflect.ProtoMessage value in argument to proto.Marshal: *core.StorageMapper does not implement protoreflect.ProtoMessage (missing method ProtoReflect)
if e != nil {
fmt.Println("Impossible to re-encode the mapping", core.MappingKeyPrefix+k)
current.Delete(core.MappingKeyPrefix + k)
}
_ = current.Set(core.MappingKeyPrefix+k, buf.Bytes(), infiniteStoreDuration)
_ = current.Set(core.MappingKeyPrefix+k, v, infiniteStoreDuration)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/defaultProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func (provider *Default) MapKeys(prefix string) map[string]string {

return true
}
if v, ok := value.(core.StorageMapper); ok {
if v, ok := value.(*core.StorageMapper); ok {
for _, v := range v.Mapping {
if v.StaleTime.After(now) {
if v.StaleTime.AsTime().After(now) {

Check failure on line 62 in pkg/storage/defaultProvider.go

View workflow job for this annotation

GitHub Actions / Caddy

v.StaleTime.AsTime undefined (type time.Time has no field or method AsTime)
keys[v.RealKey] = string(provider.Get(v.RealKey))
}
}
Expand All @@ -82,7 +82,7 @@ func (provider *Default) ListKeys() []string {
mapping, err := core.DecodeMapping(value.([]byte))
if err == nil {
for _, v := range mapping.Mapping {
if v.StaleTime.After(now) {
if v.StaleTime.AsTime().After(now) {

Check failure on line 85 in pkg/storage/defaultProvider.go

View workflow job for this annotation

GitHub Actions / Caddy

v.StaleTime.AsTime undefined (type time.Time has no field or method AsTime)
keys = append(keys, v.RealKey)
} else {
provider.m.Delete(v.RealKey)
Expand Down

0 comments on commit 32d57e3

Please sign in to comment.