diff --git a/object/transformer/channel_test.go b/object/transformer/channel_test.go index 2487391d..6b17bd59 100644 --- a/object/transformer/channel_test.go +++ b/object/transformer/channel_test.go @@ -16,9 +16,10 @@ func TestChannelTarget(t *testing.T) { ch := make(chan *objectSDK.Object, 10) tt := new(testTarget) + ct := NewChannelTarget(ch) - chTarget, _ := newPayloadSizeLimiter(maxSize, NewChannelTarget(ch)) - testTarget, _ := newPayloadSizeLimiter(maxSize, tt) + chTarget, _ := newPayloadSizeLimiter(maxSize, func() ObjectTarget { return ct }) + testTarget, _ := newPayloadSizeLimiter(maxSize, func() ObjectTarget { return tt }) ver := version.Current() cnr := cidtest.ID() diff --git a/object/transformer/transformer.go b/object/transformer/transformer.go index b369b2e5..56a7f071 100644 --- a/object/transformer/transformer.go +++ b/object/transformer/transformer.go @@ -28,11 +28,13 @@ type payloadSizeLimiter struct { splitID *object.SplitID parAttrs []object.Attribute + + nextTarget ObjectTarget } type Params struct { Key *ecdsa.PrivateKey - NextTarget ObjectTarget + NextTargetInit func() ObjectTarget SessionToken *session.Object NetworkState EpochSource MaxSize uint64 @@ -113,7 +115,7 @@ func fromObject(obj *object.Object) *object.Object { } func (s *payloadSizeLimiter) initializeCurrent() { - // create payload hashers + s.nextTarget = s.NextTargetInit() s.writtenCurrent = 0 s.initPayloadHashers() } @@ -156,11 +158,11 @@ func (s *payloadSizeLimiter) release(ctx context.Context, finalize bool) (*Acces return nil, fmt.Errorf("fillHeader: %w", err) } - if err := s.NextTarget.WriteHeader(ctx, s.current); err != nil { + if err := s.nextTarget.WriteHeader(ctx, s.current); err != nil { return nil, fmt.Errorf("could not write header to next target: %w", err) } - if _, err := s.NextTarget.Close(ctx); err != nil { + if _, err := s.nextTarget.Close(ctx); err != nil { return nil, fmt.Errorf("could not close next target: %w", err) } @@ -275,7 +277,7 @@ func (s *payloadSizeLimiter) writeChunk(ctx context.Context, chunk []byte) error } func (s *payloadSizeLimiter) writeHashes(ctx context.Context, chunk []byte) error { - _, err := s.NextTarget.Write(ctx, chunk) + _, err := s.nextTarget.Write(ctx, chunk) if err != nil { return err } diff --git a/object/transformer/transformer_test.go b/object/transformer/transformer_test.go index ca9a1da5..11a68434 100644 --- a/object/transformer/transformer_test.go +++ b/object/transformer/transformer_test.go @@ -20,7 +20,7 @@ func TestTransformer(t *testing.T) { tt := new(testTarget) - target, pk := newPayloadSizeLimiter(maxSize, tt) + target, pk := newPayloadSizeLimiter(maxSize, func() ObjectTarget { return tt }) cnr := cidtest.ID() hdr := newObject(cnr) @@ -131,7 +131,7 @@ func benchmarkTransformer(b *testing.B, header *objectSDK.Object, payloadSize in b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { - f, _ := newPayloadSizeLimiter(maxSize, benchTarget{}) + f, _ := newPayloadSizeLimiter(maxSize, func() ObjectTarget { return benchTarget{} }) if err := f.WriteHeader(ctx, header); err != nil { b.Fatalf("write header: %v", err) } @@ -144,7 +144,7 @@ func benchmarkTransformer(b *testing.B, header *objectSDK.Object, payloadSize in } } -func newPayloadSizeLimiter(maxSize uint64, nextTarget ObjectTarget) (ObjectTarget, *keys.PrivateKey) { +func newPayloadSizeLimiter(maxSize uint64, nextTarget func() ObjectTarget) (ObjectTarget, *keys.PrivateKey) { p, err := keys.NewPrivateKey() if err != nil { panic(err) @@ -152,7 +152,7 @@ func newPayloadSizeLimiter(maxSize uint64, nextTarget ObjectTarget) (ObjectTarge return NewPayloadSizeLimiter(Params{ Key: &p.PrivateKey, - NextTarget: nextTarget, + NextTargetInit: nextTarget, NetworkState: dummyEpochSource(123), MaxSize: maxSize, WithoutHomomorphicHash: true,