@@ -15,12 +15,12 @@ type operation[Resource any] struct {
1515}
1616
1717type worker [Resource any ] struct {
18- goRoutineNumber int
19- operations chan operation [Resource ]
20- loadResource func (context.Context , string ) (Resource , error )
21- saveResource func (context.Context , string , Resource ) error
22- minDuration time.Duration
23- maxDuration time.Duration
18+ goRoutineNumber int
19+ incomingOperations <- chan operation [Resource ]
20+ loadResource func (context.Context , string ) (Resource , error )
21+ saveResource func (context.Context , string , Resource ) error
22+ minDuration time.Duration
23+ maxDuration time.Duration
2424}
2525
2626func (w worker [Resource ]) run () {
@@ -34,50 +34,50 @@ func (w worker[Resource]) run() {
3434 select {
3535 case <- ticker .C :
3636 now := time .Now ()
37- for _ , b := range batchByDeadline {
38- if b .deadline .Before (now ) {
39- err := w .saveResource (b .ctx , b .key , b .resource )
40- b .publishResult (err )
41- delete (batchByResourceKey , b .key )
37+ for _ , _batch := range batchByDeadline {
38+ if _batch .deadline .Before (now ) {
39+ err := w .saveResource (_batch .ctx , _batch .key , _batch .resource )
40+ _batch .publishResult (err )
41+ delete (batchByResourceKey , _batch .key )
4242 batchByDeadline = batchByDeadline [1 :]
4343 continue
4444 }
4545 }
46- case op , ok := <- w .operations :
46+ case _operation , ok := <- w .incomingOperations :
4747 if ! ok {
48- for key , b := range batchByResourceKey {
49- err := w .saveResource (b .ctx , key , b .resource )
50- b .publishResult (err )
48+ for key , _batch := range batchByResourceKey {
49+ err := w .saveResource (_batch .ctx , key , _batch .resource )
50+ _batch .publishResult (err )
5151 continue
5252 }
5353 return
5454 }
5555
56- b , found := batchByResourceKey [op .resourceKey ]
56+ _batch , found := batchByResourceKey [_operation .resourceKey ]
5757 if ! found {
5858 ctx , cancel := context .WithTimeout (context .Background (), w .maxDuration )
5959 defer cancel ()
6060
6161 now := time .Now ()
6262
63- resource , err := w .loadResource (ctx , op .resourceKey )
63+ resource , err := w .loadResource (ctx , _operation .resourceKey )
6464 if err != nil {
65- op .result <- err
65+ _operation .result <- err
6666 continue
6767 }
68- b = & batch [Resource ]{
68+ _batch = & batch [Resource ]{
6969 ctx : ctx ,
70- key : op .resourceKey ,
70+ key : _operation .resourceKey ,
7171 resource : resource ,
7272 deadline : now .Add (w .minDuration ),
7373 }
74- batchByResourceKey [op .resourceKey ] = b
75- batchByDeadline = append (batchByDeadline , b )
74+ batchByResourceKey [_operation .resourceKey ] = _batch
75+ batchByDeadline = append (batchByDeadline , _batch )
7676 }
7777
78- b .results = append (b .results , op .result )
78+ _batch .results = append (_batch .results , _operation .result )
7979
80- op .run (b .resource )
80+ _operation .run (_batch .resource )
8181 }
8282 }
8383}
0 commit comments