My question boils down to this. How in the Kong plugin architecture can I do cluster related events. Example:
I have in local cache key “secret123” string, I want to invalidate that cached element key->value pair on other Kong nodes in a specific mlcache.
My basic understanding is kong.cluster_events is one easy way to do that through the core cluster_events DB table and uses a pub/sub model, but where I am slightly confused is how that fits into plugin architecture too.
Since plugins execute on a per service/route match(or globally) for which they are configured or in the init() phase of nginx/kong startup how can anything plugin related have background polling taking place that is required in a pub/sub model?
Essentially I want a way to broadcast to other nodes that I want this key revoked from their caches if they have it, which I imagine is something like:
kong.cluster_events:broadcast("my_custom_invalidation_channel", secret, nbf)
Where I give a custom channel, my secret string as the key, and an nbf to ensure all nodes invalidate at a similar time(5-10 seconds into the future in in sync or w/e). And this could live in the access phase of the plugin itself when there is an event I want to broadcast based on like a /revoke call made with this plugin thrown on it. Pretty easy I think to get the event out there. Correct me if this is inaccurate but it seems to be the way it works.
I think where I am confused is where would the bit live that will subscribe to the events of my custom logic channel and then do the :delete() on its own nodes local mlcache entry or something of the sort. Can the subscription live in the plugin too in the init with a function that does the cache delete? Something like this in the plugins init phase maybe?
local ok, err = kong.cluster_events:subscribe("my_custom_invalidation_channel", function(secret)
custom_mlcache:delete(secret)
end)
if not ok then
return nil, "failed to subscribe to my_custom_invalidation_channel cluster events " ..
"channel: " .. err
end
Then does this snippet of code just magically fire up a background process to run that delete logic if an invalidation event is found based on the global polling settings of kong(every 5 seconds or w/e) ^ ? Seems like it here:
Where the 3rd argument if set to true starts a ngx.timer for polling in the bg for me ? Would it really be that easy? My first chance looking into how kong does clustering, seems pretty nifty!