forked from smarty-prototypes/go-disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating reader to use new Consumer interface--it's still slower.
- Loading branch information
Oliver, Jonathan
committed
May 29, 2014
1 parent
d94cb2e
commit bfb8c88
Showing
3 changed files
with
89 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package disruptor | ||
|
||
type Consumer interface { | ||
Consume(int64, int64) int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
package disruptor | ||
|
||
import "time" | ||
|
||
type Reader struct { | ||
read *Cursor | ||
written *Cursor | ||
upstream Barrier | ||
consumer Consumer | ||
ready bool | ||
} // TODO: padding??? | ||
|
||
func NewReader(read, written *Cursor, upstream Barrier) *Reader { | ||
func NewReader(read, written *Cursor, upstream Barrier, consumer Consumer) *Reader { | ||
return &Reader{ | ||
read: read, | ||
written: written, | ||
upstream: upstream, | ||
ready: true, | ||
consumer: consumer, | ||
ready: false, | ||
} | ||
} | ||
|
||
func (this *Reader) Start() { | ||
this.ready = true | ||
go this.receive() | ||
} | ||
func (this *Reader) Stop() { | ||
this.ready = false | ||
} | ||
func (this *Reader) Receive(next int64) int64 { | ||
maximum := this.upstream.Read(next) | ||
|
||
if next <= maximum { | ||
return maximum | ||
} else if maximum = this.written.Load(); next <= maximum { | ||
return Gating | ||
} else if this.ready { | ||
return Idling | ||
} else { | ||
return Stopped | ||
} | ||
} | ||
func (this *Reader) receive() { | ||
current := this.read.Sequence + 1 | ||
for { | ||
gate := this.upstream.Read(current) | ||
|
||
func (this *Reader) Commit(sequence int64) { | ||
this.read.Store(sequence) | ||
if current <= gate { | ||
for current < gate { | ||
current += this.consumer.Consume(current, gate) | ||
} | ||
this.read.Store(current) | ||
current++ | ||
} else if gate = this.written.Load(); current <= gate { | ||
// Gating--TODO: wait strategy (provide gating count to wait strategy for phased backoff) | ||
// gating++ | ||
// idling = 0 | ||
time.Sleep(time.Microsecond) | ||
} else if this.ready { | ||
// Idling--TODO: wait strategy (provide idling count to wait strategy for phased backoff) | ||
// idling++ | ||
// gating = 0 | ||
time.Sleep(time.Microsecond) | ||
} else { | ||
break | ||
} | ||
} | ||
} |