Implementation of golang using context to control request timeout

Time:2020-9-28

cause

It has contacted a demand before: an interface is provided, this interface has a timeout time, if the timeout is returned, the timeout exception is invoked; in this interface, other interfaces are called; if the call time is exceeded, all requests will be completed.
   in this interface, I use the go coprocessor to call other interfaces, so it involves not only the timeout control of the request, but also the control of the parent coprocessor over the child coprocessor. After reading some materials, I learned the basic knowledge of context.

Context

Context is golang.org.pkg Next, the type is the interface type. The main functions are

The parent coprocessor controls all the children

   context can be accessed throughcontext.Background()perhaps context.TODO()Create an empty context. The two differences lie in todo

Context can be derived to create sub context. Context has four different sub contexts:
  (1)WithCancel: the method input parameter is a context; the return value is a copy of the parent context with the new done and the cancel function. When the cancel function is called, the channel is closed. Close rule: the internal receive channel will be closed first; if the channel is closed, the operation of receiving the channel will return immediately (that is, the channel returned by done), and context will send signals to all its sub values. If there are any sub contexts in the subcontext, the revocation signal will be passed down one level at a time. Finally, the context disconnects it from the parent context.
  (2)WithDeadlineandWithTimeout(this is the one used in this problem solving): the functions of withdeadline or withtimeout are very similar. Can be undone. They can not only be manually revoked, but also automatically scheduled revocation based on the given expiration time in the generation.
    with deadline is to set a point in time, and the time will be expired. Withtimeout is to set a period of time, such as a few seconds. After this period of time, it will time out. In fact, the underlying withtimeout is also implemented through withdeadline. The call to withtimeout is equal to withdeadline (parent, time.Now (). Add (timeout)) (where parent is the parent context)
  (3)WithValue: the input parameter is the parent, the stored key and the stored value. It returns a context with data. This context cannot be undone. The context will be skipped when the revocation signal is passed.

Data sharing between programs

Data sharing between coroutines mainly uses the subcontext generated by withvalue. The value stored in this context can also be read in other coprocesses. It can be used for data sharing.

Timeout cancellation of the coroutine

   it mainly uses the subcontext generated by withdeadline and the context field in httpclient request in go (described below)

Model of canceling timeout request

scheduling model


  

Among them, the judgment of timeout is based on the done pipeline in context. If it times out, the done pipeline can get something.

Cancel request after timeout

use http.NewRequest The req obtained by the method can be called WithContext to put the defined WithTimeout type context into it and then to call it. htto.Client The {}. Do() method is enough. The method of “Cancel” has been called in “request” manually, but it is not recommended to use this method in “request”. Because it cannot cancel the HTTP / 2 request.

There is now a privatized roundtrip method in the code that calls the cancelrequest method called by cancelrequest. The roundtrip method will be wrapped with a layer of roundtrip method in the transport, and then sent to the send method in the client for calling. (the specific source code can be consulted). So now through the do method of the client, the timeout control of the request can be completed automatically.

conclusion

   after the model is tested, the request timeout control can be realized. As long as the timeout time is set at the outermost layer for 30s, all requests in the coroutine will end and the corresponding coroutines will also end accordingly Client.Do Methods to make the timeout control more concise. I will write a blog about the client in HTTP to explain in detail the principle of client time-out control.

This article introduces the implementation of using context to control request timeout in golang. For more related content of golang context control request timeout, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!