Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared static apiClient between Svix instances #1601

Closed
mustafaozpinar opened this issue Jan 2, 2025 · 4 comments · Fixed by #1608
Closed

Shared static apiClient between Svix instances #1601

mustafaozpinar opened this issue Jan 2, 2025 · 4 comments · Fixed by #1608
Assignees
Labels
lib/java Java client library

Comments

@mustafaozpinar
Copy link

Bug Report

Version

Java library: 1.24.0

Platform

Darwin Mustafa-MacBook-Pro.local 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec 6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000 arm64

Description

Problem definiton:

Starting from 2024-10-08, we're getting error com.svix.exceptions.ApiException: Application not found (not_found) 1-2 per day. Svix library retries 5 times automatically and all fails. Then, when our code retries again immediately, it succeeds.

Here's the findings:

We build Svix instance with separate tokens for two different Svix endpoints. And each time when publishing a message a new Svix instance is created. In constructor, there’s a static assignment: Configuration.setDefaultApiClient(apiClient);

public Svix(final String token, final SvixOptions options) {
    ApiClient apiClient = new ApiClient();

    String[] tokenParts = token.split("\\.");
    String region = tokenParts[tokenParts.length - 1];
    if (region.equals("us")) {
       apiClient.setBasePath("https://api.us.svix.com/");
    } else if (region.equals("eu")) {
       apiClient.setBasePath("https://api.eu.svix.com/");
    } else if (region.equals("in")) {
       apiClient.setBasePath("https://api.in.svix.com/");
    } else {
       apiClient.setBasePath(options.getServerUrl());
    }

    apiClient.setUserAgent(String.format("svix-libs/%s/java", Svix.VERSION));

    HttpBearerAuth httpBearer = (HttpBearerAuth) apiClient.getAuthentication("HTTPBearer");
    httpBearer.setBearerToken(token);

    Configuration.setDefaultApiClient(apiClient);

    application = new Application();
    authentication = new Authentication();
    endpoint = new Endpoint();
    eventType = new EventType();
    integration = new Integration();
    message = new Message();
    messageAttempt = new MessageAttempt();
    statistics = new Statistics();
}

This api client is being used in MessageApi constructor:

public MessageApi() {
    this(Configuration.getDefaultApiClient());
} 

When processing messages in parallel, it’s possible that message api takes wrong apiClient because it’s set to a different one by other threads.
That’s why, fixed it by creating svix instances at startup and use those when sending messages.

Is it intended to set and share default apiClient between Svix instances?

@tasn
Copy link
Member

tasn commented Jan 2, 2025

Thanks! I've asked @svix-jplatte to take a look.

I assume the Kotlin client is similarly affected.

@svix-jplatte
Copy link
Member

Fix PR is up. Kotlin doesn't seem to be affected.

@tasn
Copy link
Member

tasn commented Jan 3, 2025

Thanks Jonas!

@svix-jplatte
Copy link
Member

Fix published in v1.45.1.

@svix-jplatte svix-jplatte self-assigned this Jan 3, 2025
@svix-jplatte svix-jplatte removed the lib/kotlin Kotlin client library label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib/java Java client library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants