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

Synchronize access to route singletons #248

Merged
merged 4 commits into from
Sep 3, 2021
Merged

Conversation

julianlocke
Copy link
Contributor

Makes objective-c route singleton access thread safe by synchronizing over a lock object per file. This has been live in our production iOS app for a week without incident. Example output:

static NSObject *lockObj = nil;
+ (void)initialize {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    lockObj = [[NSObject alloc] init];
  });
}

+ (DBRoute *)DBACCOUNTAppleLogin {
  @synchronized(lockObj) {
    if (!DBACCOUNTAppleLogin) {
      DBACCOUNTAppleLogin =
          [[DBRoute alloc] init:@"apple_login"
                           namespace_:@"account"
                           deprecated:@NO
                           resultType:[DBACCOUNTAppleLoginResult class]
                            errorType:[DBACCOUNTAppleLoginError class]
                                attrs:@{
                                  @"auth" : @"app",
                                  @"host" : @"api",
                                  @"style" : @"rpc"
                                }
                dataStructSerialBlock:nil
              dataStructDeserialBlock:nil];
    }
    return DBACCOUNTAppleLogin;
  }
}

+ (DBRoute *)DBACCOUNTAppleSignup {
  @synchronized(lockObj) {
    if (!DBACCOUNTAppleSignup) {
      DBACCOUNTAppleSignup =
          [[DBRoute alloc] init:@"apple_signup"
                           namespace_:@"account"
                           deprecated:@NO
                           resultType:[DBACCOUNTAppleSignupResult class]
                            errorType:[DBACCOUNTAppleSignupError class]
                                attrs:@{
                                  @"auth" : @"app",
                                  @"host" : @"api",
                                  @"style" : @"rpc"
                                }
                dataStructSerialBlock:nil
              dataStructDeserialBlock:nil];
    }
    return DBACCOUNTAppleSignup;
  }
}
... etc.

General Contributing

  • Have you read the Code of Conduct and signed the CLA?

Is This a Code Change?

  • Non-code related change (markdown/git settings etc)
  • Code Change
  • Example/Test Code Change

Validation

  • Have you ran tox?
  • Do the tests pass?

@codecov
Copy link

codecov bot commented Sep 1, 2021

Codecov Report

Merging #248 (43898e6) into main (b0e5ae1) will decrease coverage by 0.04%.
The diff coverage is 5.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #248      +/-   ##
==========================================
- Coverage   51.65%   51.60%   -0.05%     
==========================================
  Files          37       37              
  Lines        8393     8400       +7     
  Branches     1790     1790              
==========================================
  Hits         4335     4335              
- Misses       3744     3751       +7     
  Partials      314      314              
Flag Coverage Δ
unit 51.60% <5.71%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
stone/backends/obj_c_types.py 0.00% <0.00%> (ø)
stone/backends/python_rsrc/stone_validators.py 87.84% <ø> (ø)
stone/backends/swift.py 0.00% <ø> (ø)
stone/backends/swift_helpers.py 0.00% <0.00%> (ø)
stone/backends/swift_types.py 0.00% <0.00%> (ø)
stone/backends/tsd_client.py 55.31% <0.00%> (ø)
stone/backends/tsd_types.py 37.91% <0.00%> (ø)
stone/cli.py 0.00% <0.00%> (ø)
stone/frontend/parser.py 92.04% <0.00%> (ø)
stone/backend.py 83.07% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0e5ae1...43898e6. Read the comment docs.

Copy link
Contributor

@YufeiG YufeiG left a comment

Choose a reason for hiding this comment

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

lgtm

@aelawson aelawson merged commit 68bc875 into main Sep 3, 2021
@aelawson aelawson deleted the thread-safety-rebase branch September 3, 2021 16:56
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.

4 participants