Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add DoRedirects, DoTimeout and DoDeadline to Proxy middleware #2332

Merged
merged 6 commits into from
Feb 24, 2023

Conversation

gaby
Copy link
Member

@gaby gaby commented Feb 12, 2023

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Explain the details for making this change. What existing problem does the pull request solve?

Fixes issue reported on Discord

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - https://github.com/gofiber/docs for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

Signed-off-by: Juan Calderon-Perez <[email protected]>
@welcome
Copy link

welcome bot commented Feb 12, 2023

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Signed-off-by: Juan Calderon-Perez <[email protected]>
@gaby
Copy link
Member Author

gaby commented Feb 12, 2023

Middleware coverage is now at 90.7% up from ~78%

@gaby
Copy link
Member Author

gaby commented Feb 12, 2023

Do we still need to update the https://github.com/gofiber/docs repo?

@efectn
Copy link
Member

efectn commented Feb 12, 2023

Do we still need to update the https://github.com/gofiber/docs repo?

Yeah

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example would be cool for README

@gaby
Copy link
Member Author

gaby commented Feb 12, 2023

@efectn Example added, PR to docs repo submitted.

@ReneWerner87
Copy link
Member

Will review it later today or tomorrow morning

@ReneWerner87
Copy link
Member

am not 100% sure yet

guess we will also need methods for these 2 things which will come sooner or later, it would be good if we can do this with as little copying and effort as possible

https://github.com/valyala/fasthttp/blob/9230a3dd7a778630e568ab4c0f7a5b4340ffa3ab/client.go#L96
https://github.com/valyala/fasthttp/blob/9230a3dd7a778630e568ab4c0f7a5b4340ffa3ab/client.go#L69

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check my comments and try to improve

@ReneWerner87
Copy link
Member

@gaby

@gaby
Copy link
Member Author

gaby commented Feb 13, 2023

check my comments and try to improve

Sounds good, I will take a look when I get home.

@ReneWerner87
Copy link
Member

thx

@gaby
Copy link
Member Author

gaby commented Feb 20, 2023

Coverage:

ok  	github.com/gofiber/fiber/v2/middleware/proxy	12.136s	coverage: 92.7% of statements

@gaby gaby changed the title Feature: Add DoRedirects to Proxy middleware Feature: Add DoRedirects, DoTimeout and DoDeadline to Proxy middleware Feb 20, 2023
@gaby
Copy link
Member Author

gaby commented Feb 22, 2023

@ReneWerner87 This one should be ready for review

@ReneWerner87
Copy link
Member

Ok will do the review tomorrow (vacation day)

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks very good so far

@gaby
Can you add a benchmark and check the master version against the current version for the normal "Do"?

have the assumption that we might have additional allocations by passing the parameters to the inner "doAction" method, which we could then optimize if that is the case

@ReneWerner87
Copy link
Member

just want to make sure that we always offer a very fast method, without allocations that we could also avoid now already

@gaby
Copy link
Member Author

gaby commented Feb 24, 2023

@ReneWerner87 To be able to do a benchmark, I had to use the original Do_RestoreOriginalURL tests without the createProxyTestServer.

Test:

// go test -v ./... -run=^$ -bench=Benchmark_Proxy_Do_RestoreOriginalURL -benchmem -count=4
func Benchmark_Proxy_Do_RestoreOriginalURL(b *testing.B) {
	app := fiber.New()
	app.Get("/proxy", func(c *fiber.Ctx) error {
		return c.SendString("ok")
	})
	app.Get("/test", func(c *fiber.Ctx) error {
		return Do(c, "/proxy")
	})

	for n := 0; n < b.N; n++ {
		_, _ = app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
	}
}

Results for master branch:

ubuntu@ubuntu:~/Desktop/git/other/fiber/middleware/proxy$ go test -v ./... -run=^$ -bench=Benchmark_Proxy_Do_RestoreOriginalURL -benchmem -count=4
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/proxy
cpu: AMD Ryzen 7 1700X Eight-Core Processor         
Benchmark_Proxy_Do_RestoreOriginalURL
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     150	   7942960 ns/op	   19538 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     148	   8120107 ns/op	   19417 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     147	   8082403 ns/op	   19356 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     147	   8057143 ns/op	   19405 B/op	      47 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2/middleware/proxy	7.798s

Results after changes from this PR:

ubuntu@ubuntu:~/Desktop/git/fix/fiber/middleware/proxy$ go test -v ./... -run=^$ -bench=Benchmark_Proxy_Do_RestoreOriginalURL -benchmem -count=4
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/proxy
cpu: AMD Ryzen 7 1700X Eight-Core Processor         
Benchmark_Proxy_Do_RestoreOriginalURL
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     151	   7957396 ns/op	   19428 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     150	   7942095 ns/op	   19355 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     150	   7809482 ns/op	   19348 B/op	      47 allocs/op
Benchmark_Proxy_Do_RestoreOriginalURL-4   	     151	   8060554 ns/op	   19261 B/op	      46 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2/middleware/proxy	7.759s

@ReneWerner87 ReneWerner87 merged commit dc038d8 into master Feb 24, 2023
@welcome
Copy link

welcome bot commented Feb 24, 2023

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@gaby gaby deleted the follow-redirects branch February 25, 2023 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants