darkweak / souin

An HTTP cache system, RFC compliant, compatible with @tyktechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @go-kratos, @gin-gonic, @roadrunner-server, @zalando, @zeromicro, @nginx and @apache

Home Page:https://docs.souin.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tyk plugin: stale-while-revalidate

acaporrini opened this issue · comments

Hi all,

I'm trying to use souin as Tyk plugin with Tyk Gateway 4.0.6 and is not clear to me how to configure it to enable stale-while-revalidate cache control.

My Tyk API definition:

{
  "name": "Test API",
  "slug": "Test API",
  "api_id": "test_api",
  "org_id": "test",
  "use_keyless": false,
  "auth": {
    "auth_header_name": "X-Api-Key",
    "strip_auth_data": true
  },
  "definition": {
    "location": "url"
  },
  "version_data": {
    "not_versioned": false,
    "versions": {
      "v1": {
        "name": "v1",
        "use_extended_paths": false
      }
    }
  },
  "proxy": {
    "listen_path": "/test/",
    "enable_load_balancing" : true,
    "target_list": [
      " https://test.com"
    ],
    "strip_listen_path": true
  },
  "global_rate_limit" : {
    "rate": 10000,
    "per": 60
  },

  "custom_middleware": {
    "pre": [],
    "post": [
      {
        "name": "SouinRequestHandler",
        "path": "./middleware/souin-plugin.so"
      }
    ],
    "response": [
      {
        "name": "SouinResponseHandler",
        "path": "./middleware/souin-plugin.so"
      }
    ],
    "driver": "goplugin"
  },
  "config_data": {
    "httpcache": {
      "api": {
        "souin": {
          "enable": true
        }
      },
      "default_cache": {
        "ttl": "60s",
        "stale": "60m"
    },
      "log_level": "DEBUG"
    }
  },
  "active": true
}

if 'https://test.com' sends responses with header:

"Cache-Control" : "max-age=120, stale-while-revalidate=36000"

First time misses the cache (expected)
Then for the next 60 secs the following requests are fetched from cache (also expected)
At this point after the cache ttl is reached I would expect the cache marked as stale for the next 60 mins, so that if I send another request immediately after with stale-while-revalidate=36000 this would return stale content and revalidate the cache in background.

However the following requests are simply a cache miss so that the response is refetched from upstream while the request is waiting.

I also tried to add config:

"default_cache_control": "max-age=120, stale-while-revalidate=36000"

But still no success.

Am I doing something wrong?

Hello @acaporrini the Tyk plugin differs from the base middleware (because Tyk plugin doesn't as a basic middleware). I will have to update the plugin with the latest changes from the common middleware. I will have to write more Tyk e2e tests to ensure it has the same behaviour too.

Thank you for trying to use Souin inside Tyk and thank you for your feedback! 🙏

Hi @darkweak thank you a lot for quick reply and for looking into this. Do you maybe have an idea on when this will be implemented?

Hey @acaporrini I'll wrap the Tyk changes in the PR #335 it may take few days to be merged but I cross my fingers to merge and release before Friday. I don't have much time to work on the missing fix to make the pipeline green.