Skip to content

Conversation

@copybara-service
Copy link

@copybara-service copybara-service bot commented Jan 15, 2026

Refactor oracle.go to improve code readability and testability.

key changes:

  1. Introduced DiscoveryClient and MetricsCollector interfaces.
    The Service struct now holds these dependencies. This allows mocking implementations to be injected during tests.
    discoveryClient is held as an instance (since it's stateless), and MetricsCollector is created via a factory function (newMetricsCollector) because it is stateful (holds DB connections) and runs inside a recoverable routine that may need to recreate it upon restart.

  2. Extracted logic from the Start method into smaller helper methods: waitForWorkload, startDiscoveryRoutine, startMetricCollectionRoutine, and startGuestActionsRoutine.
    This separates concerns and makes the code easier to follow and allows testing of individual components.

  3. Replaced blocking time.Sleep call with a select statement listening to ctx.Done(). This makes sure the service responds immediately to cancellation requests instead of waiting for the sleep interval to complete.

@copybara-service copybara-service bot force-pushed the test_856418402 branch 13 times, most recently from 9c1ee58 to c9203c2 Compare January 15, 2026 22:16
key changes:

1. Introduced DiscoveryClient and MetricsCollector interfaces.
The Service struct now holds these dependencies. This allows mocking implementations to be injected during tests.
discoveryClient is held as an instance (since it's stateless), and MetricsCollector is created via a factory function (newMetricsCollector) because it is stateful (holds DB connections) and runs inside a recoverable routine that may need to recreate it upon restart.

2. Extracted logic from the Start method into smaller helper methods: waitForWorkload, startDiscoveryRoutine, startMetricCollectionRoutine, and startGuestActionsRoutine.
This separates concerns and makes the code easier to follow and allows testing of individual components.

3. Replaced blocking time.Sleep call with a select statement listening to ctx.Done(). This makes sure the service responds immediately to cancellation requests instead of waiting for the sleep interval to complete.

PiperOrigin-RevId: 856817758
@copybara-service copybara-service bot merged commit 8a7dad2 into main Jan 15, 2026
@copybara-service copybara-service bot deleted the test_856418402 branch January 15, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant