-
Notifications
You must be signed in to change notification settings - Fork 88
Labels
enhancementNew feature or requestNew feature or requestinternalFor changes that affect the project's internal workings but not its outward-facing functionality.For changes that affect the project's internal workings but not its outward-facing functionality.
Milestone
Description
Problem
The Relay class has a two-step initialization pattern (new Relay() + await relay.init()) that is error-prone and leads to services being initialized with undefined Redis client and not propery ensure operator has balance
Current flow:
- In server/index.ts we import relay from server/server.ts, which runs all the initialization code for the Relay class
- Services within the class initialize with
undefinedRedis client (CacheService,EthImpl) - Later,
init()connects Redis and setsthis.redisClient - But services already have
undefined- updating the field doesn't help
Issues found:
- Acceptance tests don't call
relay.init()(discovered in PR review) - Services get
undefinedRedis client during construction - Easy to forget calling
init()after construction - Split initialization logic between constructor and
init()method
Proposed Solution
Implement the Static Factory Method pattern by:
- Make constructor
privateand acceptredisClientparameter - Create
static async init(logger, register): Promise<Relay>method that:- Connects to Redis first, if redis is enable
- Constructs Relay with connected Redis client
- Performs operator balance check
- Returns fully initialized instance
New usage:
// Old (error-prone)
const relay = new Relay(logger, register);
await relay.init();
// New (foolproof)
const relay = await Relay.init(logger, register);Benefits
- Enforces correct initialization - Private constructor prevents
new Relay() - Services get working Redis client - Connected before construction
- Single initialization method - All async setup in one place
- Catches test bugs - Tests must use proper initialization
- Type-safe - Returns
Promise<Relay>with guarantees
Implementation Checklist
- Update
Relayclass inpackages/relay/src/lib/relay.ts- Make constructor private with
redisClientparameter - Create static
init()factory method - Move Redis connection logic into factory
- Make
isRedisEnabled()static
- Make constructor private with
- Update
packages/server/src/index.tsto useRelay.init() - Update
packages/ws-server/src/index.tsto useRelay.init() - Update acceptance tests in
packages/server/tests/acceptance/ - Update unit tests that instantiate Relay
- Update documentation/examples
Breaking Change
This is a breaking change for any code that directly instantiates Relay with new Relay(). All consumers must update to use the async factory method.
Related: Addresses issue found in PR #4454 review
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestinternalFor changes that affect the project's internal workings but not its outward-facing functionality.For changes that affect the project's internal workings but not its outward-facing functionality.