1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/platform/basic.h"
32
33 #include "mongo/base/status.h"
34 #include "mongo/transport/ticket.h"
35 #include "mongo/transport/ticket_impl.h"
36 #include "mongo/transport/transport_layer.h"
37
38 namespace mongo {
39 namespace transport {
40
41 const Date_t Ticket::kNoExpirationDate{Date_t::max()};
42
43 Status Ticket::ExpiredStatus = Status(ErrorCodes::ExceededTimeLimit, "Ticket has expired.");
44
45 Status Ticket::SessionClosedStatus =
46 Status(ErrorCodes::TransportSessionClosed, "Ticket's Session is closed.");
47
Ticket(TransportLayer * tl,std::unique_ptr<TicketImpl> ticket)48 Ticket::Ticket(TransportLayer* tl, std::unique_ptr<TicketImpl> ticket)
49 : _tl(tl), _ticket(std::move(ticket)) {}
50
Ticket(Status status)51 Ticket::Ticket(Status status) : _status(status) {}
52
53 Ticket::~Ticket() = default;
54
55 Ticket::Ticket(Ticket&&) = default;
56 Ticket& Ticket::operator=(Ticket&&) = default;
57
wait()58 Status Ticket::wait()&& {
59 // If the ticket is invalid then _tl is a nullptr and we should return early.
60 if (!valid())
61 return status();
62
63 invariant(_tl);
64 return _tl->wait(std::move(*this));
65 }
66
asyncWait(TicketCallback cb)67 void Ticket::asyncWait(TicketCallback cb)&& {
68 // If the ticket is invalid then _tl is a nullptr and we should return early.
69 if (!valid()) {
70 cb(status());
71 return;
72 }
73
74 invariant(_tl);
75 return _tl->asyncWait(std::move(*this), std::move(cb));
76 }
77
valid()78 bool Ticket::valid() {
79 return _status == Status::OK() && !expired();
80 }
81
status() const82 Status Ticket::status() const {
83 return _status;
84 }
85
expired()86 bool Ticket::expired() {
87 bool expired = expiration() <= Date_t::now();
88 if (_status == Status::OK() && expired) {
89 _status = Status(ErrorCodes::ExceededTimeLimit, "Ticket has expired.");
90 }
91 return expired;
92 }
93
94 } // namespace transport
95 } // namespace mongo
96