-
Notifications
You must be signed in to change notification settings - Fork 60
api: updated response/future methods #501
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
base: master
Are you sure you want to change the base?
Conversation
1f3999a to
e37fa1b
Compare
e37fa1b to
bf0bbdc
Compare
|
Detected flaky TestSQLBindings and TestStressSQL |
oleg-jukovec
left a comment
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.
Thank you for the patch!
See issues of the current implementation, fix them and we will try again.
response.go
Outdated
| resp.decoded = false | ||
| resp.decodedTyped = false | ||
| resp.err = nil | ||
| return &ptr |
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.
You've actually return a pointer into a local value (address of ptr) instead of an original slice that was allocated at a pool.
response.go
Outdated
| // Release free responses data and returns buffer's data. | ||
| Release() *[]byte |
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.
The release method must free all data allocated by the response. Otherwise, the semantic of the call is very confusing.
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.
ofc, we could design better
but if we want to keep to each connection their own sync.Pool, so we should provide some relations between response and connection (to do this, i used future and future.conn). Or could we single pool for all connections?
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.
oh, i understand my fault, sorry..
response.go
Outdated
| resp.header.RequestId, resp.header.Error = 0, 0 | ||
| resp.data = nil | ||
| ptr := resp.buf.b | ||
| resp.buf.b = nil | ||
| resp.buf.p = 0 | ||
| resp.decoded = false | ||
| resp.decodedTyped = false | ||
| resp.err = 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.
it is shorter, but still not work as expected.
| resp.header.RequestId, resp.header.Error = 0, 0 | |
| resp.data = nil | |
| ptr := resp.buf.b | |
| resp.buf.b = nil | |
| resp.buf.p = 0 | |
| resp.decoded = false | |
| resp.decodedTyped = false | |
| resp.err = nil | |
| ptr := resp.buf.b | |
| *resp = SelectResponse{} |
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.
but we are allocating here a new SelectResponse object, aren't we?
(and this is a baseResponse's method, why do we use SelectResponse?)
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.
but we are allocating here a new SelectResponse object, aren't we?
No, nothing will be allocated here.
(and this is a baseResponse's method, why do we use SelectResponse?)
My fault, should be baseResponse{}.
response.go
Outdated
| var baseResponsePool *sync.Pool = &sync.Pool{ | ||
| New: func() interface{} { | ||
| return &baseResponse{} | ||
| }, | ||
| } |
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.
This looks like overengineering. We don't have any allocations of the baseResponse objects. And therefore all this stuff about baseResponse is not needed at all.
| // Release is freeing the Future resources. | ||
| // After this, using this Future becomes invalid. | ||
| func (fut *Future) Release() { | ||
| if fut.pool != nil && fut.resp != nil { | ||
| ptr := fut.resp.Release() | ||
| fut.pool.Put(ptr) | ||
| } | ||
| } |
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.
It should look like this (all resources related to a response should be release inside response.Release()):
| // Release is freeing the Future resources. | |
| // After this, using this Future becomes invalid. | |
| func (fut *Future) Release() { | |
| if fut.pool != nil && fut.resp != nil { | |
| ptr := fut.resp.Release() | |
| fut.pool.Put(ptr) | |
| } | |
| } | |
| // Release is freeing the Future resources. | |
| // After this, using this Future becomes invalid. | |
| func (fut *Future) Release() { | |
| if fut.resp != nil { | |
| fut.resp.Release() | |
| } | |
| } |
future.go
Outdated
| func (fut *Future) Get() ([]interface{}, error) { | ||
| defer fut.Release() |
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.
| func (fut *Future) Get() ([]interface{}, error) { | |
| defer fut.Release() | |
| func (fut *Future) Get() ([]interface{}, error) { |
The code:
fut := conn.Do(req)
data, err := fut.Get()
data, err := fut.Get()
Should work fine by design. A user should call future.Release() after usage of the future directly:
fut := conn.Do(req)
data, err := fut.Get()
data, err := fut.Get()
fut.Release()
future.go
Outdated
| defer fut.Release() | ||
| fut.wait() |
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.
| defer fut.Release() | |
| fut.wait() | |
| fut.wait() |
connection.go
Outdated
|
|
||
| go conn.eventer(events) | ||
|
|
||
| buf := smallBuf{} |
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.
It will not work since the same object will be used inside multiple futures:
Line 895 in bf0bbdc
| if err := fut.SetResponse(header, &buf); err != nil { |
The simplest solution - a pool of smallBuf objects.
a15281c to
078cb7d
Compare
078cb7d to
beda545
Compare
Added method Release to Future and Response's interface, that allows to free used data directly by calling. Fixes #493
beda545 to
50c8953
Compare
Added Response's interface method Buffer.
Connection now contains sync.Pool that was used to reduce allocations.
Fixes #493
I didn't forget about (remove if it is not applicable):