Skip to content

Commit

Permalink
Removing more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Apr 17, 2023
1 parent 228ca77 commit d216733
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 30 deletions.
21 changes: 17 additions & 4 deletions Sources/Vapor/Authentication/AuthenticationCache.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import NIOConcurrencyHelpers

extension Request {
/// Helper for accessing authenticated objects.
/// See `Authenticator` for more information.
Expand Down Expand Up @@ -56,18 +58,29 @@ extension Request.Authentication {
return self.get(A.self) != nil
}

private final class Cache {
// Internal `storage` is behind a lock so this can be unchecked Sendable
private final class Cache: @unchecked Sendable {
private var storage: [ObjectIdentifier: Any]

private let storageLock: NIOLock

init() {
self.storageLock = .init()
self.storage = [:]
}

internal subscript<A>(_ type: A.Type) -> A?
where A: Authenticatable
{
get { return storage[ObjectIdentifier(A.self)] as? A }
set { storage[ObjectIdentifier(A.self)] = newValue }
get {
storageLock.withLock {
return storage[ObjectIdentifier(A.self)] as? A
}
}
set {
storageLock.withLockVoid {
storage[ObjectIdentifier(A.self)] = newValue
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Concurrency/AsyncBasicResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIOCore
/// A basic, async closure-based `Responder`.
public struct AsyncBasicResponder: Sendable, AsyncResponder {
/// The stored responder closure.
private let closure: (Request) async throws -> Response
private let closure: @Sendable (Request) async throws -> Response

/// Create a new `BasicResponder`.
///
Expand All @@ -15,7 +15,7 @@ public struct AsyncBasicResponder: Sendable, AsyncResponder {
/// - parameters:
/// - closure: Responder closure.
public init(
closure: @escaping (Request) async throws -> Response
closure: @Sendable @escaping (Request) async throws -> Response
) {
self.closure = closure
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation

extension AsyncPasswordHasher {
public func hash<Password>(_ password: Password) async throws -> [UInt8]
where Password: DataProtocol
where Password: DataProtocol & Sendable
{
try await self.hash(password).get()
}
Expand All @@ -12,7 +12,7 @@ extension AsyncPasswordHasher {
_ password: Password,
created digest: Digest
) async throws -> Bool
where Password: DataProtocol, Digest: DataProtocol
where Password: DataProtocol & Sendable, Digest: DataProtocol & Sendable
{
try await self.verify(password, created: digest).get()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Concurrency/AsyncSessionDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIOCore
/// Capable of managing CRUD operations for `Session`s.
///
/// This is an async version of `SessionDriver`
public protocol AsyncSessionDriver: SessionDriver {
public protocol AsyncSessionDriver: SessionDriver, Sendable {
func createSession(_ data: SessionData, for request: Request) async throws -> SessionID
func readSession(_ sessionID: SessionID, for request: Request) async throws -> SessionData?
func updateSession(_ sessionID: SessionID, to data: SessionData, for request: Request) async throws -> SessionID
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Content/ContainerGetPathExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Decodes nested single values from data at a key path.
internal struct ContainerGetPathExecutor<D: Decodable>: Decodable, Sendable {
internal struct ContainerGetPathExecutor<D: Decodable>: Decodable {
let result: D

static func userInfo(for keyPath: [CodingKey]) -> [CodingUserInfoKey: Any] {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Content/ContentCoders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIOCore
import NIOHTTP1

/// Conform a type to this protocol to make it usable for encoding data via Vapor's ``ContentConfiguration`` system.
public protocol ContentEncoder {
public protocol ContentEncoder: Sendable {
/// Legacy "encode object" method. The provided encodable object's contents must be stored in the provided
/// ``NIOCore/ByteBuffer``, and any appropriate headers for the type of the content may be stored in the provided
/// ``NIOHTTP1/HTTPHeaders``.
Expand All @@ -26,7 +26,7 @@ public protocol ContentEncoder {
}

/// Conform a type to this protocol to make it usable for decoding data via Vapor's ``ContentConfiguration`` system.
public protocol ContentDecoder {
public protocol ContentDecoder: Sendable {
/// Legacy "decode object" method. The provided ``NIOCore/ByteBuffer`` should be decoded as a vaule of the given
/// type, optionally guided by the provided ``NIOHTTP1/HTTPHeaders``.
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Content/URLQueryCoders.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
public protocol URLQueryDecoder {
public protocol URLQueryDecoder: Sendable {
func decode<D>(_ decodable: D.Type, from url: URI) throws -> D
where D: Decodable

func decode<D>(_ decodable: D.Type, from url: URI, userInfo: [CodingUserInfoKey: Any]) throws -> D
where D: Decodable
}

public protocol URLQueryEncoder {
public protocol URLQueryEncoder: Sendable {
func encode<E>(_ encodable: E, to url: inout URI) throws
where E: Encodable

Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/HTTP/BasicResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIOCore
/// A basic, closure-based `Responder`.
public struct BasicResponder: Responder, Sendable {
/// The stored responder closure.
private let closure: (Request) throws -> EventLoopFuture<Response>
private let closure: @Sendable (Request) throws -> EventLoopFuture<Response>

/// Create a new `BasicResponder`.
///
Expand All @@ -15,7 +15,7 @@ public struct BasicResponder: Responder, Sendable {
/// - parameters:
/// - closure: Responder closure.
public init(
closure: @escaping (Request) throws -> EventLoopFuture<Response>
closure: @Sendable @escaping (Request) throws -> EventLoopFuture<Response>
) {
self.closure = closure
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Passwords/Application+Passwords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension Application {
}

public func use(
_ makeVerifier: @escaping (Application) -> (PasswordHasher)
_ makeVerifier: @Sendable @escaping (Application) -> (PasswordHasher)
) {
self.storage.makeVerifier = makeVerifier
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Passwords/AsyncPasswordHasher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public struct AsyncPasswordHasher: Sendable {
self.eventLoop = eventLoop
}

public func hash<Password: Sendable>(_ password: Password) -> EventLoopFuture<[UInt8]>
where Password: DataProtocol
public func hash<Password>(_ password: Password) -> EventLoopFuture<[UInt8]>
where Password: DataProtocol & Sendable
{
return self.threadPool.runIfActive(eventLoop: self.eventLoop) {
try self.hasher.hash(password)
Expand Down
59 changes: 52 additions & 7 deletions Sources/Vapor/Sessions/Session.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,71 @@
import NIOConcurrencyHelpers

/// Sessions are a method for associating data with a client accessing your app.
///
/// Each session has a unique identifier that is used to look it up with each request
/// to your app. This is usually done via HTTP cookies.
///
/// See `Request.session()` and `SessionsMiddleware` for more information.
public final class Session: Sendable {
public final class Session: @unchecked Sendable {
/// This session's unique identifier. Usually a cookie value.
public var id: SessionID?
public var id: SessionID? {
get {
idLock.withLock {
return _id
}
}
set {
idLock.withLockVoid {
_id = newValue
}
}
}

/// This session's data.
public var data: SessionData
public var data: SessionData {
get {
dataLock.withLock {
return _data
}
}
set {
dataLock.withLockVoid {
_data = newValue
}
}
}

/// `true` if this session is still valid.
var isValid: Bool
var isValid: Bool {
get {
isValidLock.withLock {
return _isValid
}
}
set {
isValidLock.withLockVoid {
_isValid = newValue
}
}
}

private var _id: SessionID?
private var _data: SessionData
private var _isValid: Bool
private let idLock: NIOLock
private let dataLock: NIOLock
private let isValidLock: NIOLock

/// Create a new `Session`.
///
/// Normally you will use `Request.session()` to do this.
public init(id: SessionID? = nil, data: SessionData = .init()) {
self.id = id
self.data = data
self.isValid = true
self.idLock = .init()
self.dataLock = .init()
self.isValidLock = .init()
self._id = id
self._data = data
self._isValid = true
}

/// Invalidates the current session, removing persisted data from the session driver
Expand Down
24 changes: 21 additions & 3 deletions Sources/Vapor/Sessions/SessionCache.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import NIOConcurrencyHelpers

/// Singleton service cache for a `Session`. Used with a message's private container.
internal final class SessionCache {
internal final class SessionCache: @unchecked Sendable {
/// Set to `true` when passing through middleware.
// This is only ever set in the middleware so doesn't need a lock
var middlewareFlag: Bool

/// The cached session.
var session: Session?
var session: Session? {
get {
sessionLock.withLock {
return _session
}
}
set {
sessionLock.withLockVoid {
_session = newValue
}
}
}

private let sessionLock: NIOLock
private var _session: Session?

/// Creates a new `SessionCache`.
init(session: Session? = nil) {
self.session = session
self._session = session
self.sessionLock = .init()
self.middlewareFlag = false
}
}
2 changes: 1 addition & 1 deletion Sources/Vapor/Validation/Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct Validation: Sendable {
}
}

init(nested key: ValidationKey, required: Bool, unkeyed factory: @escaping (Int, inout Validations) -> (), customFailureDescription: String?) {
init(nested key: ValidationKey, required: Bool, unkeyed factory: @Sendable @escaping (Int, inout Validations) -> (), customFailureDescription: String?) {
self.init(
key: key,
valuelessKeyBehavior: required ? .missing : .skipAlways,
Expand Down

0 comments on commit d216733

Please sign in to comment.