TykTechnologies / custom-plugin-examples

Custom plugin examples for the Tyk Gateway - if you ever needed to extend the functionality!
https://tyk.io
36 stars 5 forks source link

Custom Quota Consumer For Each Request #5

Open senolatac opened 3 years ago

senolatac commented 3 years ago

We would like to set custom quota usage per endpoint.

For example endpoint "xxx" will use 1 point per request, whereas endpoint "yyy" will use 5 point per request. And if the user has 100 quota limit, he can call 100 "xxx" requests or 20 "yyy" requests. Or 20 "xxx" and 16 "yyy" requests.

Is it implementable with custom-plugins?

joshblakeley commented 3 years ago

Hey @senolatac

Yes you're in custom plugin territory at this point. You can change the key objects directly from the plugin so can increment the quota counter by each of the possible values depending on your path/method etc.

If you write something in Go and it's sturdy we always consider inclusion into the main set of Gateway middlewares if it is a general solution for users. Or we can include any plugin solutions you come up with in this plugins meta repository.

senolatac commented 3 years ago

Hey @joshblakeley,

Actually, currently I try tyk-cloud and I want to implement this option. Is it possible with tyk-cloud? or is it possible only on-premise?

joshblakeley commented 3 years ago

Yep you can use plugins in cloud with the caveat that we need to load the code from trusted sources so you use our Mserv tool to get your custom code on S3 and then we pull from there to the gateways.

Docs are here https://tyk.io/docs/tyk-cloud/using-plugins/

senolatac commented 3 years ago

Alright, how can I debug the request? Is it possible? (Because I should figure out request counter header) And is it possible implement with java? I saw some plugins with java.

joshblakeley commented 3 years ago

On the cloud gateways hosted by Tyk it's just javascript and python currently (and Go too but it's a little more experimental) - though if you run a hybrid gateway then all plugin types will work.

You could do it without any special header if you wanted too - set the quota for a key to the GCF of the various weights and then just add them per path per request by modifying that key, there is an SDK for the javascript plugins for example that lets you call up and edit a key by some ID - that way you take advantage of the native quota mechanism.

For the development phase you could just run the APIs with the plugin on a local gateway just using the open source GW since it will operate exactly the same as on cloud.

senolatac commented 3 years ago

@joshblakeley I couldn’t get the exact idea in here. You mean that?

  1. Create a custom header like: x-custom-header: 5 for endpoint-1, x-custom-header: 3 for endpoint-2...
  2. Then decrease quota with this (x-custom-header) header. In here, if quota counter is "X-Ratelimit-Remaining" and if I set this header like X-Ratelimit-Remaining = X-Ratelimit-Remaining - x-custom-header, quota will be decreased like that...

I think in here the important thing is quota counter. Am I handle it via X-Ratelimit-Remaining?

Do you mean like this?

senolatac commented 3 years ago

@joshblakeley I think that there is misunderstanding in here.

Let me explain my ideal scenario:

  1. I have two APIs (my-api-1, my-api-2)
  2. I have a single api-key (api-key-1)
  3. api-key-1 has 10 quotes. X-Ratelimit-Limit=10 , X-Ratelimit-Remaining=10
  4. my-api-1 consumes 7 quotes, my-api-2 consumes 3 quotes. In here, I can handle consumer counts with custom-header...
  5. Call my-api-1 => X-Ratelimit-Remaining=3 => 10 - 7 = 3
  6. Call my-api-2 => X-Ratelimit-Remaining=0 => 3 - 3 = 0
  7. Call my-api-1 => Error => Too many request because no remaining quote.