This content originally appeared on DEV Community and was authored by Name cannot be blank
Context and Goal
This task involved creating a simple API endpoint (/me
) in any language and framework of choice so opted to using the Gin framework in Go. The critical requirement was that the API’s response payload needed to include fresh data fetched from an external, third-party service Cat Fact
Why have I chosen Go? I’m currently learning Go and this serves as an opportunity to make it a project based learning.
The Core Lesson: Outgoing Traffic Control
The important architectural choice I faced was how to protect the third-party API from me. If my service goes viral(laughs) and gets 1,000 requests per second, I cannot afford to forward 1,000 requests to catfact.ninja.
This would certainly cause me to be blocked.
Hence, I resorted to limiting outgoing traffic by implementing a Token Bucket using golang’s rate package to strictly control the flow leaving my service (strict 0.5 requests per second).
If a request attempts to call the external API but the bucket is empty, we return a 429 Too Many Requests to the client, protecting the external service from my own overload.
Conclusion
It seems I’ve successfully built an API endpoint that is not only functional but is also resilient (with Timeouts), reliable (with UTC timestamps), and architecturally sound (with proper traffic control). Also, I hope I have not bitten more than I can chew by joining the rigorous HNG Internship while learning a new language.
This content originally appeared on DEV Community and was authored by Name cannot be blank