Permalink
Browse files

Reworked the config system completely including an event hook system

  • Loading branch information...
1 parent 6f96e9f commit cd77bb4767f398e22fa1974001b5ccb767718f9a @pintsized committed Feb 20, 2012
Showing with 183 additions and 133 deletions.
  1. +37 −41 config.lua
  2. +42 −0 lib/config.lua
  3. +40 −0 lib/event.lua
  4. +64 −92 lib/ledge.lua
View
@@ -1,45 +1,41 @@
--- Ledge configuration file
+local config = require("lib.config")
+local event = require("lib.event")
+
+-- collapse_origin_requests
--
--- Nginx must be reloaded for changes to take affect.
--- Run "lua config.lua" to check for syntax correctness.
--- TODO: Write a config check tool (test number of params to matches etc)
+-- Collapse concurrent requests to the origin server into a single
+-- request. The other requests wait until the first one finishes.
+-- Suits slow but cacheable origins. Probably turn this off for
+-- URIs which are deifinitely not cacheable.
+config.set("collapse_origin_requests", true)
-local esi_processor = require("plugins.esi_processor")
-local preemptive_recache = require("plugins.preemptive_recache")
+-- serve_when_stale
+--
+-- How 'stale' an item can be (in seconds). A stale hit will return the cached
+-- result but trigger an origin round trip in the background.
+config.set("serve_when_stale", 3600, {
+ match_uri = {
+ { "^/about", 250 },
+ { "^/contact", 350 },
+ }
+})
-return {
-
- -- How 'stale' an item can be. A stale hit will return the cached
- -- result but trigger an origin round trip in the background.
- max_stale_age = {
- default = 900,
- match_uri = {
- { "^/about", 250 },
- { "^/contact", 360 },
- },
- },
-
- -- Collapse concurrent requests to the origin server into a single
- -- request. The other requests wait until the first one finishes.
- -- Suits slow but cacheable origins. Probably turn this off for
- -- URIs which are deifinitely not cacheable.
- collapse_origin_requests = {
- default = false,
- match_uri = {
- { "^/cart", false },
- },
- },
-
- --[[on_before_send = {
- default = function(ledge, response)
- --return esi_processor.process(ledge, response)
- end
- },]]--
-
- --[[on_after_send = {
- default = function(ledge, response)
- --return preemptive_recache.go(ledge, response)
- end
- }]]--
+-- You can define event handlers too. Predefined events are:
+--
+-- * config_loaded (before anything happens)
+-- * cache_accessed (cache state established)
+-- * origin_required (we're going to the origin)
+-- * origin_fetched (successfully fetched from the origin)
+-- * response_ready (response is ready to be sent)
+-- * response_sent (response has been sent to the browser)
+-- * finished (we're about to exit)
+--
+-- Example:
+--
+-- events.listen("response_ready", function()
+-- ngx.log(ngx.NOTICE, "My content is ready to be sent")
+-- end)
-}
+event.listen("response_sent", function()
+ ngx.log(ngx.NOTICE, "My stale config setting was " .. ngx.ctx.config.serve_when_stale)
+end)
View
@@ -0,0 +1,42 @@
+local config = {}
+
+-- Set a config parameter
+--
+-- Used by the config system to set a parameter. The vararg is an optional parameter
+-- containing a table which specifies per URI or header based value filters. This allows
+-- a config item to only be set on certain URIs, for example. See the config file for examples.
+--
+-- @param string The config parameter
+-- @param mixed The config default value
+-- @param ... Filter table. First level is the filter type "match_uri" or "match_header".
+-- Each of these has a list of pattern => value pairs.
+function config.set(param, value, ...)
+ local cfg = ngx.ctx.config or {}
+
+ cfg[param] = value
+ local filters = select(1, ...)
+ if filters then
+ if filters.match_uri then
+ for _,filter in ipairs(filters.match_uri) do
+ if ngx.var.uri:find(filter[1]) ~= nil then
+ cfg[param] = filter[2]
+ break
+ end
+ end
+ end
+
+ if filters.match_header then
+ local h = ngx.req.get_headers()
+ for _,filter in ipairs(filters.match_header) do
+ if h[filter[1]] ~= nil and h[filter[1]]:find(filter[2]) ~= nil then
+ cfg[param] = filter[3]
+ break
+ end
+ end
+ end
+ end
+
+ ngx.ctx.config = cfg
+end
+
+return config
View
@@ -0,0 +1,40 @@
+-- Event registration is handled in the config file. Nothing is prescribed, config
+-- code is free to define and listen to any event. The predefined event are:
+--
+-- * config_loaded (before anything happens)
+-- * cache_accessed (cache state established)
+-- * origin_required (we're going to the origin)
+-- * origin_fetched (successfully fetched from the origin)
+-- * response_ready (response is ready to be sent)
+-- * response_sent (response has been sent to the browser)
+-- * finished (we're about to exit)
+local event = {}
+
+
+-- Attach handler to an event
+--
+-- @param string The event identifier
+-- @param function The event handler
+-- @return void
+function event.listen(event, handler)
+ local e = ngx.ctx.event or {}
+ if not e[event] then e[event] = {} end
+ table.insert(e[event], handler)
+ ngx.ctx.event = e
+end
+
+
+-- Broadcast an event
+--
+-- @param string The event identifier
+-- @return void
+function event.emit(event)
+ local e = ngx.ctx.event or {}
+ for _,handler in ipairs(e[event] or {}) do
+ if type(handler) == 'function' then
+ handler()
+ end
+ end
+end
+
+return event
Oops, something went wrong.

0 comments on commit cd77bb4

Please sign in to comment.