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

Context value not saved with "WithValue" entry on 2nd run without going through WithOnAccept for both http1.1 and http2 #733

Closed
kolinfluence opened this issue Apr 18, 2023 · 14 comments
Labels
invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) stale

Comments

@kolinfluence
Copy link

kolinfluence commented Apr 18, 2023

context value not saved with "WithValue" entry on 2nd run without going through WithOnAccept for both http1.1 and http2

with reference to this issue:
#731

package main

import (
	"context"
	"log"
	"net"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func main() {
	var i uint8 = 0
	h := server.New(
		server.WithOnAccept(func(conn net.Conn) context.Context {
			log.Printf("only run once if ctx is kept alive right?")
			c := context.Background()
			if i == 0 { 
				i++
				return context.WithValue(c, "aaa", "bbb")
			}
			return context.Background()
		}),
	)
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"message": "pong"})
		log.Printf("value1 = %v", c.Value("aaa"))
		log.Printf("value2 = %v", c.Value("2"))
		//why 2nd run c.Value("2") is not 1? it is still nothing. isn't c updated below?
		c = context.WithValue(c, "2", uint8(1))
		log.Printf("value2 = %v", c.Value("2"))
	})

	h.Spin()
}

result gotten is where value2 is not saved on consecutive runs. totally strange! used to work before
pls provide a workaround or fix this.

2023/04/17 23:55:45.433963 engine.go:617: [Debug] HERTZ: Method=GET    absolutePath=/ping                     --> handlerName=main.main.func2 (num=1 handlers)
2023/04/17 23:55:45.434466 engine.go:389: [Info] HERTZ: Using network library=netpoll
2023/04/17 23:55:45.435113 transport.go:109: [Info] HERTZ: HTTP server listening on address=[::]:8888
2023/04/17 23:55:51 only run once if ctx is kept alive right?
2023/04/17 23:55:54 value1 = bbb
2023/04/17 23:55:54 value2 = <nil>
2023/04/17 23:55:54 value2 = 1
2023/04/17 23:56:05 value1 = bbb
2023/04/17 23:56:05 value2 = <nil>
2023/04/17 23:56:05 value2 = 1
2023/04/18 00:50:34 only run once if ctx is kept alive right?
2023/04/18 00:50:36 value1 = <nil>
2023/04/18 00:50:36 value2 = <nil>
2023/04/18 00:50:36 value2 = 1
2023/04/18 00:50:44 value1 = <nil>
2023/04/18 00:50:44 value2 = <nil>
2023/04/18 00:50:44 value2 = 1
@github-actions github-actions bot added the invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) label Apr 18, 2023
@github-actions
Copy link

This issue has been marked as invalid question, please give more information by following the issue template. The issue will be closed in 3 days if no further activity occurs.

@github-actions github-actions bot added the stale label Apr 18, 2023
@li-jin-gou
Copy link
Member

Conclusion: HTTP1 maintenance of long links only triggers Accepct once.

@li-jin-gou
Copy link
Member

I have used two different conn and it is fine

@kolinfluence
Copy link
Author

@li-jin-gou how to workaround? what do u mean two different conn is fine?

what about 1 connection? i'm testing with 1 now

@li-jin-gou
Copy link
Member

Conclusion: HTTP1 maintenance of long links only triggers Accepct once.

I have used two different tcp conn and it is fine

@li-jin-gou
Copy link
Member

This does not appear to be a problem with the framework and you need to change the code.

@kolinfluence
Copy link
Author

kolinfluence commented Apr 18, 2023

@li-jin-gou can u tell me where do i change in the code? the example i've given u, first run doesnt save the key "2" in context but "aaa"'s value is still there. why? how to change to have "2" 's value and key stick inside the code on 2nd run?

@kolinfluence
Copy link
Author

@li-jin-gou can u pls provide a solution? basically i would like to save key "2" into the http call. it doesnt get saved but "aaa" is saved.

@li-jin-gou
Copy link
Member

Currently working on other issues😩, you can try again yourself

@li-jin-gou
Copy link
Member

li-jin-gou commented Apr 18, 2023

The key is that a conn holds a ctx and HTTP1 will again keep long connection

@li-jin-gou
Copy link
Member

If you are already using hertz in production, please let us know - we will provide high-quality support for the needs of enterprise users - all you need to do is just reply this issue 👀

@kolinfluence
Copy link
Author

@li-jin-gou i cant reply to issue coz i havent fix this yet. it's still using version 0.5 of hertz with this issue i not sure what's the solution with.
possible to help resolve with a coded solution? i'm best to revert with feedback from production run.

@li-jin-gou
Copy link
Member

li-jin-gou commented Apr 18, 2023

No time at the moment and currently working on other issues, you can try again yourself @kolinfluence refer to https://www.jetbrains.com/help/go/debugging-code.html and https://l-lin.github.io/2020-02-10-debug-with-vim-go/

@kolinfluence
Copy link
Author

@li-jin-gou ok i got it with debug tool etc. was refactoring code and understand the issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) stale
Development

No branches or pull requests

2 participants