Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to troubleshoot timeouts for web developers

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to troubleshoot timeout problems in web development". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to troubleshoot timeout problems in web development".

Bug side leakage var request = require ('request'); exports.handler = function (event, context, callback) {console.log ("event:" + event); console.log (' context:', JSON.stringify (context)) Const options = {url: 'https://saweather.market.alicloudapi.com/spot-to-weather?area=%E6%B3%B0%E5%B1%B1&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0', headers: {Authorization:' APPCODE 5d9129e294fc4f518793ae9f9a15dbff'} request (options, function (error, response) Body) {if (error | | response.statusCode! = 200) {console.log ("error" + error) Return} console.log (body.day_weether););}

A very simple nodejs function, just contact with the function calculation will often be high-spirited to write such a piece of code, usually in order to simply test the flow of the function. However, after it was released to it, there was a problem of call timeout.

Make a bold guess and be careful to verify

First of all, when you don't know how to start, your bold guess is as follows:

The function entry is written wrong.

There is something wrong with the logic in the code

There is a problem with the function calculation service.

To exclude 3 of these options, first run the test locally through fun local:

Fun local invoke nodejs_timeout

The results are obtained:

As you can see, the program is also stuck here. Thus, the possibility of 3 can be ruled out.

Next, you can further narrow the scope of troubleshooting by adding some logs or single-step debugging, which is chosen here because this method is often simpler.

First, use VSCode to make the next breakpoint in the sidebar:

Then use the following command to run the function in a debug manner (reference to the basic usage of debugging, which is not discussed too much here):

Fun local invoke-d 3000 nodejs_timeout

Then click the start debugging button of VSCode to debug:

As you can see, the function is called correctly and enters the entry function. At this point, the possibility of 1 can be ruled out.

Next, identify the problems in the possibility of 2.

The code at request sets a breakpoint, continue into it, and then you can see the value of the variable when it runs here in Local.

As you can see, the statusCode of the resposne returned by the http request is 200, as expected. Body also has data, which is in line with expectations.

Put the body.weakday into the watch to execute it and see the results.

This is not the expected value.

Take a closer look at the object body and find that its display format is not correct. Print out the type of body through typeof:

This guy is a string!

I was just about to curse, but I thought I wrote the code myself, so shut up.

Then turn it to json.

But after turning, found that things are even more wrong, day_weether is not directly under the body, but hidden very deep.

And the name is not day_weether, there is a spelling mistake. The correct way to write it should be:

JSON.parse (body). Showapi_res_body.f1.day_weather

What the.... Swearing is on the tip of my tongue, but when you think about writing the code yourself, you can't scold yourself, just fix it.

Paste the correct expression into Watch and check it out. Sure enough, you can get the correct value:

After verification, paste the correct value into the code, thinking that it is finally solved.

Rerun the function and find that you can get the data:

But the function is still stuck here, and does not continue to go down-the problem of timeout still exists!

Now, the anger is gone, replaced by blue thin, shiitake mushrooms. Stand up, look out of the window and let the cold wind wreak havoc on your face. For a long time, my heart became calmer.

At the end of the mountain, the willow is dark and the flowers are bright.

Calm down and sort out the clue:

Judging from the debugging results just now, the function has been run and the correct result has been obtained, but the function does not end until it times out. Suddenly, a faint answer hovers around you, you desperately want to grasp, pace back and forth, suddenly, as if there is a lifesaver, open the function to calculate Nodejs documents, you use all your strength to move your eyes, read the document quickly, and implore the emergence of that lifesaver in your heart. All of a sudden, you seem to have passed through a thick iron door, the light around you suddenly changed from dark to bright, and you were so stung by the bright light that you could not open your eyes. But you know, the answer is in this dazzling light. Tears, can not stop, slowly flow down your face. Finally, you loosened your clenched fist, bent down the straight waist, tears suddenly poured out of your nose, and you burst into tears. All the grievances vent from the heart through the throat, nose and eyes, accompanied by this wave of gradually weakening echoes, far away.

Cry, dry your tears, calm yourself down, and tap out the answer you have been looking for for a long time slowly but forcefully:

Callback (null, JSON.parse (body). Showapi_res_body.f1.day_weather)

Finally, this code is like your rebellious child, taking off the disguise of rebellion and showing its clever appearance:

If you want to do something good, you must sharpen its tools first.

After this incident, you have summed up three lessons:

Write code patiently and carefully, and every piece of code has to be thought about. To finish the function in one breath, it often seems to write fast, but in fact it will bury a lot of pits, so that others are fine, and they often accidentally cheat themselves.

Read more documents. To use the language, to read language documents, to use third-party libraries, to read library documents, to use products, to read product documents. If you don't read it carefully and write it based on your own guess, the code is often full of loopholes. Men must not hold on, do not be so special to yourself. If you want to rest when you are tired, you can read the documents and relax. As the saying goes, sharpening a knife does not miss a firewood worker!

Proficient use of Fun tools is a top priority. In the case of failing to do the first two points, the Fun tool plays an important role in troubleshooting the problem. But inversely proportional to the effort, it takes the least time to learn Fun tools, especially the local debugging functions. With such a huge income ratio, I have made up my mind that I must spend some more time to thoroughly eat the Fun tools!

After summing up, you look at the code you wrote attentively, just like looking at your own child, doting, loving, and angry. Suddenly, as soon as you frown, you yell: this is not right, when abnormal, also want to return!

So, you correct the wrong behavior of the code:

If (error | | response.statusCode! = 200) {console.log ("error" + error); callback (error, null);} Thank you for reading. The above is the content of "how to troubleshoot timeouts in web development". After the study of this article, I believe you have a deeper understanding of how to troubleshoot timeouts in web development, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report