-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ischenkx/KAN-51
KAN-51 - instant deps introduced
- Loading branch information
Showing
14 changed files
with
706 additions
and
165 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
testing/tmp | ||
.idea | ||
unused/stand/*/bin/* | ||
unused/stand/*/bin/* | ||
/tmp/ |
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 |
---|---|---|
@@ -1,16 +1,71 @@ | ||
package pool | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"fmt" | ||
"kantoku/common/data/transactional" | ||
) | ||
|
||
//type Reader[Item any] interface { | ||
// Read(ctx context.Context) (<-chan Item, error) | ||
//} | ||
// | ||
//type Writer[Item any] interface { | ||
// Write(ctx context.Context, item Item) error | ||
//} | ||
// | ||
//type Pool[Item any] interface { | ||
// Reader[Item] | ||
// Writer[Item] | ||
//} | ||
|
||
type Reader[Item any] interface { | ||
Read(ctx context.Context) (<-chan Item, error) | ||
Read(ctx context.Context) (<-chan transactional.Object[Item], error) | ||
//pool.Reader[Transaction[Item]] | ||
} | ||
|
||
// Writer probably should have NewTransaction method | ||
type Writer[Item any] interface { | ||
Write(ctx context.Context, item Item) error | ||
// Write *must* write all items in a transaction! | ||
Write(ctx context.Context, items ...Item) error | ||
//pool.Writer[Item] | ||
} | ||
|
||
type Pool[Item any] interface { | ||
Reader[Item] | ||
Writer[Item] | ||
} | ||
|
||
func ReadAutoCommit[Item any](ctx context.Context, reader Reader[Item], | ||
f func(ctx context.Context, item Item) error) error { | ||
ch, err := reader.Read(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to open read channel: %s", err) | ||
} | ||
loop: | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
break loop | ||
case tx := <-ch: | ||
err = func(tx transactional.Object[Item]) error { | ||
item, err := tx.Get(ctx) | ||
defer tx.Rollback(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to get item from transaction: %s", err) | ||
} | ||
if err := f(ctx, item); err != nil { | ||
return err | ||
} | ||
if err := tx.Commit(ctx); err != nil { | ||
return fmt.Errorf("failed to commit: %s", err) | ||
} | ||
return nil | ||
}(tx) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,9 @@ | ||
package transactional | ||
|
||
import "context" | ||
|
||
type Object[Item any] interface { | ||
Get(ctx context.Context) (Item, error) | ||
Commit(ctx context.Context) error | ||
Rollback(ctx context.Context) error | ||
} |
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,28 @@ | ||
package mempool | ||
|
||
import ( | ||
"context" | ||
"go/types" | ||
"kantoku/common/data/transactional" | ||
) | ||
|
||
var _ transactional.Object[types.Object] = &Transaction[types.Object]{} | ||
|
||
type Transaction[T any] struct { | ||
data T | ||
success chan bool | ||
} | ||
|
||
func (t *Transaction[T]) Get(ctx context.Context) (T, error) { | ||
return t.data, nil | ||
} | ||
|
||
func (t *Transaction[T]) Commit(ctx context.Context) error { | ||
t.success <- true | ||
return nil | ||
} | ||
|
||
func (t *Transaction[T]) Rollback(ctx context.Context) error { | ||
t.success <- false | ||
return nil | ||
} |
Oops, something went wrong.