Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
* Added `box.MustNew` wrapper for `box.New` without an error (#448).
* Added missing IPROTO feature flags to greeting negotiation
(iproto.IPROTO_FEATURE_IS_SYNC, iproto.IPROTO_FEATURE_INSERT_ARROW) (#466).
* Added Future.cond (sync.Cond) and Future.finished bool. Added Future.finish() marks Future as done (#496).

### Changed

Expand All @@ -28,6 +29,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
* `LogAppendPushFailed` replaced with `LogBoxSessionPushUnsupported` (#480).
* Removed deprecated `Connection` methods, related interfaces and tests are updated (#479).
* Replaced the use of optional types in crud with go-option library (#492).
* Future.done replaced with Future.cond (sync.Cond) + Future.finished bool (#496).

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TODO
* Removed `box.session.push()` support: Future.AppendPush() and Future.GetIterator()
methods, ResponseIterator and TimeoutResponseIterator types.
* Removed deprecated `Connection` methods, related interfaces and tests are updated.

*NOTE*: due to Future.GetTyped() doesn't decode SelectRequest into structure, substitute Connection.GetTyped() following the example:
```Go
var singleTpl = Tuple{}
Expand All @@ -30,6 +31,7 @@ TODO
).GetTyped(&tpl)
singleTpl := tpl[0]
```
* Future.done replaced with Future.cond (sync.Cond) + Future.finished bool.

## Migration from v1.x.x to v2.x.x

Expand Down
31 changes: 20 additions & 11 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
ErrRateLimited,
"Request is rate limited on client",
}
fut.done = nil
fut.finish()
return
}
}
Expand All @@ -950,23 +950,23 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
ErrConnectionClosed,
"using closed connection",
}
fut.done = nil
fut.finish()
shard.rmut.Unlock()
return
case connDisconnected:
fut.err = ClientError{
ErrConnectionNotReady,
"client connection is not ready",
}
fut.done = nil
fut.finish()
shard.rmut.Unlock()
return
case connShutdown:
fut.err = ClientError{
ErrConnectionShutdown,
"server shutdown in progress",
}
fut.done = nil
fut.finish()
shard.rmut.Unlock()
return
}
Expand Down Expand Up @@ -995,7 +995,7 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
runtime.Gosched()
select {
case conn.rlimit <- struct{}{}:
case <-fut.done:
case <-fut.WaitChan():
if fut.err == nil {
panic("fut.done is closed, but err is nil")
}
Expand All @@ -1009,12 +1009,12 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
// is "done" before the response is come.
func (conn *Connection) contextWatchdog(fut *Future, ctx context.Context) {
select {
case <-fut.done:
case <-fut.WaitChan():
case <-ctx.Done():
}

select {
case <-fut.done:
case <-fut.WaitChan():
return
default:
conn.cancelFuture(fut, fmt.Errorf("context is done (request ID %d): %w",
Expand All @@ -1036,7 +1036,12 @@ func (conn *Connection) send(req Request, streamId uint64) *Future {
conn.incrementRequestCnt()

fut := conn.newFuture(req)
if fut.done == nil {

fut.mutex.Lock()
is_done := fut.finished
fut.mutex.Unlock()

if is_done {
conn.decrementRequestCnt()
return fut
}
Expand All @@ -1059,12 +1064,16 @@ func (conn *Connection) putFuture(fut *Future, req Request, streamId uint64) {
shardn := fut.requestId & (conn.opts.Concurrency - 1)
shard := &conn.shard[shardn]
shard.bufmut.Lock()
select {
case <-fut.done:

fut.mutex.Lock()
is_done := fut.finished
fut.mutex.Unlock()

if is_done {
shard.bufmut.Unlock()
return
default:
}

firstWritten := shard.buf.Len() == 0
if shard.buf.Cap() == 0 {
shard.buf.b = make([]byte, 0, 128)
Expand Down
64 changes: 46 additions & 18 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,39 @@ type Future struct {
mutex sync.Mutex
resp Response
err error
cond sync.Cond
finished bool
done chan struct{}
}

func (fut *Future) wait() {
if fut.done == nil {
return
fut.mutex.Lock()
defer fut.mutex.Unlock()

for !fut.finished {
fut.cond.Wait()
}
<-fut.done
}

func (fut *Future) isDone() bool {
if fut.done == nil {
return true
}
select {
case <-fut.done:
return true
default:
return false
func (fut *Future) finish() {
fut.mutex.Lock()
defer fut.mutex.Unlock()

fut.finished = true

if fut.done != nil {
close(fut.done)
}

fut.cond.Broadcast()
}

// NewFuture creates a new empty Future for a given Request.
func NewFuture(req Request) (fut *Future) {
fut = &Future{}
fut.done = make(chan struct{})
fut.done = nil
fut.finished = false
fut.cond.L = &fut.mutex
fut.req = req
return fut
}
Expand All @@ -50,7 +57,7 @@ func (fut *Future) SetResponse(header Header, body io.Reader) error {
fut.mutex.Lock()
defer fut.mutex.Unlock()

if fut.isDone() {
if fut.finished {
return nil
}

Expand All @@ -60,7 +67,14 @@ func (fut *Future) SetResponse(header Header, body io.Reader) error {
}
fut.resp = resp

close(fut.done)
fut.finished = true

if fut.done != nil {
close(fut.done)
}

fut.cond.Broadcast()

return nil
}

Expand All @@ -69,12 +83,18 @@ func (fut *Future) SetError(err error) {
fut.mutex.Lock()
defer fut.mutex.Unlock()

if fut.isDone() {
if fut.finished {
return
}
fut.err = err

close(fut.done)
fut.finished = true

if fut.done != nil {
close(fut.done)
}

fut.cond.Broadcast()
}

// GetResponse waits for Future to be filled and returns Response and error.
Expand Down Expand Up @@ -122,8 +142,16 @@ func init() {

// WaitChan returns channel which becomes closed when response arrived or error occurred.
func (fut *Future) WaitChan() <-chan struct{} {
if fut.done == nil {
fut.mutex.Lock()
defer fut.mutex.Unlock()

if fut.finished {
return closedChan
}

if fut.done == nil {
fut.done = make(chan struct{})
}

return fut.done
}
Loading