When writing services or function there are some things that need to be known:

  1. Multi threading
  2. Logging
  3. Filemanager
  4. Global data


Both services and functions run in a multithreaded environment. Hence all code must be thread safe.

Code is made thread safe by making sure that all global data is accessed through a synchronisation mechanism. In Swift that is done through Grand Central Dispatch (GCD).

A short example on how to do this is shown below:

final class GlobalData {
    private static let queue = DispatchQueue(
        label: "GlobalData",
        qos: DispatchQoS.default,
        attributes: DispatchQueue.Attributes(),
        autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
        target: nil)
    private static var data: Int = 0
    public func setData(_ data: Int) {
        GlobalData.queue.sync { GlobalData.data = data }
    public func getData() -> Int {
        return GlobalData.queue.sync(execute: { return GlobalData.data })

Mistakes made can show up in crashes of the entire application or in users receiving websites that are incorrect, or even intended for somebody else.


Swiftfire has a logger that can be used by custom functions and sequences also.

The typical use is:

Log.atDebug?.log(id: connection.logId, source: #file.source(#function, #line), message: <...>)

Of course the <…> should be replaced by whatever should be logged.

There are several logging levels available. Read more about these in the readme


Due to multi threading, the default filemanager should not be used. Since creating a filemanager is expensive (time & resources) a filemanager is provided in the “connection” object.

Every SFConnection has a “filemanager” entry that can be used instead of the default manager.

For sequences:

let data = connection.filemanager.contents(atPath: resourcePath)

For functions:

let data = environment.connection.filemanager.contents(atPath: resourcePath)

Global data

Swiftfire has a number of global data items. While they are not intended for use by the sequences or functions it is necessary to be aware that they exist to prevent name clashes producing unexpected results.

The following global objects exist:

(For Swiftfire v0.10.9)

  • parameters
  • logforwarder
  • serverBlacklist
  • statistics
  • services
  • functions
  • headerLogger
  • domains
  • connectionPool
  • telemetry
  • httpServerAcceptQueue
  • httpsServerAcceptQueue
  • httpServer
  • httpsServer
  • consoleServerCertificateFound