foldershare-8.x-1.2/src/Utilities/LimitUtilities.php

src/Utilities/LimitUtilities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
 
namespace Drupal\foldershare\Utilities;
 
use Drupal\Component\Utility\Bytes;
 
use Drupal\foldershare\Settings;
 
/**
 * Defines utility functions to get and check memory and execution time limits.
 *
 * The functions in this class support runtime queries to monitor the current
 * process's memory and execution time. Callers may use this to decide how
 * to divide up and schedule work so that these limits are not encountered.
 *
 * <B>Warning:</B> This class is strictly internal to the FolderShare
 * module. The class's existance, name, and content may change from
 * release to release without any promise of backwards compatability.
 *
 * @ingroup foldershare
 */
final class LimitUtilities {
 
  /*---------------------------------------------------------------------
   *
   * Functions - memory use.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Returns TRUE if this process is above the soft memory use limit.
   *
   * PHP's configuration defines a hard memory use limit. Processes that
   * reach this limit are automatically aborted. This will interrupt any
   * work being done and can leave operations and database state in an
   * indeterminate state.
   *
   * To avoid hitting the hard memory use limit, this module defines a
   * "soft memory use limit" as a percentage of the hard limit. The percentage
   * is a configuration parameter for the module and defaults to 80%.
   * Code that reaches this limit should find a way to gracefully end work
   * and postpone further work until another process run. If they do not,
   * they risk being aborted abruptly by PHP.
   *
   * This function queries the current process memory use and returns TRUE
   * if that usage is above the soft limit.
   *
   * If PHP's configured memory limit is (-1), for unlimited memory use,
   * this method always returns FALSE. However, it is not recommended practice
   * to configure a site's PHP interpreter without a memory limit.
   *
   * @return bool
   *   Returns TRUE if current memory use has exceeded the memory
   *   use considered safe for this process.
   *
   * @see ::getMemoryUseLimit()
   * @see ::getPhpMemoryUseLimit()
   * @see \Drupal\foldershare\Settings::getMemoryUseLimitPercentage()
   */
  public static function aboveMemoryUseLimit() {
 
    // Get the current soft memory use limit.
    $limit = self::getMemoryUseLimit();
 
    if ($limit === (-1) || memory_get_usage() <= $limit) {
      return FALSE;
    }
 
    return TRUE;
  }
 
  /**
   * Returns the configured PHP hard memory limit.
   *
   * The returned value is parsed from PHP's 'memory_limit' directive,
   * which is a string number that may include 'K', 'M', and 'G' suffixes
   * to indicate kilobytes, megabytes, or gigabytes. A typical value is 128M.
   *
   * Processes that use more than this hard limit will be abruptly aborted by
   * the PHP interpreter.
   *
   * A return value of (-1) indicates that there is no memory limit. However,
   * it is not recommended practice to configure a site's PHP interpreter
   * without a memory limit.
   *
   * @return int
   *   Returns PHP's configured hard memory use limit, or (-1) if there is
   *   no limit.
   *
   * @see ::aboveMemoryUseLimit()
   * @see ::getMemoryUseLimit()
   */
  public static function getPhpMemoryLimit() {
 
    // Get the memory limit string.
    $memoryLimit = ini_get('memory_limit');
    if ($memoryLimit === FALSE) {
      // The memory limit directive is not available? Assume unlimited.
      return (-1);
    }
 
    if (empty($memoryLimit) === TRUE) {
      // The memory limit was set to an empty string. Assume unlimited.
      return (-1);
    }
 
    if ($memoryLimit < 0) {
      // The memory limit was set to (-1) = unlimited, or some other
      // negative value that we'll treat as (-1).
      return (-1);
    }
 
    return Bytes::toInt($memoryLimit);
  }
 
  /**
   * Returns the soft memory use limit set for this process.
   *
   * The returned value is a soft limit that is less than PHP's hard
   * memory use limit. The soft limit value is computed by multiplying
   * PHP's limit by a percentage less than 100% that is configured in
   * this module's settings. A typical value is 80%.
   *
   * Unlike a hard limit, a soft limit is not monitored by the PHP interpreter.
   * Passing the limit will not cause the process to be aborted.
   * Instead, code may monitor memory use and check the soft limit to
   * decide when to gracefully stop and schedule further work to be done by
   * anothe rprocess.
   *
   * If PHP's configured memory limit is (-1), for unlimited memory use,
   * this method returns (-1).
   *
   * @return int
   *   Returns the memory use limit for this process, or (-1) if
   *   there is no limit.
   *
   * @see ::aboveMemoryUseLimit()
   * @see ::getPhpMemoryUseLimit()
   * @see \Drupal\foldershare\Settings::getMemoryUseLimitPercentage()
   */
  public static function getMemoryUseLimit() {
 
    // Cache the soft memory use limit since it does not change during
    // execution of a process. This avoids re-parsing the PHP memory limit,
    // getting the module's configured limit percentage, etc.
    static $memoryUseLimit = 0;
 
    if ($memoryUseLimit === 0) {
      // Get, parse, and return PHP's configured memory limit. A (-1)
      // means there is no limit.
      $memoryUseLimit = self::getPhpMemoryLimit();
 
      if ($memoryUseLimit !== (-1)) {
        // Calculate the module's own limit below PHP's based upon a
        // memory use limit percentage.
        $memoryUseLimit = (int) ($memoryUseLimit *
          Settings::getMemoryUseLimitPercentage());
        if ($memoryUseLimit === 0) {
          $memoryUseLimit = (-1);
        }
      }
    }
 
    return $memoryUseLimit;
  }
 
  /*---------------------------------------------------------------------
   *
   * Functions - execution time.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Returns TRUE if this process is above the soft execution time limit.
   *
   * PHP's configuration defines a hard execution time limit. Processes that
   * reach this limit are automatically aborted. This will interrupt any
   * work being done and can leave operations and database state in an
   * indetermine state.
   *
   * To avoid hitting the hard execution time limit, this module defines a
   * "soft execution time limit", expressed as a percentage of the hard limit.
   * typical percentage is 80%. Code that reaches this limit should find a
   * way to gracefully end work and postpone further work until another
   * process run. If they do not, they risk being aborted abruptly by PHP.
   *
   * This function queries the current process execution time and returns
   * TRUE if that time is above the soft limit.
   *
   * If PHP's configured execution time limit is 0, for unlimited time,
   * this method always returns FALSE.
   *
   * @return bool
   *   Returns TRUE if current exeuction time has exceeded the
   *   execution time considered safe for this process.
   *
   * @see ::aboveResponseExecutionTimeLimit()
   * @see ::getExecutionTimeLimit()
   * @see ::getPhpExecutionTimeLimit()
   * @see \Drupal\foldershare\Settings::getExecutionTimeLimitPercentage()
   */
  public static function aboveExecutionTimeLimit() {
 
    $limit = self::getExecutionTimeLimit();
    if ($limit <= 0) {
      return FALSE;
    }
 
    $elapsedTime = time() - (int) $_SERVER['REQUEST_TIME'];
    return ($elapsedTime > $limit);
  }
 
  /**
   * Returns TRUE if this process is above the responsiveness time limit.
   *
   * PHP's configuration defines a hard execution time limit. Processes that
   * reach this limit are automatically aborted. This will interrupt any
   * work being done and can leave operations and database state in an
   * indetermine state.
   *
   * A typical PHP configuration uses a 30 second execution time limit.
   * However, this is far too long to make a user wait for a response.
   * Responsive sites aim at response times of 5 seconds or less.
   *
   * To avoid hitting the hard execution time limit, or exceeding a reasonable
   * responsiveness time limit, this module defines two "soft execution time
   * limits". The first is expressed as a percentage of the hard limit. The
   * second is a maximum time, in seconds, considered reasonable for
   * responsiveness. A typical percentage is 80%, while a typical responsiveness
   * limit is 5 seconds. Code that reaches either limit should find a way to
   * gracefully end work and postpone further work until another process run.
   * If they do not, they risk a sluggish response to the user or being
   * aborted abruptly by PHP.
   *
   * This function queries the current process execution time and returns
   * TRUE if that time is above either the soft limit or the responsiveness
   * limit, whichever is less.
   *
   * If PHP's configured execution limit is (-1), for unlimited run time,
   * this method still checks the responsiveness limit.
   *
   * @return bool
   *   Returns TRUE if current exeuction time has exceeded the execution time
   *   limit considered safe for this process, or the responsiveness limit
   *   recommended for responsive sites.
   *
   * @see ::getExecutionTimeLimit()
   * @see ::aboveExecutionTimeLimit()
   * @see \Drupal\foldershare\Settings::getResponseExecutionTimeLimit()
   */
  public static function aboveResponseExecutionTimeLimit() {
 
    // Get the current soft execution time limit and responsiveness limit.
    $limit = Settings::getResponseExecutionTimeLimit();
    $maxLimit = self::getExecutionTimeLimit();
 
    // If the maximum execution time limit is actually shorter than the
    // responsiveness limit (which is odd), then use it as the limit.
    if ($maxLimit > 0 && $limit > $maxLimit) {
      $limit = $maxLimit;
    }
 
    $elapsedTime = time() - (int) $_SERVER['REQUEST_TIME'];
    return ($elapsedTime > $limit);
  }
 
  /**
   * Returns the configured PHP hard execution time limit.
   *
   * The returned value is parsed from PHP's 'max_execution_time' directive,
   * which is a time in seconds. A typical value is 30 seconds.
   *
   * Processes that use more than this hard limit will be abruptly aborted by
   * the PHP interpreter.
   *
   * A return value of 0 indicates that there is no execution time limit.
   * This is common for code invoked from the command line, including by
   * drush.
   *
   * @return int
   *   Returns PHP's configured hard execution time limit, or 0 if there is
   *   no limit.
   *
   * @see ::aboveExecutionTimeLimit()
   * @see ::getExecutionTimeLimit()
   */
  public static function getPhpExecutionTimeLimit() {
 
    // Get the limit string.
    $executionTimeLimit = ini_get('max_execution_time');
    if ($executionTimeLimit === FALSE) {
      // The execution time limit directive is not available? Assume unlimited.
      return 0;
    }
 
    $executionTimeLimit = (int) $executionTimeLimit;
    return ($executionTimeLimit < 0) ? 0 : $executionTimeLimit;
  }
 
  /**
   * Returns the soft execution time limit set for this process.
   *
   * The returned value is a soft limit that is less than PHP's hard
   * execution time limit. The soft limit value is computed by multiplying
   * PHP's limit by a percentage less than 100% that is configured in
   * the module's settings. A typical value is 80%.
   *
   * Unlike a hard limit, a soft limit is not monitored by the PHP interpreter
   * and passing the limit will not cause the process to be aborted.
   * Instead, code may monitor execution time and check the soft limit to
   * decide when to gracefully stop what they are doing and schedule a
   * background task to finish the work.
   *
   * If PHP's configured memory limit is 0, for unlimited execution time,
   * this method returns 0.
   *
   * @return int
   *   Returns the soft execution time limit for this process, or 0 if there
   *   is no limit.
   *
   * @see ::aboveExecutionTimeLimit()
   * @see ::getPhpExecutionTimeLimit()
   * @see \Drupal\foldershare\Settings::getExecutionTimeLimitPercentage()
   */
  public static function getExecutionTimeLimit() {
 
    // Cache the soft execution time limit since it does not change during
    // execution of a process. This avoids re-parsing the PHP time limit,
    // getting the module's configured limit percentage, etc.
    static $executionTimeLimit = (-1);
 
    if ($executionTimeLimit === (-1)) {
      $executionTimeLimit = (float) self::getPhpExecutionTimeLimit() *
        Settings::getExecutionTimeLimitPercentage();
    }
 
    return $executionTimeLimit;
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc