-
Notifications
You must be signed in to change notification settings - Fork 67
No goprocess #223
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
No goprocess #223
Changes from 1 commit
fd12b84
34ef082
e36c4b9
4733bce
1d3418b
1fb4568
b6d542d
1676d10
0dbca94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…output channel
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package query | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
@@ -149,6 +150,7 @@ type Results interface { | |
| NextSync() (Result, bool) // blocks and waits to return the next result, second parameter returns false when results are exhausted | ||
| Rest() ([]Entry, error) // waits till processing finishes, returns all entries at once. | ||
| Close() // client may call Close to signal early exit | ||
| Done() <-chan struct{} // signals that Results is closed | ||
| } | ||
|
|
||
| // results implements Results | ||
|
|
@@ -189,6 +191,10 @@ func (r *results) Query() Query { | |
| return r.query | ||
| } | ||
|
|
||
| func (r *results) Done() <-chan struct{} { | ||
| return r.ctx.Done() | ||
| } | ||
|
|
||
| // ResultBuilder is what implementors use to construct results | ||
| // Implementors of datastores and their clients must respect the | ||
| // Process of the Request: | ||
|
|
@@ -205,6 +211,7 @@ type ResultBuilder struct { | |
|
|
||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| wg sync.WaitGroup | ||
| } | ||
|
|
||
| // Results returns a Results to to this builder. | ||
|
|
@@ -232,6 +239,7 @@ func NewResultBuilder(q Query) *ResultBuilder { | |
| } | ||
| b.ctx, b.cancel = context.WithCancel(context.Background()) | ||
| context.AfterFunc(b.ctx, func() { | ||
| b.wg.Wait() | ||
| close(b.Output) | ||
| }) | ||
| return b | ||
|
|
@@ -240,8 +248,11 @@ func NewResultBuilder(q Query) *ResultBuilder { | |
| // ResultsWithChan returns a Results object from a channel | ||
| // of Result entries. | ||
| // | ||
| // DEPRECATED: This iterator is impossible to cancel correctly. Canceling it | ||
| // will leave anything trying to write to the result channel hanging. | ||
| // DEPRECATED: This iterator takes sepcial care to cancel correctly. Canceling | ||
|
||
| // it will leave anything trying to write to the result channel hanging, unless | ||
| // that write can select the result channel and Results.Done(). This requires | ||
| // creating the result channel, calline ResultsWithChan, and then writing to | ||
| // the results channel. | ||
| func ResultsWithChan(q Query, res <-chan Result) Results { | ||
| return ResultsWithContext(q, func(ctx context.Context, out chan<- Result) { | ||
| for { | ||
|
|
@@ -263,14 +274,15 @@ func ResultsWithChan(q Query, res <-chan Result) Results { | |
| }) | ||
| } | ||
|
|
||
| // ResultsWithCtxs returns a Results object with the results generated by the | ||
| // passed proc function called in a separate goroutine. | ||
| // ResultsWithContext returns a Results object with the results generated by | ||
| // the passed proc function called in a separate goroutine. | ||
| func ResultsWithContext(q Query, proc func(context.Context, chan<- Result)) Results { | ||
| b := NewResultBuilder(q) | ||
|
|
||
| b.wg.Add(1) | ||
| go func() { | ||
| defer b.cancel() | ||
| proc(b.ctx, b.Output) | ||
| b.cancel() | ||
| b.wg.Done() | ||
| }() | ||
|
|
||
| return b.Results() | ||
|
|
@@ -381,6 +393,11 @@ func (r *resultsIter) Query() Query { | |
| return r.query | ||
| } | ||
|
|
||
| func (r *resultsIter) Done() <-chan struct{} { | ||
| r.useLegacyResults() | ||
| return r.legacyResults.Done() | ||
| } | ||
|
|
||
| func (r *resultsIter) useLegacyResults() { | ||
| if r.legacyResults != nil { | ||
| return | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.