Reverse Senior Mortagage

Our mortgage professionals have over 15 years of experience assisting clients in Orange County and surrounding areas; we know the California housing market better than anyone and we can help you get…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Using Golang Context to fail fast

How can golang context help avoid doing redundant things

Large software programs almost have a lot of components which need to talk to each other. Some of the components of such a system are dependent on a previous state executing successfully. This blog post is centred around how can we help these dependent components aware that something has gone wrong upstream and they need not waste critical computing resources. I will take an example of a HTTP client, but the approach can be used anywhere where components depend or are derived from each other.

Consider this hypothetical situation. you need to make three different HTTP calls to gather some data to serve the client. now, the shortest one of them responds that the server is unavailable. you required all three pieces of data to fulfil the request. In this case, you could either let the other two request go through, receive the response, arrange all data and then say that you failed to process the request or you could just stop the other two and respond with a failure ( as you would have after completing the other two).

what context means
context is basically a collection of correlating events occurring over a period of time. In computing terms, a context can be defined to be a collection of states occurring concurrently, iteratively or in parallel. A state is nothing but the collection of values the participants of that system has. An example would be a REST call. Here the call is the context and whatever events happen during that call define the state of your context. The golang context library helps communicate/coordinate from one state to another.

With context library at our disposal, we can now just pass in parent context to the caller function and listen for the done channel. If the parent context is cancelled, well, we wouldn’t need to make the request at all, else continue with the normal flow.

Thanks for reading!

Add a comment

Related posts:

June 2023 Leetcode Challenge

A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID. Each employee has one direct manager given in the manager array where…

Shiver

The hardest part about breaking up for Tess was his scent was everywhere. She could smell his soap and water cologne and notes of his musk in it and it made her shiver. When he left she curled up…

Professional Dentist and Dental Clinic Services

If you need emergency dental services? Contact our expert dentist Dr. M J. Hagood, DDS. We offer the highest level of care, the latest in dental technology and treatments to suit everyone. Schedule…