Skip to content

AIML-303 Part 5: Cache ContrastSDK instance instead of creating new for each call - #55

Merged
ChrisEdwards merged 1 commit into
mainfrom
AIML-303-6-cache-sdk-instance
Jan 7, 2026
Merged

ChrisEdwards merged 1 commit into
mainfrom
AIML-303-6-cache-sdk-instance

Conversation

@ChrisEdwards

Copy link
Copy Markdown
Collaborator

⚠️ STACKED PR - Part of AIML-303 review fixes series

This PR is stacked on branch AIML-303-5-review-fixes. Review in sequence.


Summary

  • Cache the ContrastSDK instance in ContrastConfig instead of creating a new instance for each tool call
  • Reduces object creation overhead and improves performance

Why

The createSDK() method was creating a new ContrastSDK instance for every MCP tool call. Since the SDK is stateless and thread-safe (creates new HttpURLConnection per request), a single cached instance is sufficient.

What Changed

  • ContrastConfig.java: Added sdkInstance field with synchronized lazy initialization
  • Renamed createSDK() → getSDK() to reflect caching behavior
  • BaseMcpTool.java & BaseGetTool.java: Updated to call config.getSDK()
  • 13 test files: Updated mocks from createSDK() to getSDK()
  • ContrastConfigTest.java: Added test verifying caching (same instance returned, SDKHelper called only once)

Thread-Safety Analysis

ContrastSDK is safe to share because:

  • All fields are immutable after construction (apiKey, serviceKey, urlBuilder, gson, etc.)
  • makeConnection() creates a new HttpURLConnection per request
  • No per-request state stored

Testing

  • ✅ All 541 unit tests pass
  • Added new test: getSDK_should_cache_instance_and_return_same_on_subsequent_calls()

Source

PR #45 review comment - @JacobMagesHaskinsContrast

@ChrisEdwards ChrisEdwards changed the title AIML-303: Cache ContrastSDK instance instead of creating new for each call AIML-303 Step 6: Cache ContrastSDK instance instead of creating new for each call Dec 24, 2025
@ChrisEdwards ChrisEdwards changed the title AIML-303 Step 6: Cache ContrastSDK instance instead of creating new for each call AIML-303 Part 6: Cache ContrastSDK instance instead of creating new for each call Dec 24, 2025
@ChrisEdwards ChrisEdwards changed the title AIML-303 Part 6: Cache ContrastSDK instance instead of creating new for each call AIML-303 Part 5: Cache ContrastSDK instance instead of creating new for each call Dec 24, 2025
@ChrisEdwards
ChrisEdwards marked this pull request as ready for review December 24, 2025 08:54
@ChrisEdwards
ChrisEdwards force-pushed the AIML-303-6-cache-sdk-instance branch from c9c8250 to 7315bdd Compare January 2, 2026 17:04
@ChrisEdwards
ChrisEdwards changed the base branch from main to AIML-303-4-cleanup January 2, 2026 17:05
sdkInstance =
SDKHelper.getSDK(hostName, apiKey, serviceKey, userName, httpProxyHost, httpProxyPort);
}
return sdkInstance;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice caching!

@ChrisEdwards
ChrisEdwards force-pushed the AIML-303-6-cache-sdk-instance branch from 7315bdd to 53f3b17 Compare January 6, 2026 17:24
The SDK is stateless and thread-safe (creates new HttpURLConnection per
request), so caching a single instance avoids unnecessary object creation
overhead. Uses synchronized lazy initialization.

- Renamed createSDK() to getSDK() to reflect caching behavior
- Added test to verify caching works (same instance returned on subsequent calls)
- Updated all tool test mocks to use getSDK()
@ChrisEdwards
ChrisEdwards force-pushed the AIML-303-6-cache-sdk-instance branch from 53f3b17 to b1cfc25 Compare January 7, 2026 00:12
@ChrisEdwards
ChrisEdwards changed the base branch from AIML-303-4-cleanup to main January 7, 2026 00:12
@ChrisEdwards
ChrisEdwards merged commit 86ab576 into main Jan 7, 2026
@ChrisEdwards
ChrisEdwards deleted the AIML-303-6-cache-sdk-instance branch January 7, 2026 01:01
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.

3 participants