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

reusing existing connection for requests #5704

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

mojganii
Copy link
Collaborator

@mojganii mojganii commented Jan 18, 2024

This pull request optimizes performance by enabling the reuse of the current connection, eliminating the necessity for a new connection with each incoming request. However, exceptions are made in cases such as request failures or modifications to the active configuration by other processes (e.g., Packet tunnel, UI part).


This change is Reviewable

@mojganii mojganii added the iOS Issues related to iOS label Jan 18, 2024
@mojganii mojganii self-assigned this Jan 18, 2024
Copy link

linear bot commented Jan 18, 2024

@mojganii mojganii force-pushed the reuse-existing-connection-for-requests-ios-449 branch from cdbb35f to 8da7d99 Compare January 18, 2024 10:48
Copy link
Contributor

@rablador rablador left a comment

Choose a reason for hiding this comment

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

Reviewed 8 of 9 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @mojganii)


ios/MullvadSettings/AccessMethodRepository.swift line 35 at r2 (raw file):

    }

    public var accessMethods: [PersistentAccessMethod] {

Can't we use fetchAll directly instead since it's already public.

Copy link
Collaborator Author

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @rablador)


ios/MullvadSettings/AccessMethodRepository.swift line 35 at r2 (raw file):

Previously, rablador (Jon Petersson) wrote…

Can't we use fetchAll directly instead since it's already public.

Done.

@mojganii mojganii force-pushed the reuse-existing-connection-for-requests-ios-449 branch from 8da7d99 to 9052ac0 Compare January 22, 2024 09:59
@mojganii mojganii requested a review from rablador January 22, 2024 10:00
Copy link
Contributor

@rablador rablador left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewed 5 of 9 files at r1, 4 of 4 files at r3, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @mojganii)


ios/MullvadREST/Transport/AccessMethodIterator.swift line 13 at r3 (raw file):

import MullvadSettings

class AccessMethodIterator {

nit
It just occured to me that this is named Iterator but doesn't implement the Iterator protocol, which is a misleading name.
Not a big deal, but we should be better at naming things in the future


ios/MullvadREST/Transport/AccessMethodIterator.swift line 50 at r3 (raw file):

        } else {
            /// When `firstIndex` is `nil`, that means the current configuration is not valid anymore
            /// Invalidating cache by replacing the `current`  to the next enabled access method

nit
There 2 spaces in current to


ios/MullvadREST/Transport/AccessMethodIterator.swift line 62 at r3 (raw file):

    func pick() -> PersistentAccessMethod {
        if enabledConfigurations.isEmpty {

Given that enabledConfigurations is really

dataSource.fetchAll().filter { $0.isEnabled }

we're calling this at best once, at worst 3 times every time we call pick(), which would be really bad if dataSource is a huge array to filter.

Suggestion

    func pick() -> PersistentAccessMethod {
        let configurations = enabledConfigurations
        if configurations.isEmpty {
            /// Returning `directAccess` when all methods are disabled
            return dataSource.directAccess
        } else {
            /// Picking the next `Enabled` configuration in order they are added
            /// And starting from the beginning when it reaches end
            let circularIndex = index % configurations.count
            return configurations[circularIndex]
        }
    }

ios/MullvadREST/Transport/AccessMethodIterator.swift line 63 at r3 (raw file):

    func pick() -> PersistentAccessMethod {
        if enabledConfigurations.isEmpty {
            /// Returning  `Default` strategy  when  all is disabled

nit
there are 2 spaces in Returning Default

Copy link
Collaborator Author

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @buggmagnet)


ios/MullvadREST/Transport/AccessMethodIterator.swift line 13 at r3 (raw file):

Previously, buggmagnet wrote…

nit
It just occured to me that this is named Iterator but doesn't implement the Iterator protocol, which is a misleading name.
Not a big deal, but we should be better at naming things in the future

do you have any suggestion? how about AccessMethodRotator?


ios/MullvadREST/Transport/AccessMethodIterator.swift line 50 at r3 (raw file):

Previously, buggmagnet wrote…

nit
There 2 spaces in current to

Done


ios/MullvadREST/Transport/AccessMethodIterator.swift line 62 at r3 (raw file):

Previously, buggmagnet wrote…

Given that enabledConfigurations is really

dataSource.fetchAll().filter { $0.isEnabled }

we're calling this at best once, at worst 3 times every time we call pick(), which would be really bad if dataSource is a huge array to filter.

Suggestion

    func pick() -> PersistentAccessMethod {
        let configurations = enabledConfigurations
        if configurations.isEmpty {
            /// Returning `directAccess` when all methods are disabled
            return dataSource.directAccess
        } else {
            /// Picking the next `Enabled` configuration in order they are added
            /// And starting from the beginning when it reaches end
            let circularIndex = index % configurations.count
            return configurations[circularIndex]
        }
    }

Done.


ios/MullvadREST/Transport/AccessMethodIterator.swift line 63 at r3 (raw file):

Previously, buggmagnet wrote…

nit
there are 2 spaces in Returning Default

Done

@mojganii mojganii force-pushed the reuse-existing-connection-for-requests-ios-449 branch from 9052ac0 to f2bee4d Compare January 23, 2024 13:34
Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions

@buggmagnet buggmagnet merged commit 14c7cd4 into main Jan 23, 2024
4 of 5 checks passed
@buggmagnet buggmagnet deleted the reuse-existing-connection-for-requests-ios-449 branch January 23, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants