<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18623">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
  Kevin Harwell: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_outbound_registration: Make max random delay configurable.<br><br>Currently, PJSIP will randomly wait up to 10 seconds for each<br>outbound registration's initial attempt. The reason for this<br>is to avoid having all outbound registrations attempt to register<br>simultaneously.<br><br>This can create limitations with the test suite where we need to<br>be able to receive inbound calls potentially within 10 seconds of<br>starting up. For instance, we might register to another server<br>and then try to receive a call through the registration, but if<br>the registration hasn't happened yet, this will fail, and hence<br>this inconsistent behavior can cause tests to fail. Ultimately,<br>this requires a smaller random value because there may be no good<br>reason to wait for up to 10 seconds in these circumstances.<br><br>To address this, a new config option is introduced which makes this<br>maximum delay configurable. This allows, for instance, this to be<br>set to a very small value in test systems to ensure that registrations<br>happen immediately without an unnecessary delay, and can be used more<br>generally to control how "tight" the initial outbound registrations<br>are.<br><br>ASTERISK-29965 #close<br><br>Change-Id: Iab989a8e94323e645f3a21cbb6082287c7b2f3fd<br>---<br>M configs/samples/pjsip.conf.sample<br>A contrib/ast-db-manage/config/versions/18e0805d367f_max_random_initial_delay.py<br>M res/res_pjsip_outbound_registration.c<br>3 files changed, 42 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample</span><br><span>index fd1dd57..0e8f265 100644</span><br><span>--- a/configs/samples/pjsip.conf.sample</span><br><span>+++ b/configs/samples/pjsip.conf.sample</span><br><span>@@ -1297,6 +1297,11 @@</span><br><span>                 ; (default: "")</span><br><span> ;outbound_proxy=        ; Proxy through which to send registrations, a full SIP URI</span><br><span>                         ; must be provided (default: "")</span><br><span style="color: hsl(120, 100%, 40%);">+;max_random_initial_delay=10    ; Maximum random delay for initial registrations (default: 10)</span><br><span style="color: hsl(120, 100%, 40%);">+                                ; Generally it is a good idea to space out registrations</span><br><span style="color: hsl(120, 100%, 40%);">+                                ; to not overload the system. If you have a small number</span><br><span style="color: hsl(120, 100%, 40%);">+                                ; of registrations and need them to register more quickly,</span><br><span style="color: hsl(120, 100%, 40%);">+                                ; you can reduce this to a lower value.</span><br><span> ;retry_interval=60      ; Interval in seconds between retries if outbound</span><br><span>                         ; registration is unsuccessful (default: "60")</span><br><span> ;forbidden_retry_interval=0     ; Interval used when receiving a 403 Forbidden</span><br><span>diff --git a/contrib/ast-db-manage/config/versions/18e0805d367f_max_random_initial_delay.py b/contrib/ast-db-manage/config/versions/18e0805d367f_max_random_initial_delay.py</span><br><span>new file mode 100644</span><br><span>index 0000000..343b841</span><br><span>--- /dev/null</span><br><span>+++ b/contrib/ast-db-manage/config/versions/18e0805d367f_max_random_initial_delay.py</span><br><span>@@ -0,0 +1,21 @@</span><br><span style="color: hsl(120, 100%, 40%);">+"""max_random_initial_delay</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Revision ID: 18e0805d367f</span><br><span style="color: hsl(120, 100%, 40%);">+Revises: 0bee61aa9425</span><br><span style="color: hsl(120, 100%, 40%);">+Create Date: 2022-05-18 17:07:02.626045</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+"""</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# revision identifiers, used by Alembic.</span><br><span style="color: hsl(120, 100%, 40%);">+revision = '18e0805d367f'</span><br><span style="color: hsl(120, 100%, 40%);">+down_revision = '0bee61aa9425'</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+from alembic import op</span><br><span style="color: hsl(120, 100%, 40%);">+import sqlalchemy as sa</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+def upgrade():</span><br><span style="color: hsl(120, 100%, 40%);">+    op.add_column('ps_registrations', sa.Column('max_random_initial_delay', sa.Integer))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+def downgrade():</span><br><span style="color: hsl(120, 100%, 40%);">+    op.drop_column('ps_registrations', 'max_random_initial_delay')</span><br><span>diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c</span><br><span>index add3ddf..b00221a 100644</span><br><span>--- a/res/res_pjsip_outbound_registration.c</span><br><span>+++ b/res/res_pjsip_outbound_registration.c</span><br><span>@@ -104,6 +104,15 @@</span><br><span>                             <configOption name="outbound_proxy" default=""></span><br><span>                                    <synopsis>Full SIP URI of the outbound proxy used to send registrations</synopsis></span><br><span>                               </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+                         <configOption name="max_random_initial_delay" default="10"></span><br><span style="color: hsl(120, 100%, 40%);">+                                 <synopsis>Maximum interval in seconds for which an initial registration may be randomly delayed</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+                                        <description></span><br><span style="color: hsl(120, 100%, 40%);">+                                           <para>By default, registrations are randomly delayed by a small amount to prevent</span><br><span style="color: hsl(120, 100%, 40%);">+                                               too many registrations from being made simultaneously.</para></span><br><span style="color: hsl(120, 100%, 40%);">+                                           <para>Depending on your system usage, it may be desirable to set this to a smaller</span><br><span style="color: hsl(120, 100%, 40%);">+                                              or larger value to have fine grained control over the size of this random delay.</para></span><br><span style="color: hsl(120, 100%, 40%);">+                                 </description></span><br><span style="color: hsl(120, 100%, 40%);">+                          </configOption></span><br><span>                                <configOption name="retry_interval" default="60"></span><br><span>                                  <synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis></span><br><span>                                </configOption></span><br><span>@@ -305,6 +314,8 @@</span><br><span>  );</span><br><span>   /*! \brief Requested expiration time */</span><br><span>      unsigned int expiration;</span><br><span style="color: hsl(120, 100%, 40%);">+      /*! \brief Maximum random initial delay interval for initial registrations */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int max_random_initial_delay;</span><br><span>       /*! \brief Interval at which retries should occur for temporal responses */</span><br><span>  unsigned int retry_interval;</span><br><span>         /*! \brief Interval at which retries should occur for permanent responses */</span><br><span>@@ -1464,6 +1475,7 @@</span><br><span>         struct sip_outbound_registration_state *state = data;</span><br><span>        struct sip_outbound_registration *registration = ao2_bump(state->registration);</span><br><span>   size_t i;</span><br><span style="color: hsl(120, 100%, 40%);">+     int max_delay;</span><br><span> </span><br><span>   /* Just in case the client state is being reused for this registration, free the auth information */</span><br><span>         ast_sip_auth_vector_destroy(&state->client_state->outbound_auths);</span><br><span>@@ -1483,10 +1495,12 @@</span><br><span>       state->client_state->retries = 0;</span><br><span>      state->client_state->support_path = registration->support_path;</span><br><span>     state->client_state->auth_rejection_permanent = registration->auth_rejection_permanent;</span><br><span style="color: hsl(120, 100%, 40%);">+      max_delay = registration->max_random_initial_delay;</span><br><span> </span><br><span>   pjsip_regc_update_expires(state->client_state->client, registration->expiration);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-  schedule_registration(state->client_state, (ast_random() % 10) + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+       /* n mod 0 is undefined, so don't let that happen */</span><br><span style="color: hsl(120, 100%, 40%);">+      schedule_registration(state->client_state, (max_delay ? ast_random() % max_delay : 0) + 1);</span><br><span> </span><br><span>   ao2_ref(registration, -1);</span><br><span>   ao2_ref(state, -1);</span><br><span>@@ -2296,6 +2310,7 @@</span><br><span>  ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport));</span><br><span>       ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy));</span><br><span>     ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration));</span><br><span style="color: hsl(120, 100%, 40%);">+    ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_random_initial_delay", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_random_initial_delay));</span><br><span>         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval));</span><br><span>     ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "forbidden_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, forbidden_retry_interval));</span><br><span>  ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "fatal_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, fatal_retry_interval));</span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18623">change 18623</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18623"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Iab989a8e94323e645f3a21cbb6082287c7b2f3fd </div>
<div style="display:none"> Gerrit-Change-Number: 18623 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>