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

Adds a "local" storage driver that handles all operations in memory #112

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ require (
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
)

require (
github.com/dchest/siphash v1.2.2 // indirect
github.com/dustinxie/lockfree v0.0.0-20210712051436-ed0ed42fd0d6 // indirect
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
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=
github.com/dchest/siphash v1.2.2 h1:9DFz8tQwl9pTVt5iok/9zKyzA1Q6bRGiF3HPiEEVr9I=
github.com/dchest/siphash v1.2.2/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustinxie/lockfree v0.0.0-20210712051436-ed0ed42fd0d6 h1:OCG9DHxQwv2sABVGARZaUh4OK8dVaR3kzTIHV0vW4gg=
github.com/dustinxie/lockfree v0.0.0-20210712051436-ed0ed42fd0d6/go.mod h1:m7oIj8lFrQgKxP9h9m6GxjzGbTuMD5/5yXF8+pTpJms=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -24,6 +28,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
Expand Down
3 changes: 3 additions & 0 deletions temporal/connector/connector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package connector

import (
"github.com/TykTechnologies/storage/temporal/internal/driver/local"
"github.com/TykTechnologies/storage/temporal/internal/driver/redisv9"
"github.com/TykTechnologies/storage/temporal/model"
"github.com/TykTechnologies/storage/temporal/temperr"
Expand All @@ -15,6 +16,8 @@ func NewConnector(connType string, options ...model.Option) (model.Connector, er
switch connType {
case model.RedisV9Type:
return redisv9.NewRedisV9WithOpts(options...)
case model.LocalType:
return local.NewLocalConnector(local.NewLockFreeStore()), nil
default:
return nil, temperr.InvalidHandlerType
}
Expand Down
3 changes: 3 additions & 0 deletions temporal/flusher/flusher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flusher

import (
"github.com/TykTechnologies/storage/temporal/internal/driver/local"
"github.com/TykTechnologies/storage/temporal/internal/driver/redisv9"
"github.com/TykTechnologies/storage/temporal/model"
"github.com/TykTechnologies/storage/temporal/temperr"
Expand All @@ -12,6 +13,8 @@ func NewFlusher(conn model.Connector) (Flusher, error) {
switch conn.Type() {
case model.RedisV9Type:
return redisv9.NewRedisV9WithConnection(conn)
case model.LocalType:
return local.NewLocalStore(conn), nil
default:
return nil, temperr.InvalidHandlerType
}
Expand Down
47 changes: 47 additions & 0 deletions temporal/internal/driver/local/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package local

import (
"context"
"sync"

"github.com/TykTechnologies/storage/temporal/temperr"
)

type LocalConnector struct {
Store KVStore
Broker Broker
mutex sync.RWMutex
connected bool
}

// Disconnect disconnects from the backend
func (api *LocalConnector) Disconnect(context.Context) error {
api.mutex.RLock()
defer api.mutex.RUnlock()
api.connected = false
return nil
}

// Ping executes a ping to the backend
func (api *LocalConnector) Ping(context.Context) error {
if !api.connected {
return temperr.ClosedConnection
}

return nil
}

// Type returns the connector type
func (api *LocalConnector) Type() string {
return "local"
}

// As converts i to driver-specific types.
// Same concept as https://gocloud.dev/concepts/as/ but for connectors.
func (api *LocalConnector) As(i interface{}) bool {
if _, ok := i.(*API); ok {
return true
}

return false
}
34 changes: 34 additions & 0 deletions temporal/internal/driver/local/flusher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package local

import (
"context"
)

func (api *API) FlushAll(ctx context.Context) error {
// save the ops
_, ok := api.Store.Features()[FeatureFlushAll]
if ok {
err := api.Store.FlushAll()
if err != nil {
return err
}

api.initialiseKeyIndexes()
}

keyIndex, err := api.Store.Get(keyIndexKey)
if err != nil {
return err
}

keys := keyIndex.Value.(map[string]interface{})
for key := range keys {
err := api.Delete(ctx, key)
if err != nil {
return err
}
}

api.initialiseKeyIndexes()
return nil
}
Loading
Loading