File tree 5 files changed +60
-55
lines changed
5 files changed +60
-55
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,11 @@ import (
17
17
)
18
18
19
19
func main () {
20
- inputStream := func () <- chan string {
20
+ ctx , cancel := context.WithCancel (context.TODO ())
21
+ defer cancel ()
22
+
23
+ my := oproc.NewOrderedProc [string /* input param*/ , string /* output param*/ ](ctx)
24
+ my.InputStream = func () <- chan string {
21
25
ch := make (chan string )
22
26
go func () {
23
27
defer close (ch)
@@ -28,20 +32,16 @@ func main() {
28
32
}
29
33
}()
30
34
return ch
31
- }
32
-
33
- doWork := func (str string ) string {
35
+ }()
36
+ my.DoWork = func (str string ) string {
34
37
// sleep instead of fetching
35
38
time.Sleep (time.Duration (rand.Intn (100 )) * time.Millisecond )
36
39
return fmt.Sprintf (" %s ... is fetched!" , str)
37
40
}
38
41
39
- ctx , cancel := context.WithCancel (context.TODO ())
40
- defer cancel ()
41
-
42
42
start := time.Now ()
43
43
44
- for s := range oproc. OrderedProc (ctx, inputStream (), doWork /* , 10 optional # of goroutines */ ) {
44
+ for s := range my. Process ( ) {
45
45
fmt.Println (s)
46
46
}
47
47
Original file line number Diff line number Diff line change @@ -23,7 +23,11 @@ type item struct {
23
23
}
24
24
25
25
func main () {
26
- inputStream := func () <- chan item {
26
+ ctx , cancel := context .WithCancel (context .TODO ())
27
+ defer cancel ()
28
+
29
+ my := oproc.NewOrderedProc [item /*input param*/ , item /*output param*/ ](ctx )
30
+ my .InputStream = func () <- chan item {
27
31
valStream := make (chan item )
28
32
go func () {
29
33
defer close (valStream )
@@ -43,9 +47,8 @@ func main() {
43
47
}
44
48
}()
45
49
return valStream
46
- }
47
-
48
- doWork := func (o item ) item {
50
+ }()
51
+ my .DoWork = func (o item ) item {
49
52
resp , err := http .Get ("https://ctan.org/json/2.0/pkg/" + o .Key )
50
53
if err != nil {
51
54
log .Fatal (err )
@@ -57,11 +60,9 @@ func main() {
57
60
}
58
61
return o
59
62
}
63
+ my .Size = 20
60
64
61
- ctx , cancel := context .WithCancel (context .TODO ())
62
- defer cancel ()
63
-
64
- for s := range oproc .OrderedProc (ctx , inputStream (), doWork , 20 ) {
65
+ for s := range my .Process () {
65
66
b , err := json .Marshal (s )
66
67
if err == nil {
67
68
fmt .Println (string (b ))
Original file line number Diff line number Diff line change @@ -10,7 +10,11 @@ import (
10
10
)
11
11
12
12
func main () {
13
- inputStream := func () <- chan string {
13
+ ctx , cancel := context .WithCancel (context .TODO ())
14
+ defer cancel ()
15
+
16
+ my := oproc.NewOrderedProc [string /*input param*/ , string /*output param*/ ](ctx )
17
+ my .InputStream = func () <- chan string {
14
18
ch := make (chan string )
15
19
go func () {
16
20
defer close (ch )
@@ -21,20 +25,16 @@ func main() {
21
25
}
22
26
}()
23
27
return ch
24
- }
25
-
26
- doWork := func (str string ) string {
28
+ }()
29
+ my .DoWork = func (str string ) string {
27
30
// sleep instead of fetching
28
31
time .Sleep (time .Duration (rand .Intn (100 )) * time .Millisecond )
29
32
return fmt .Sprintf ("%s ... is fetched!" , str )
30
33
}
31
34
32
- ctx , cancel := context .WithCancel (context .TODO ())
33
- defer cancel ()
34
-
35
35
start := time .Now ()
36
36
37
- for s := range oproc . OrderedProc ( ctx , inputStream (), doWork ) {
37
+ for s := range my . Process ( ) {
38
38
fmt .Println (s )
39
39
}
40
40
Original file line number Diff line number Diff line change @@ -5,76 +5,80 @@ import (
5
5
"runtime"
6
6
)
7
7
8
- func OrderedProc [T , V any ](
9
- ctx context.Context ,
10
- inStream <- chan V ,
11
- doWork func (V ) T ,
12
- size ... int ,
13
- ) <- chan T {
14
- lvl := runtime .NumCPU ()
15
- if len (size ) > 0 {
16
- lvl = size [0 ]
8
+ type OrderedProc [TI , TO any ] struct {
9
+ Ctx context.Context
10
+ InputStream <- chan TI
11
+ DoWork func (TI ) TO
12
+ Size int
13
+ }
14
+
15
+ func NewOrderedProc [TI , TO any ](ctx context.Context ) * OrderedProc [TI , TO ] {
16
+ return & OrderedProc [TI , TO ]{
17
+ Ctx : ctx ,
18
+ Size : runtime .NumCPU (),
17
19
}
20
+ }
18
21
19
- orDone := func (c <- chan T ) <- chan T {
20
- ch := make (chan T )
22
+ func (o * OrderedProc [TI , TO ]) Process () <- chan TO {
23
+ orDone := func (c <- chan TO ) <- chan TO {
24
+ ch := make (chan TO )
21
25
go func () {
22
26
defer close (ch )
23
27
for {
24
28
select {
25
- case <- ctx .Done ():
29
+ case <- o . Ctx .Done ():
26
30
return
27
31
case v , ok := <- c :
28
32
if ! ok {
29
33
return
30
34
}
31
35
select {
32
36
case ch <- v :
33
- case <- ctx .Done ():
37
+ case <- o . Ctx .Done ():
34
38
}
35
39
}
36
40
}
37
41
}()
38
42
return ch
39
43
}
40
44
41
- chanchan := func () <- chan <- chan T {
42
- chch := make (chan (<- chan T ), lvl )
45
+ chanchan := func () <- chan <- chan TO {
46
+ chch := make (chan (<- chan TO ), o . Size )
43
47
go func () {
44
48
defer close (chch )
45
- for v := range inStream {
46
- ch := make (chan T )
49
+ for v := range o . InputStream {
50
+ ch := make (chan TO )
47
51
chch <- ch
48
52
49
53
go func () {
50
54
defer close (ch )
51
- ch <- doWork (v )
55
+ ch <- o . DoWork (v )
52
56
}()
53
57
}
54
58
}()
55
59
return chch
56
60
}
57
61
58
62
// bridge-channel
59
- return func (chch <- chan <- chan T ) <- chan T {
60
- vch := make (chan T )
63
+ return func (chch <- chan <- chan TO ) <- chan TO {
64
+ vch := make (chan TO )
61
65
go func () {
62
66
defer close (vch )
63
67
for {
64
- var ch <- chan T
68
+ var ch <- chan TO
65
69
select {
66
70
case maybe , ok := <- chch :
67
71
if ! ok {
68
72
return
69
73
}
70
74
ch = maybe
71
- case <- ctx .Done ():
75
+ case <- o . Ctx .Done ():
72
76
return
73
77
}
74
78
for v := range orDone (ch ) {
75
79
select {
76
80
case vch <- v :
77
- case <- ctx .Done ():
81
+ case <- o . Ctx .Done ():
78
82
}
79
83
}
80
84
}
Original file line number Diff line number Diff line change @@ -6,7 +6,11 @@ import (
6
6
)
7
7
8
8
func ExampleOrderedProc () {
9
- inputStream := func () <- chan string {
9
+ ctx , cancel := context .WithCancel (context .TODO ())
10
+ defer cancel ()
11
+
12
+ my := NewOrderedProc [string /*input param*/ , string /*output param*/ ](ctx )
13
+ my .InputStream = func () <- chan string {
10
14
ch := make (chan string )
11
15
go func () {
12
16
defer close (ch )
@@ -15,16 +19,12 @@ func ExampleOrderedProc() {
15
19
}
16
20
}()
17
21
return ch
18
- }
19
-
20
- doWork := func (str string ) string {
22
+ }()
23
+ my .DoWork = func (str string ) string {
21
24
return fmt .Sprintf ("line:%s" , str )
22
25
}
23
26
24
- ctx , cancel := context .WithCancel (context .TODO ())
25
- defer cancel ()
26
-
27
- for s := range OrderedProc (ctx , inputStream (), doWork ) {
27
+ for s := range my .Process () {
28
28
fmt .Println (s )
29
29
// Output:
30
30
// line:0
You can’t perform that action at this time.
0 commit comments