Farfetch/kafkaflow

[Feature Request]: Improve Dependency Injection Scope Management

filipeesch opened this issue · 0 comments

Is your request related to a problem you have?

This request is directly related to a problem within the KafkaFlow framework. The issue involves dependency injection scope management, particularly for consumers that execute asynchronous processing tasks. Currently, there are instances where middleware is created using the wrong dependency injection (DI) scope, specifically when using Worker or Message lifetimes. This implementation flaw can lead to critical issues when consumers process messages asynchronously. Premature disposal of the DI scope can result in ObjectDisposedException errors within client applications.

Describe the solution you'd like

Given the importance of managing dependency injection scopes, especially in a distributed event-driven system like Kafka, it's pivotal to provide granular control over dependencies' lifecycle. Here's a more detailed breakdown of the proposed solution:

1. Exposing DI Scopes in IMessageContext and IConsumerContext:

  • IMessageContext Changes:

    The IMessageContext will expose the IDependencyResolver DependencyResolver property. This resolver is intrinsically tied to the scope of a single processed message.

IDependencyResolver DependencyResolver { get; }
  • IConsumerContext Changes:

    Two properties will be introduced: ConsumerDependencyResolver and WorkerDependencyResolver. The former will resolve dependencies tied to the lifecycle of a single consumer, whereas the latter caters to a single worker's lifecycle.

IDependencyResolver ConsumerDependencyResolver { get; }
IDependencyResolver WorkerDependencyResolver { get; }

2. MiddlewareExecutor Update:

The MiddlewareExecutor needs to be enhanced to recognize and use the correct dependency injection scope when instantiating middleware instances. This will be achieved by:

  • Fetching the appropriate IDependencyResolver from the MessageContext or ConsumerContext.
  • Resolving middleware instances using the fetched resolver.

This change ensures that middleware instances are created and managed using the correct DI scope, thus addressing potential scope mismatches.

3. Benefits:

By integrating these changes, the following benefits are realized:

  • Enhanced Flexibility: Client applications will have a fine-grained control over dependency lifecycles. They can decide when and how to instantiate objects tied to different KafkaFlow lifecycles.
  • Resource Management: By correctly managing the DI scopes, premature disposal issues are mitigated, leading to improved resource management and fewer errors like ObjectDisposedException.
  • Scalability: This enhancement ensures that as the application scales and more Kafka consumers and workers are employed, the resource management remains optimal, reducing potential issues that can arise in high-concurrency scenarios.

Are you able to help bring it to life and contribute with a Pull Request?

Yes

Additional context

No response