Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Rare: switching the app in and out frequently can needlessly consume all the budget for API calls #265

Closed
alexbbrown opened this issue Nov 28, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@alexbbrown
Copy link

alexbbrown commented Nov 28, 2022

Describe the bug

Switching the app in and out of focus - eg while collecting assets for a post - consumes API calls quickly, and can overwhelm the server call limits resulting in a "too many requests" error.
This is because each time the app is brought forward it invokes 'refreshIdentity' which refreshes validation, custom emojis, lists, filters, emojis, announcements and preferences from the server, even if this was done only seconds ago.

Note: this is hard to achieve, but possible. I would only recommend addressing this issue if there are other reasons API calls can be consumed quickly.

weak recommendation: throttle these refreshes so they don't do so many reads in the limit case of switching every few seconds. Different resource types might merit different fetch rates.

Describe the bug

I was searching for an intermittent problem #253 where posting took 30s and started to print out messages from the server (in this case hachyderm.io). I suspected this might have to do with the server slowing down the calls for some reason, or perhaps other performance issues with the call, so I enabled some more logging (print statements).

note: Im a big fan of combine so was very happy to see how this app is wired together!

I noticed that each call to the server returned information about rate limiting:

    "X-RateLimit-Limit" =     (
        300
    );
    "X-RateLimit-Remaining" =     (
        82
    );
    "X-RateLimit-Reset" =     (
        "2022-11-28T04:20:00.470711Z"
    );

Low impact… each time the app comes to the foreground, 7 API reads are done. These come out of a budget of around 300/5 minutes or 1 per second set by my mastodon server.

I observed that each time I switched from the app to the Home Screen or another app, the count went down by 5-10.

I recalled that I often spend a while composing a post, gathering materials like URLs from safari, photos from my album etc. These actions would surely switch the app out of focus, triggering the fetch here:

        NotificationCenter.default.publisher(for: UIScene.willEnterForegroundNotification)
            .sink { [weak self] _ in self?.viewModel.refreshIdentity() }
            .store(in: &cancellables)

To Reproduce
Steps to reproduce the behavior:

  1. Go to app.
  2. swipe left and right on the home affordance about 100 times
  3. observe an error: "too many requests"

I also saw some scary SQL transaction errors doing this.

image

Expected behavior

the app should be designed to avoid overloading the server with calls if possible.

(please complete the following information):

  • Device: iPhone 12
  • OS: iOS 16.2
  • App Version: 1.7

Additional context
again, this is rate, and more interesting as an exercise in debugging than a bug to be urgently fixed

@jzzocc jzzocc added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Nov 28, 2022
@jzzocc
Copy link
Member

jzzocc commented Nov 28, 2022

Fixed in v1.7.1

@jzzocc jzzocc closed this as completed Nov 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants