-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
refactor gin code logic #2790
base: master
Are you sure you want to change the base?
refactor gin code logic #2790
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2790 +/- ##
=======================================
Coverage 98.70% 98.70%
=======================================
Files 41 41
Lines 2085 2088 +3
=======================================
+ Hits 2058 2061 +3
Misses 15 15
Partials 12 12
Continue to review full report at Codecov.
|
func init() { | ||
if engine == nil { | ||
engine = gin.Default() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this different from just writing:
var engine = gin.Default()
? I don't think you can change the value of the engine
variable before init
is executed, so the if condition will always be true.
v.once.Do(func() { | ||
// lazyInit initialize the validate Settings, no need to use sync.once | ||
func (v *defaultValidator) lazyInit() { | ||
if v.validate == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is an improvement. I believe sync.Once
was used to avoid race conditions. This version seems kind of safe, too, but it might call validator.New()
multiple times unnecessarily. I don't see the point of eliminating sync.Once
.
There is no need to use the once mechanism here. You are all using this mechanism indiscriminately.
…------------------ Original ------------------
From: kszafran ***@***.***>
Date: Mon,Jul 19,2021 0:30 AM
To: gin-gonic/gin ***@***.***>
Cc: heige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
@kszafran commented on this pull request.
In ginS/gins.go:
> -func engine() *gin.Engine { -once.Do(func() { -internalEngine = gin.Default() -}) -return internalEngine +func init() { +if engine == nil { +engine = gin.Default() +}
How is this different from just writing:
var engine = gin.Default()
? I don't think you can change the value of the engine variable before init is executed, so the if condition will always be true.
In binding/default_validator.go:
> return v.validate } -func (v *defaultValidator) lazyinit() { -v.once.Do(func() { +// lazyInit initialize the validate Settings, no need to use sync.once +func (v *defaultValidator) lazyInit() { +if v.validate == nil {
I'm not sure if this is an improvement. I believe sync.Once was used to avoid race conditions. This version seems kind of safe, too, but it might call validator.New() multiple times unnecessarily. I don't see the point of eliminating sync.Once.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
For engine, this is a variable, not the previous function mode. Even if the once mechanism is used, it is recommended to declare it as a private variable mode, which is better and more intuitive.
…------------------ Original ------------------
From: daheige ***@***.***>
Date: Mon,Jul 19,2021 7:52 AM
To: reply+ACMGV623UENNAYDFPQQH7KV7AA4DDEVBNHHDQTCOQU ***@***.***>, gin-gonic/gin ***@***.***>
Cc: daheige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
There is no need to use the once mechanism here. You are all using this mechanism indiscriminately.
------------------ Original ------------------
From: kszafran ***@***.***>
Date: Mon,Jul 19,2021 0:30 AM
To: gin-gonic/gin ***@***.***>
Cc: heige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
@kszafran commented on this pull request.
In ginS/gins.go:
> -func engine() *gin.Engine { -once.Do(func() { -internalEngine = gin.Default() -}) -return internalEngine +func init() { +if engine == nil { +engine = gin.Default() +}
How is this different from just writing:
var engine = gin.Default()
? I don't think you can change the value of the engine variable before init is executed, so the if condition will always be true.
In binding/default_validator.go:
> return v.validate } -func (v *defaultValidator) lazyinit() { -v.once.Do(func() { +// lazyInit initialize the validate Settings, no need to use sync.once +func (v *defaultValidator) lazyInit() { +if v.validate == nil {
I'm not sure if this is an improvement. I believe sync.Once was used to avoid race conditions. This version seems kind of safe, too, but it might call validator.New() multiple times unnecessarily. I don't see the point of eliminating sync.Once.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
For the singleton mode implemented by once mechanism, there is no need to use once mechanism here. I think it is more intuitive and clear to judge by nil.
…------------------ Original ------------------
From: daheige ***@***.***>
Date: Mon,Jul 19,2021 7:57 AM
To: reply+ACMGV623UENNAYDFPQQH7KV7AA4DDEVBNHHDQTCOQU ***@***.***>, gin-gonic/gin ***@***.***>
Cc: daheige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
For engine, this is a variable, not the previous function mode. Even if the once mechanism is used, it is recommended to declare it as a private variable mode, which is better and more intuitive.
------------------ Original ------------------
From: daheige ***@***.***>
Date: Mon,Jul 19,2021 7:52 AM
To: reply+ACMGV623UENNAYDFPQQH7KV7AA4DDEVBNHHDQTCOQU ***@***.***>, gin-gonic/gin ***@***.***>
Cc: daheige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
There is no need to use the once mechanism here. You are all using this mechanism indiscriminately.
------------------ Original ------------------
From: kszafran ***@***.***>
Date: Mon,Jul 19,2021 0:30 AM
To: gin-gonic/gin ***@***.***>
Cc: heige ***@***.***>, Author ***@***.***>
Subject: Re: [gin-gonic/gin] refactor gin code logic (#2790)
@kszafran commented on this pull request.
In ginS/gins.go:
> -func engine() *gin.Engine { -once.Do(func() { -internalEngine = gin.Default() -}) -return internalEngine +func init() { +if engine == nil { +engine = gin.Default() +}
How is this different from just writing:
var engine = gin.Default()
? I don't think you can change the value of the engine variable before init is executed, so the if condition will always be true.
In binding/default_validator.go:
> return v.validate } -func (v *defaultValidator) lazyinit() { -v.once.Do(func() { +// lazyInit initialize the validate Settings, no need to use sync.once +func (v *defaultValidator) lazyInit() { +if v.validate == nil {
I'm not sure if this is an improvement. I believe sync.Once was used to avoid race conditions. This version seems kind of safe, too, but it might call validator.New() multiple times unnecessarily. I don't see the point of eliminating sync.Once.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
1.rename validateHeader to getIPFromHeader
2.fix:shadow: declaration of "err" shadows declaration at line 304
3.lazyInit initialize the validate Settings, no need to use sync.once
4.ginS engine adjust
5.code style adjust