Golang, or just Go, has become known as the Go gopher language. If you still don't understand why the gopher you can find a surprising mascot history in this article. Well, let's get it started from the beginning, Golang has become known as one of the most productive programming languages out there. Unlike traditional languages like Java or Python, Go strikes a unique balance between easy-to-write code and quick execution, speeding up development and cutting down on debugging and testing time. This article will look at a real-world example of using goroutines and context in a project that interacts with a slow and unstable REST API microservice.
The Task
Let's imagine you have just joined the team and your team lead asks you to bind a new microservice written in Golang (for example the risk manager) to the user microservice exposed through REST API.
The Problem
The risk manager needs to interact with a REST API that can be slow and unstable, requiring us to handle such requests carefully. I will use goroutines for asynchronous HTTP requests and context to manage request timeouts and error handling.
The solution
Using goroutines and context in Golang allows efficient management of concurrent tasks and handling of slow or unstable external APIs.
The first step to interpreting it in a code is to create the API, I used the shareware service https://mockapi.io/ It is convenient to generate a REST API with a basic set of entities, such as users.
Let’s say someone tried their best and your company has an internal service that lists users. Our task is to reuse the user list in the new risk manager (the user data structure satisfies the contract described by mockapi.io). The code below makes a request, processes the body, and produces either a list of users or the corresponding error.
type User struct { CreatedAt string Name string Avatar string Id string } // Original slow request func fetchUsers(ctx context.Context) (*[]User, error) { resp, err := http.Get("https://
To control the operation, I will use the topmost parent context for all other contexts - context.Background. I will supplement this context with new data, in this case the timeout - context.WithTimeout, which I will define as 2 seconds. You can read more about working with contexts in Go in this article.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() usersChan := make(chan *[]User) errChan := make(chan error) go func() { users, err := fetchUsers(ctx) if err != nil { errChanIn the example, I use two channels - usersChan to record the result received from our internal service, and the second channel errChan - to record the error.
If the response from a slow API does not arrive within 2 seconds, we detect an error and give the opportunity to correctly process it on an external layer.In this practical example with the risk manager, goroutines enabled asynchronous HTTP requests, while context ensured timeout handling, which is critical for building reliable and resilient microservices.
P.S.
This article is intended for beginner developers who have basic knowledge, but little idea of where they can apply their knowledge. I will be glad to receive your feedback and will be happy to answer your questions.
A working example and several other implementations of the most basic things in the Go language can be found on my GitHub, at the link
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3