-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipeline): added a batch processor
Needs some more test improvements
- Loading branch information
1 parent
5db8219
commit b27f24f
Showing
12 changed files
with
637 additions
and
76 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,32 @@ | ||
package util | ||
|
||
import "context" | ||
|
||
// Cacel passes the in chan to the out chan until the context is canceled. | ||
// When the context is canceled, everything from in is passed to the cancel func instead of out. | ||
// Out closes when in is closed. | ||
func Cancel(ctx context.Context, canceled func(i interface{}), in <-chan interface{}) <-chan interface{} { | ||
out := make(chan interface{}) | ||
go func() { | ||
defer close(out) | ||
for { | ||
select { | ||
// When the context isnt canceld, pass everything to the out chan | ||
// until in is closed | ||
case i, open := <-in: | ||
if !open { | ||
return | ||
} | ||
out <- i | ||
// When the context is canceled, pass all ins to the | ||
// cancel fun until in is closed | ||
case <-ctx.Done(): | ||
for i := range in { | ||
canceled(i) | ||
} | ||
return | ||
} | ||
} | ||
}() | ||
return out | ||
} |
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,57 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCancel(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
cancel func(i interface{}) | ||
in <-chan interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want <-chan interface{} | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Cancel(tt.args.ctx, tt.args.cancel, tt.args.in); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Cancel() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
t.Run("in never closes", func(t *testing.T) { | ||
// In closes after the context | ||
in := make(chan interface{}) | ||
go func() { | ||
defer close(in) | ||
i := 0 | ||
endAt := time.Now().Add(10 * time.Millisecond) | ||
for now := time.Now(); now.Before(endAt); now = time.Now() { | ||
in <- i | ||
i++ | ||
} | ||
t.Log("ended") | ||
}() | ||
|
||
// Create a logger for the cancel fun | ||
canceled := func(i interface{}) { | ||
t.Logf("canceled: %d\n", i) | ||
} | ||
|
||
// Context times out after 1 second | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) | ||
defer cancel() | ||
for o := range Cancel(ctx, canceled, in) { | ||
t.Logf("out: %d", o) | ||
} | ||
|
||
}) | ||
} |
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
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,27 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// Delay delays reading each input by duration. | ||
// If the context is canceled the delay will not be applied. | ||
func Delay(ctx context.Context, duration time.Duration, in <-chan interface{}) <-chan interface{} { | ||
out := make(chan interface{}) | ||
go func() { | ||
defer close(out) | ||
// Keep reading from in until its closed | ||
for i := range in { | ||
// Take one element from in and pass it to out | ||
out <- i | ||
select { | ||
// Wait duration before reading another input | ||
case <-time.After(duration): | ||
// Don't wait if the context is canceled | ||
case <-ctx.Done(): | ||
} | ||
} | ||
}() | ||
return out | ||
} |
Oops, something went wrong.