Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix concurrency warning #12

Merged
merged 3 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Demo.swiftpm/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down Expand Up @@ -36,8 +36,8 @@ let package = Package(
.executableTarget(
name: "AppModule",
dependencies: [
.productItem(name: "Parchment", package: "Parchment", condition: nil),
.productItem(name: "ParchmentDefault", package: "Parchment", condition: nil),
.product(name: "Parchment", package: "Parchment"),
.product(name: "ParchmentDefault", package: "Parchment"),
]
)
]
Expand Down
13 changes: 6 additions & 7 deletions Demo.swiftpm/Sources/AppModule/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ extension LoggerComponentID {
}

struct MyLogger: LoggerComponent {
static var id: LoggerComponentID = .my
static let id: LoggerComponentID = .my

func send(_ log: [LoggerSendable]) async -> Bool {
print("send \(log)")
try? await Task.sleep(nanoseconds: 1000_000)
return true
}
}
Expand All @@ -36,14 +37,12 @@ struct ExampleAppApp: App {
WindowGroup {
Button("send event") {
Task {
await logger.send(.tap, with: .init(policy: .bufferingFirst))
await logger.send(event: .tap, with: .init(policy: .bufferingFirst))
}
}
.onAppear {
logger.startLogging()
Task {
await logger.send(.impletion("home"))
}
.task {
await logger.startLogging()
await logger.send(event: .impletion("home"))
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ let package = Package(
targets: ["ParchmentDefault"]
),
],
dependencies: [
// TODO: versioning
.package(url: "https://github.com/realm/SwiftLint", branch: "main"),
],
targets: [
.target(
name: "Parchment",
swiftSettings: [
.unsafeFlags([
"-strict-concurrency=complete"
])
],
plugins: [
.plugin(name: "SwiftLintPlugin", package: "SwiftLint")
]
),
.target(
Expand All @@ -39,9 +32,6 @@ let package = Package(
.unsafeFlags([
"-strict-concurrency=complete"
])
],
plugins: [
.plugin(name: "SwiftLintPlugin", package: "SwiftLint")
]
),
.testTarget(
Expand Down
4 changes: 2 additions & 2 deletions Sources/Parchment/BufferRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct BufferRecord: Loggable, LoggerSendable, Equatable {
public let id: String
public let destination: String
public let eventName: String
public let parameters: [String: Any]
public let parameters: [String: Sendable]
public let timestamp: Date

public init(id: String = UUID().uuidString, destination: String, event: Loggable, timestamp: Date) {
Expand All @@ -26,7 +26,7 @@ public struct BufferRecord: Loggable, LoggerSendable, Equatable {
id: String = UUID().uuidString,
destination: String,
eventName: String,
parameters: [String: Any],
parameters: [String: Sendable],
timestamp: Date
) {
self.id = id
Expand Down
5 changes: 2 additions & 3 deletions Sources/Parchment/BufferedEventFlushScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
import Foundation

public protocol BufferedEventFlushScheduler {
func schedule(with buffer: TrackingEventBufferAdapter) async -> AsyncThrowingStream<[BufferRecord], Error>
func cancel()
public protocol BufferedEventFlushScheduler: Sendable {
func schedule(with buffer: TrackingEventBuffer) async -> AsyncThrowingStream<[BufferRecord], Error>
}
5 changes: 4 additions & 1 deletion Sources/Parchment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by k-kohey on 2021/10/26.
//

@MainActor
public enum Configuration {
public static var debugMode = false
}
Expand All @@ -28,5 +29,7 @@ enum ConsoleLoggerCategory: String {
// }

func assertionIfDebugMode(_ msg: String) {
assert(Configuration.debugMode, msg)
Task { @MainActor in
assert(Configuration.debugMode, msg)
}
}
8 changes: 4 additions & 4 deletions Sources/Parchment/Loggable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

import Foundation

public protocol Loggable {
public protocol Loggable: Sendable {
var eventName: String { get }
var parameters: [String: Any] { get }
var parameters: [String: Sendable] { get }
}

public struct TrackingEvent: Loggable {
public let eventName: String
public let parameters: [String: Any]
public let parameters: [String: Sendable]

public init(eventName: String, parameters: [String: Any]) {
public init(eventName: String, parameters: [String: Sendable]) {
self.eventName = eventName
self.parameters = parameters
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/Parchment/LoggableDictonary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import Foundation

/// Experimental API

public typealias LoggableDictonary = [PartialKeyPath<Loggable>: Any]
public typealias LoggableDictonary = [PartialKeyPath<Loggable>: Sendable]

extension PartialKeyPath<Loggable>: @unchecked Sendable {}

extension LoggableDictonary: Loggable {
public var eventName: String {
self[\.eventName] as? String ?? ""
}

public var parameters: [String: Any] {
self[\.parameters] as? [String: Any] ?? [:]
public var parameters: [String: Sendable] {
self[\.parameters] as? [String: Sendable] ?? [:]
}
}
41 changes: 10 additions & 31 deletions Sources/Parchment/LoggerBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
//
import Foundation

struct DateProvider {
static var mock: Date?

static func current() -> Date {
Self.mock ?? .init()
}
}

public final class LoggerBundler {
public final actor LoggerBundler {
private var components: [any LoggerComponent]
private let buffer: TrackingEventBufferAdapter
private let buffer: TrackingEventBuffer
private let flushStrategy: BufferedEventFlushScheduler

public var configMap: [LoggerComponentID: Configuration] = [:]
Expand All @@ -30,7 +22,7 @@ public final class LoggerBundler {
loggingStrategy: some BufferedEventFlushScheduler
) {
self.components = components
self.buffer = .init(buffer)
self.buffer = buffer
flushStrategy = loggingStrategy
}

Expand All @@ -52,7 +44,7 @@ public final class LoggerBundler {
let record = BufferRecord(
destination: logger.id.value,
event: mutations.transform(event, id: logger.id),
timestamp: DateProvider.current()
timestamp: .init()
)
await dispatch([record], for: logger, with: option)
}
Expand Down Expand Up @@ -91,28 +83,15 @@ public final class LoggerBundler {
}
}

public func startLogging() {
loggingTask = Task { [weak self] in
guard let self = self else {
assertionIfDebugMode(
"LoggerBundler instance should been retained by any object due to log events definitely"
)
return
}
do {
for try await records in await self.flushStrategy.schedule(with: self.buffer) {
await self.bloadcast(records)
}
} catch {
// console()?.log("\(error.localizedDescription)")
@discardableResult
public func startLogging() -> Task<Void, Error> {
Task {
for try await records in await flushStrategy.schedule(with: buffer) {
await self.bloadcast(records)
}
}
}

func cancell() {
loggingTask?.cancel()
}

private func bloadcast(_ records: [BufferRecord]) async {
let recordEachLogger = Dictionary(grouping: records) { record in
record.destination
Expand Down Expand Up @@ -164,7 +143,7 @@ public extension LoggerBundler {
await send(event, with: option)
}

func send(event: [PartialKeyPath<Loggable>: Any], with option: LoggingOption = .init()) async {
func send(event: [PartialKeyPath<Loggable>: Sendable], with option: LoggingOption = .init()) async {
await send(event, with: option)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Parchment/LoggerComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public struct LoggerComponentID: Hashable {
}
}

public protocol LoggerSendable {
public protocol LoggerSendable: Sendable {
var event: Loggable { get }
var timestamp: Date { get }
}

public protocol LoggerComponent {
public protocol LoggerComponent: Sendable {
static var id: LoggerComponentID { get }
func send(_: [LoggerSendable]) async -> Bool
func send(_: [any LoggerSendable]) async -> Bool
}

extension LoggerComponent {
Expand Down
34 changes: 7 additions & 27 deletions Sources/Parchment/TrackingEventBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,14 @@

import Foundation

public protocol TrackingEventBuffer {
func save(_ event: [BufferRecord])
func load(limit: Int64) -> [BufferRecord]
func count() -> Int
public protocol TrackingEventBuffer: Sendable {
func save(_ event: [BufferRecord]) async
func load(limit: Int64) async -> [BufferRecord]
func count() async -> Int
}

extension TrackingEventBuffer {
func load() -> [BufferRecord] {
load(limit: -1)
}
}

public final actor TrackingEventBufferAdapter {
private let buffer: any TrackingEventBuffer

init(_ buffer: some TrackingEventBuffer) {
self.buffer = buffer
}

public func save(_ event: [BufferRecord]) {
buffer.save(event)
}

public func load(limit: Int64 = -1) -> [BufferRecord] {
buffer.load(limit: limit)
}

public func count() -> Int {
buffer.count()
public extension TrackingEventBuffer {
func load() async -> [BufferRecord] {
await load(limit: -1)
}
}
2 changes: 1 addition & 1 deletion Sources/ParchmentDefault/DeviceDataMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import UIKit

public struct DeviceDataMutation: Mutation {
private let deviceParams: [String: Any]
private let deviceParams: [String: Sendable]

@MainActor
public init(device: UIDevice) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParchmentDefault/LoggerBundler+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public extension LoggerBundler {
static func make(
components: [any LoggerComponent],
buffer: some TrackingEventBuffer = try! SQLiteBuffer(),
loggingStrategy: some BufferedEventFlushScheduler = RegularlyPollingScheduler.default
loggingStrategy: some BufferedEventFlushScheduler = RegularlyPollingScheduler(timeInterval: 60)
) -> LoggerBundler {
Self(components: components, buffer: buffer, loggingStrategy: loggingStrategy)
}
Expand Down
Loading