Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Redis\Limiters;
4
 
5
use Illuminate\Contracts\Redis\LimiterTimeoutException;
6
use Illuminate\Support\InteractsWithTime;
7
 
8
class ConcurrencyLimiterBuilder
9
{
10
    use InteractsWithTime;
11
 
12
    /**
13
     * The Redis connection.
14
     *
15
     * @var \Illuminate\Redis\Connections\Connection
16
     */
17
    public $connection;
18
 
19
    /**
20
     * The name of the lock.
21
     *
22
     * @var string
23
     */
24
    public $name;
25
 
26
    /**
27
     * The maximum number of entities that can hold the lock at the same time.
28
     *
29
     * @var int
30
     */
31
    public $maxLocks;
32
 
33
    /**
34
     * The number of seconds to maintain the lock until it is automatically released.
35
     *
36
     * @var int
37
     */
38
    public $releaseAfter = 60;
39
 
40
    /**
41
     * The amount of time to block until a lock is available.
42
     *
43
     * @var int
44
     */
45
    public $timeout = 3;
46
 
47
    /**
48
     * The number of milliseconds to wait between attempts to acquire the lock.
49
     *
50
     * @var int
51
     */
52
    public $sleep = 250;
53
 
54
    /**
55
     * Create a new builder instance.
56
     *
57
     * @param  \Illuminate\Redis\Connections\Connection  $connection
58
     * @param  string  $name
59
     * @return void
60
     */
61
    public function __construct($connection, $name)
62
    {
63
        $this->name = $name;
64
        $this->connection = $connection;
65
    }
66
 
67
    /**
68
     * Set the maximum number of locks that can be obtained per time window.
69
     *
70
     * @param  int  $maxLocks
71
     * @return $this
72
     */
73
    public function limit($maxLocks)
74
    {
75
        $this->maxLocks = $maxLocks;
76
 
77
        return $this;
78
    }
79
 
80
    /**
81
     * Set the number of seconds until the lock will be released.
82
     *
83
     * @param  int  $releaseAfter
84
     * @return $this
85
     */
86
    public function releaseAfter($releaseAfter)
87
    {
88
        $this->releaseAfter = $this->secondsUntil($releaseAfter);
89
 
90
        return $this;
91
    }
92
 
93
    /**
94
     * Set the amount of time to block until a lock is available.
95
     *
96
     * @param  int  $timeout
97
     * @return $this
98
     */
99
    public function block($timeout)
100
    {
101
        $this->timeout = $timeout;
102
 
103
        return $this;
104
    }
105
 
106
    /**
107
     * The number of milliseconds to wait between lock acquisition attempts.
108
     *
109
     * @param  int  $sleep
110
     * @return $this
111
     */
112
    public function sleep($sleep)
113
    {
114
        $this->sleep = $sleep;
115
 
116
        return $this;
117
    }
118
 
119
    /**
120
     * Execute the given callback if a lock is obtained, otherwise call the failure callback.
121
     *
122
     * @param  callable  $callback
123
     * @param  callable|null  $failure
124
     * @return mixed
125
     *
126
     * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
127
     */
128
    public function then(callable $callback, callable $failure = null)
129
    {
130
        try {
131
            return (new ConcurrencyLimiter(
132
                $this->connection, $this->name, $this->maxLocks, $this->releaseAfter
133
            ))->block($this->timeout, $callback, $this->sleep);
134
        } catch (LimiterTimeoutException $e) {
135
            if ($failure) {
136
                return $failure($e);
137
            }
138
 
139
            throw $e;
140
        }
141
    }
142
}