Skip to content

Commit

Permalink
feat: add sendable conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Dec 28, 2023
1 parent 8e45fd9 commit 917ade0
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 29 deletions.
20 changes: 20 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-log-loki open source project
//
// Copyright (c) 2023 Timo Zacherl
// Licensed under MIT
//
// See LICENSE for license information
//
// SPDX-License-Identifier: MIT
//
//===----------------------------------------------------------------------===//

This product contains a derivation of the NIOLock implementation
from Swift NIO.

* LICENSE (Apache License 2.0):
* https://www.apache.org/licenses/LICENSE-2.0
* HOMEPAGE:
* https://github.com/apple/swift-nio
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/Batch.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Batch {
struct Batch: Sendable {
var entries: [BatchEntry]

let createdAt = Date()
Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/BatchEntry.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct BatchEntry {
struct BatchEntry: Sendable {
var labels: LokiLabels
var logEntries: [LokiLog]
}
42 changes: 21 additions & 21 deletions Sources/LoggingLoki/Batcher.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

class Batcher {
final class Batcher: Sendable {
private let session: LokiSession
private let headers: [String: String]

Expand All @@ -10,9 +10,7 @@ class Batcher {
private let batchSize: Int
private let maxBatchTimeInterval: TimeInterval?

private var currentTimer: Timer? = nil

var batch: Batch? = nil
let batch: NIOLockedValueBox<Batch?> = NIOLockedValueBox(nil)

init(session: LokiSession,
headers: [String: String],
Expand All @@ -29,29 +27,31 @@ class Batcher {
}

func addEntryToBatch(_ log: LokiLog, with labels: LokiLabels) {
if var batch {
batch.addEntry(log, with: labels)
self.batch = batch
} else {
var batch = Batch(entries: [])
batch.addEntry(log, with: labels)
self.batch = batch
self.batch.withLockedValue { batch in
if batch != nil {
batch!.addEntry(log, with: labels)
} else {
batch = Batch(entries: [])
batch!.addEntry(log, with: labels)
}
}
}

func sendBatchIfNeeded() {
guard let batch else { return }
self.batch.withLockedValue { safeBatch in
guard let batch = safeBatch else { return }

if let maxBatchTimeInterval, batch.createdAt.addingTimeInterval(maxBatchTimeInterval) < Date() {
sendBatch(batch)
self.batch = nil
return
}
if let maxBatchTimeInterval, batch.createdAt.addingTimeInterval(maxBatchTimeInterval) < Date() {
sendBatch(batch)
safeBatch = nil
return
}

if batch.totalLogEntries >= batchSize {
sendBatch(batch)
self.batch = nil
return
if batch.totalLogEntries >= batchSize {
sendBatch(batch)
safeBatch = nil
return
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiError.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import struct Foundation.Data
import protocol Foundation.LocalizedError

enum LokiError: LocalizedError {
enum LokiError: LocalizedError, Sendable {
case invalidResponse(Data?)

var errorDescription: String? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FoundationNetworking
import Logging

/// ``LokiLogHandler`` is a logging backend for `Logging`.
public struct LokiLogHandler: LogHandler {
public struct LokiLogHandler: LogHandler, Sendable {

internal let session: LokiSession

Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiRequest.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct LokiRequest: Encodable {
struct LokiRequest: Encodable, Sendable {
var streams: [LokiStream]

static func fromBatch(_ batch: Batch) -> LokiRequest {
Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FoundationNetworking
import Logging
import Snappy

protocol LokiSession {
protocol LokiSession: Sendable {
func send(_ batch: Batch,
url: URL,
headers: [String: String],
Expand Down
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiStream.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import class Foundation.NumberFormatter
import class Foundation.NSNumber

struct LokiStream: Encodable {
struct LokiStream: Encodable, Sendable {
var stream: Dictionary<String, String>
var values: Array<Array<String>>

Expand Down
Loading

0 comments on commit 917ade0

Please sign in to comment.