Skip to content

remove readonly of AuthenticationConnection in Session #844

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

Closed
wants to merge 1 commit into from

Conversation

wizicer
Copy link

@wizicer wizicer commented Jul 3, 2021

remove readonly of AuthenticationConnection in Session to support customize parallel connecting number.

Related Issues:
#215
#264
#409
#840

@wizicer
Copy link
Author

wizicer commented Aug 4, 2021

            SetSshNetConcurrency(100);
        public static void SetSshNetConcurrency(int concurrency)
        {
            var field = typeof(Renci.SshNet.Session).GetField("AuthenticationConnection",
                         BindingFlags.Static |
                         BindingFlags.NonPublic);
            if (field != null)
            {
                field.SetValue(null, new SemaphoreLight(concurrency));
            }
        }

Utilize this to change it at startup. It works for me flawlessly.

@xantari
Copy link

xantari commented Jul 15, 2022

Why are this and other PR's not being checked in. Is the project dead?

@drieseng
Copy link
Member

@xantari because I don't have time and because this is clearly a hack. I'm open to alternate proposals but can't promise that I'll immediately find time to discuss them.

@ascott18
Copy link

ascott18 commented Sep 6, 2022

Unfortunately the workaround above does not work on .NET Core 3+ due to dotnet/runtime#11571.

Instead, you can do this

var sessionSemaphoreField = typeof(Renci.SshNet.Session)
   .GetField("AuthenticationConnection", BindingFlags.Static | BindingFlags.NonPublic);

var semaphoreCountField = typeof(Renci.SshNet.Common.SemaphoreLight)
   .GetField("_currentCount", BindingFlags.Instance | BindingFlags.NonPublic);

object semaphore = sessionSemaphoreField.GetValue(null);

semaphoreCountField.SetValue(semaphore, concurrency);

or this, since SemaphoreLight does not validate against its provided initialCount:

var sessionSemaphoreField = typeof(Renci.SshNet.Session)
    .GetField("AuthenticationConnection", BindingFlags.Static | BindingFlags.NonPublic);

var semaphore = (Renci.SshNet.Common.SemaphoreLight)sessionSemaphoreField.GetValue(null);

// Add a much higher count to the semaphore to prevent pointless blocking of connection attempts.
semaphore.Release(100);

@ascott18
Copy link

ascott18 commented Sep 6, 2022

@drieseng I would suggest that the proposal in this comment is the best solution - to remove this semaphore outright and leave throttling as a concern of the calling application.

@Rob-Hague
Copy link
Collaborator

The AuthenticationConnection semaphore has been removed in #1304

@Rob-Hague Rob-Hague closed this Feb 6, 2024
@WojciechNagorski
Copy link
Collaborator

This issue has been fixed in the 2024.0.0 version.

@WojciechNagorski WojciechNagorski added this to the 2024.0.0 milestone Feb 22, 2024
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.

7 participants