~tcuthbert/wordpress/openstack-objectstorage-k8s

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
<?php
namespace GuzzleHttp;

use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\EndEvent;
use GuzzleHttp\Exception\StateException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\FutureResponse;
use GuzzleHttp\Message\MessageFactoryInterface;
use GuzzleHttp\Ring\Future\FutureInterface;

/**
 * Responsible for transitioning requests through lifecycle events.
 */
class RequestFsm
{
    private $handler;
    private $mf;
    private $maxTransitions;

    public function __construct(
        callable $handler,
        MessageFactoryInterface $messageFactory,
        $maxTransitions = 200
    ) {
        $this->mf = $messageFactory;
        $this->maxTransitions = $maxTransitions;
        $this->handler = $handler;
    }

    /**
     * Runs the state machine until a terminal state is entered or the
     * optionally supplied $finalState is entered.
     *
     * @param Transaction $trans      Transaction being transitioned.
     *
     * @throws \Exception if a terminal state throws an exception.
     */
    public function __invoke(Transaction $trans)
    {
        $trans->_transitionCount = 0;

        if (!$trans->state) {
            $trans->state = 'before';
        }

        transition:

        if (++$trans->_transitionCount > $this->maxTransitions) {
            throw new StateException("Too many state transitions were "
                . "encountered ({$trans->_transitionCount}). This likely "
                . "means that a combination of event listeners are in an "
                . "infinite loop.");
        }

        switch ($trans->state) {
            case 'before': goto before;
            case 'complete': goto complete;
            case 'error': goto error;
            case 'retry': goto retry;
            case 'send': goto send;
            case 'end': goto end;
            default: throw new StateException("Invalid state: {$trans->state}");
        }

        before: {
            try {
                $trans->request->getEmitter()->emit('before', new BeforeEvent($trans));
                $trans->state = 'send';
                if ((bool) $trans->response) {
                    $trans->state = 'complete';
                }
            } catch (\Exception $e) {
                $trans->state = 'error';
                $trans->exception = $e;
            }
            goto transition;
        }

        complete: {
            try {
                if ($trans->response instanceof FutureInterface) {
                    // Futures will have their own end events emitted when
                    // dereferenced.
                    return;
                }
                $trans->state = 'end';
                $trans->response->setEffectiveUrl($trans->request->getUrl());
                $trans->request->getEmitter()->emit('complete', new CompleteEvent($trans));
            } catch (\Exception $e) {
                $trans->state = 'error';
                $trans->exception = $e;
            }
            goto transition;
        }

        error: {
            try {
                // Convert non-request exception to a wrapped exception
                $trans->exception = RequestException::wrapException(
                    $trans->request, $trans->exception
                );
                $trans->state = 'end';
                $trans->request->getEmitter()->emit('error', new ErrorEvent($trans));
                // An intercepted request (not retried) transitions to complete
                if (!$trans->exception && $trans->state !== 'retry') {
                    $trans->state = 'complete';
                }
            } catch (\Exception $e) {
                $trans->state = 'end';
                $trans->exception = $e;
            }
            goto transition;
        }

        retry: {
            $trans->retries++;
            $trans->response = null;
            $trans->exception = null;
            $trans->state = 'before';
            goto transition;
        }

        send: {
            $fn = $this->handler;
            $trans->response = FutureResponse::proxy(
                $fn(RingBridge::prepareRingRequest($trans)),
                function ($value) use ($trans) {
                    RingBridge::completeRingResponse($trans, $value, $this->mf, $this);
                    $this($trans);
                    return $trans->response;
                }
            );
            return;
        }

        end: {
            $trans->request->getEmitter()->emit('end', new EndEvent($trans));
            // Throw exceptions in the terminal event if the exception
            // was not handled by an "end" event listener.
            if ($trans->exception) {
                if (!($trans->exception instanceof RequestException)) {
                    $trans->exception = RequestException::wrapException(
                        $trans->request, $trans->exception
                    );
                }
                throw $trans->exception;
            }
        }
    }
}