Skip to content

Commit

Permalink
storage: a couple of redis cleanups
Browse files Browse the repository at this point in the history
First, don't make all the methods use pointer receivers. None of them
modify the struct, and the struct itself is tiny - just a string and two
bools, which should total three ints. This should mean a slight memory
and performance win, as we don't have to allocate as many objects and we
don't have to jump around memory as much.

Second, refactor GetRelevantClusterReference into a method with a
shorter name, making the code more understandable and simpler.

While replacing RedisCluster pointers with structs, I also noticed how
RedisNotificationHandler could discard notifications if the store wasn't
initialised. Instead, synchronously set up the store, so we know it has
been set up. This is not a problem, given that Connect doesn't actually
block connecting to all the hosts.
  • Loading branch information
mvdan committed Oct 10, 2017
1 parent 747c405 commit 8e7743a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 112 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ func invalidateCacheHandler(w http.ResponseWriter, r *http.Request) {

keyPrefix := "cache-" + apiID
matchPattern := keyPrefix + "*"
store := &storage.RedisCluster{KeyPrefix: keyPrefix, IsCache: true}
store := storage.RedisCluster{KeyPrefix: keyPrefix, IsCache: true}

if ok := store.DeleteScanMatch(matchPattern); !ok {
err := errors.New("scan/delete failed")
Expand Down
8 changes: 4 additions & 4 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ type ChainObject struct {
Subrouter *mux.Router
}

func prepareStorage() (*storage.RedisCluster, *storage.RedisCluster, *storage.RedisCluster, *RPCStorageHandler, *RPCStorageHandler) {
func prepareStorage() (storage.RedisCluster, storage.RedisCluster, storage.RedisCluster, *RPCStorageHandler, *RPCStorageHandler) {
redisStore := storage.RedisCluster{KeyPrefix: "apikey-", HashKeys: config.Global.HashKeys}
redisOrgStore := storage.RedisCluster{KeyPrefix: "orgkey."}
healthStore := &storage.RedisCluster{KeyPrefix: "apihealth."}
healthStore := storage.RedisCluster{KeyPrefix: "apihealth."}
rpcAuthStore := RPCStorageHandler{KeyPrefix: "apikey-", HashKeys: config.Global.HashKeys, UserKey: config.Global.SlaveOptions.APIKey, Address: config.Global.SlaveOptions.ConnectionString}
rpcOrgStore := RPCStorageHandler{KeyPrefix: "orgkey.", UserKey: config.Global.SlaveOptions.APIKey, Address: config.Global.SlaveOptions.ConnectionString}

FallbackKeySesionManager.Init(&redisStore)

return &redisStore, &redisOrgStore, healthStore, &rpcAuthStore, &rpcOrgStore
return redisStore, redisOrgStore, healthStore, &rpcAuthStore, &rpcOrgStore
}

func skipSpecBecauseInvalid(spec *APISpec) bool {
Expand Down Expand Up @@ -261,7 +261,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
}

keyPrefix := "cache-" + spec.APIID
cacheStore := &storage.RedisCluster{KeyPrefix: keyPrefix, IsCache: true}
cacheStore := storage.RedisCluster{KeyPrefix: keyPrefix, IsCache: true}
cacheStore.Connect()

var chain http.Handler
Expand Down
4 changes: 2 additions & 2 deletions coprocess_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TykStoreData(CKey, CValue *C.char, CTTL C.int) {
value := C.GoString(CValue)
ttl := int64(CTTL)

store := &storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
store.SetKey(key, value, ttl)
}

Expand All @@ -43,7 +43,7 @@ func TykStoreData(CKey, CValue *C.char, CTTL C.int) {
func TykGetData(CKey *C.char) *C.char {
key := C.GoString(CKey)

store := &storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
// TODO: return error
val, _ := store.GetKey(key)
return C.CString(val)
Expand Down
2 changes: 1 addition & 1 deletion event_handler_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (w *WebHookHandler) Init(handlerConf interface{}) error {
return err
}

w.store = &storage.RedisCluster{KeyPrefix: "webhook.cache."}
w.store = storage.RedisCluster{KeyPrefix: "webhook.cache."}
w.store.Connect()

// Pre-load template on init
Expand Down
6 changes: 3 additions & 3 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ const extendedPathGatewaySetup = `{
func createSpecTest(t *testing.T, def string) *APISpec {
spec := createDefinitionFromString(def)
tname := t.Name()
redisStore := &storage.RedisCluster{KeyPrefix: tname + "-apikey."}
healthStore := &storage.RedisCluster{KeyPrefix: tname + "-apihealth."}
orgStore := &storage.RedisCluster{KeyPrefix: tname + "-orgKey."}
redisStore := storage.RedisCluster{KeyPrefix: tname + "-apikey."}
healthStore := storage.RedisCluster{KeyPrefix: tname + "-apihealth."}
orgStore := storage.RedisCluster{KeyPrefix: tname + "-orgKey."}
spec.Init(redisStore, redisStore, healthStore, orgStore)
return spec
}
Expand Down
2 changes: 1 addition & 1 deletion host_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestHostChecker(t *testing.T) {
t.Error("Should set defaults", GlobalHostChecker.checker.checkTimeout)
}

redisStore := GlobalHostChecker.store.(*storage.RedisCluster)
redisStore := GlobalHostChecker.store.(storage.RedisCluster)
if ttl, _ := redisStore.GetKeyTTL(PoolerHostSentinelKeyPrefix + testHttpFailure); int(ttl) != GlobalHostChecker.checker.checkTimeout+1 {
t.Error("HostDown expiration key should be checkTimeout + 1", ttl)
}
Expand Down
4 changes: 2 additions & 2 deletions le_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func StoreLEState(m *letsencrypt.Manager) {

log.Debug("[SSL] --> Connecting to DB")

store := &storage.RedisCluster{KeyPrefix: LEKeyPrefix}
store := storage.RedisCluster{KeyPrefix: LEKeyPrefix}
connected := store.Connect()

log.Debug("--> Connected to DB")
Expand All @@ -41,7 +41,7 @@ func StoreLEState(m *letsencrypt.Manager) {
func GetLEState(m *letsencrypt.Manager) {
checkKey := "cache"

store := &storage.RedisCluster{KeyPrefix: LEKeyPrefix}
store := storage.RedisCluster{KeyPrefix: LEKeyPrefix}

connected := store.Connect()
log.Debug("[SSL] --> Connected to DB")
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func setupGlobals() {
}

// Initialise our Host Checker
healthCheckStore := &storage.RedisCluster{KeyPrefix: "host-checker:"}
healthCheckStore := storage.RedisCluster{KeyPrefix: "host-checker:"}
InitHostCheckManager(healthCheckStore)

if config.Global.EnableAnalytics && analytics.Store == nil {
Expand Down Expand Up @@ -145,7 +145,7 @@ func setupGlobals() {
}).Debug("Notifier will not work in hybrid mode")
mainNotifierStore := storage.RedisCluster{}
mainNotifierStore.Connect()
MainNotifier = RedisNotifier{&mainNotifierStore, RedisPubSubChannel}
MainNotifier = RedisNotifier{mainNotifierStore, RedisPubSubChannel}

if config.Global.Monitor.EnableTriggerMonitors {
h := &WebHookHandler{}
Expand Down Expand Up @@ -924,7 +924,7 @@ func getGlobalStorageHandler(keyPrefix string, hashKeys bool) storage.Handler {
if config.Global.SlaveOptions.UseRPC {
return &RPCStorageHandler{KeyPrefix: keyPrefix, HashKeys: hashKeys, UserKey: config.Global.SlaveOptions.APIKey, Address: config.Global.SlaveOptions.ConnectionString}
}
return &storage.RedisCluster{KeyPrefix: keyPrefix, HashKeys: hashKeys}
return storage.RedisCluster{KeyPrefix: keyPrefix, HashKeys: hashKeys}
}

func main() {
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func start(arguments map[string]interface{}) {
log.WithFields(logrus.Fields{
"prefix": "main",
}).Debug("Initialising default org store")
//DefaultOrgStore.Init(&storage.RedisCluster{KeyPrefix: "orgkey."})
//DefaultOrgStore.Init(storage.RedisCluster{KeyPrefix: "orgkey."})
DefaultOrgStore.Init(getGlobalStorageHandler("orgkey.", false))
//DefaultQuotaStore.Init(getGlobalStorageHandler(CloudHandler, "orgkey.", false))
DefaultQuotaStore.Init(getGlobalStorageHandler("orgkey.", false))
Expand Down
16 changes: 6 additions & 10 deletions redis_signal_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,29 @@ type InterfaceNotification struct {
}

type RedisNotificationHandler struct {
CacheStore *storage.RedisCluster
CacheStore storage.RedisCluster
}

const (
UIChanName = "dashboard.ui.messages"
)

func (u *RedisNotificationHandler) Start() {
go u.StartUIPubSubConn()
u.CacheStore = storage.RedisCluster{KeyPrefix: "gateway-notifications:"}
u.CacheStore.Connect()
go u.PubSubLoop()
}

func (u *RedisNotificationHandler) Notify(n InterfaceNotification) error {
jsonError, err := json.Marshal(n)
if err != nil {
return err
}

if u.CacheStore != nil {
u.CacheStore.Publish(UIChanName, string(jsonError))
}

u.CacheStore.Publish(UIChanName, string(jsonError))
return nil
}

func (u *RedisNotificationHandler) StartUIPubSubConn() {
u.CacheStore = &storage.RedisCluster{KeyPrefix: "gateway-notifications:"}
u.CacheStore.Connect()
func (u *RedisNotificationHandler) PubSubLoop() {
// On message, synchronize
for {
err := u.CacheStore.StartPubSubHandler(UIChanName, u.HandleIncommingRedisEvent)
Expand Down
2 changes: 1 addition & 1 deletion redis_signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func isPayloadSignatureValid(notification Notification) bool {

// RedisNotifier will use redis pub/sub channels to send notifications
type RedisNotifier struct {
store *storage.RedisCluster
store storage.RedisCluster
channel string
}

Expand Down
4 changes: 2 additions & 2 deletions rpc_backup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func saveRPCDefinitionsBackup(list string) {

log.Info("--> Connecting to DB")

store := &storage.RedisCluster{KeyPrefix: RPCKeyPrefix}
store := storage.RedisCluster{KeyPrefix: RPCKeyPrefix}
connected := store.Connect()

log.Info("--> Connected to DB")
Expand All @@ -57,7 +57,7 @@ func LoadDefinitionsFromRPCBackup() []*APISpec {
tagList := getTagListAsString()
checkKey := BackupKeyBase + tagList

store := &storage.RedisCluster{KeyPrefix: RPCKeyPrefix}
store := storage.RedisCluster{KeyPrefix: RPCKeyPrefix}

connected := store.Connect()
log.Info("[RPC] --> Connected to DB")
Expand Down
Loading

0 comments on commit 8e7743a

Please sign in to comment.