Added 'Await' method which allows the caller to choose how they will wait--but they should only use Reserve() or Await().
diff --git a/example/main.go b/example/main.go
index 3c97749..31c59c7 100644
--- a/example/main.go
+++ b/example/main.go
@@ -32,20 +32,28 @@
const Reservations = 1
-// const ReservationMask = -Reservations + 1
-
func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
sequence := disruptor.InitialSequenceValue
writer := disruptor.NewWriter(written, upstream, BufferSize)
for sequence <= Iterations {
sequence += Reservations
- writer.Reserve(sequence)
+ writer.Await(sequence)
ringBuffer[sequence&BufferMask] = sequence
writer.Commit(sequence)
}
}
// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
+// sequence := disruptor.InitialSequenceValue
+// writer := disruptor.NewWriter(written, upstream, BufferSize)
+// for sequence <= Iterations {
+// sequence = writer.Reserve(Reservations)
+// ringBuffer[sequence&BufferMask] = sequence
+// writer.Commit(sequence)
+// }
+// }
+
+// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
// previous := disruptor.InitialSequenceValue
// gate := disruptor.InitialSequenceValue
diff --git a/writer.go b/writer.go
index 4ed7dae..d580b8a 100644
--- a/writer.go
+++ b/writer.go
@@ -27,12 +27,19 @@
}
}
-func (this *Writer) Reserve(next int64) {
+func (this *Writer) Reserve(count int64) int64 {
+ this.previous += count
+ for this.previous-this.capacity > this.gate {
+ this.gate = this.upstream.Read(0)
+ }
+ return this.previous
+}
+
+func (this *Writer) Await(next int64) {
for next-this.capacity > this.gate {
this.gate = this.upstream.Read(0)
}
}
-
func (this *Writer) Commit(sequence int64) {
this.written.sequence = sequence
}