ov icon indicating copy to clipboard operation
ov copied to clipboard

Fix deep context usage

Open ccoVeille opened this issue 1 year ago • 10 comments

A few methods were creating a new context, this is a bad pattern.

These issues were in oviewer/move_vertical.go for the following methods:

  • nextSection
  • prevSection

Because of these 2 functions code had to be refactored to pass func(context.Context) everywhere needed.

Fixes #539 539

ccoVeille avatar Apr 25 '24 16:04 ccoVeille

Here are two things I would like to discuss with you @noborus

  • asymetric code

why prevSection has a timeout https://github.com/noborus/ov/blob/64bd638fdf21036aecde885683f958bd93a7204a/oviewer/move_vertical.go#L298-L306

while nextSection doesn't

https://github.com/noborus/ov/blob/64bd638fdf21036aecde885683f958bd93a7204a/oviewer/move_vertical.go#L309-L323

  • should we add a context on Run

https://github.com/noborus/ov/blob/7106978e6cbda664896eda0c6534f676f5f8493a/oviewer/oviewer.go#L592-L595

It's usually a bad idea to do not set up the context in main.go

ccoVeille avatar Apr 25 '24 16:04 ccoVeille

This branch has to be tested by building the binary and launching it locally.

You know the tool and all the use cases.

ccoVeille avatar Apr 25 '24 16:04 ccoVeille

thank you.

Something strange happened because I kept adding context later. I think this pull request is correct.

noborus avatar Apr 26 '24 02:04 noborus

Here are two things I would like to discuss with you @noborus

  • asymetric code

why prevSection has a timeout

https://github.com/noborus/ov/blob/64bd638fdf21036aecde885683f958bd93a7204a/oviewer/move_vertical.go#L298-L306

while nextSection doesn't

The reason prevSection has a timeout is because section-header is using it. If you move to the end of a large file and then specify a non-existent line as section-header, the program will freeze while searching for section-header. This is to prevent that. Originally, a timeout should be inserted on the section-header side, but the fix was postponed.

noborus avatar Apr 26 '24 02:04 noborus

Here are two things I would like to discuss with you @noborus

  • asymetric code

why prevSection has a timeout

https://github.com/noborus/ov/blob/64bd638fdf21036aecde885683f958bd93a7204a/oviewer/move_vertical.go#L298-L306

while nextSection doesn't

The reason prevSection has a timeout is because section-header is using it. If you move to the end of a large file and then specify a non-existent line as section-header, the program will freeze while searching for section-header. This is to prevent that. Originally, a timeout should be inserted on the section-header side, but the fix was postponed.

Great explanation, you should comment the code accordingly then.

And maybe create an issue for the postponed fix

ccoVeille avatar Apr 26 '24 07:04 ccoVeille

Great explanation, you should comment the code accordingly then.

And maybe create an issue for the postponed fix

I agree. For now, I'll add a comment.

noborus avatar Apr 26 '24 11:04 noborus

You are not funny, your changes led to conflicts 😅

Next time, please append it to my PR, there were no rush to add the comment 😂

ccoVeille avatar Apr 26 '24 15:04 ccoVeille

sorry. solved.

noborus avatar Apr 28 '24 09:04 noborus

sorry. solved.

Thanks

ccoVeille avatar Apr 28 '24 09:04 ccoVeille

Feel free to test and merge

I agree with you, you will have to define the timeout in the caller function. It should be way easier now I refactored everything to pass context.

So feel free to add it in this PR or another.

ccoVeille avatar Apr 28 '24 09:04 ccoVeille

I'm going to merge! Thank you very much.

noborus avatar May 02 '24 23:05 noborus