Closed grundoon closed 4 years ago
What about just a normal userInfo
style dictionary? The app
is only intended to be used on its event loop (app.eventLoop
). So there shouldn't be a need to make the dictionary thread-safe.
Besides that small thing, I like this idea a lot π
Think we should close this in favor of #1822
Part of the original thinking here was that the RFC1123 date for the header was being unnecessarily potentially recalculated on every Response, and could instead be cached upstream on the event loop itself. I don't know that #1822 addresses this type of use case.
On master branch, the Date
header is now calculated once per event loop per second using a scheduled task. Each response now just reads whatever the current value is: https://github.com/vapor/http/blob/master/Sources/HTTPKit/Utilities/RFC1123.swift#L70
Veering off to the side for a moment, should that scheduleRepeatedTask
not have a finer-grained initialDelay
value in order to closely align it with an actual second flip-over boundary?
That would be a nice feature to add π
Sorry, don't have 5.0 in this box's toolchain so can't really play today, but are you good with something like
let fracSeconds = 1.0 - Date().timeIntervalSince1970.truncatingRemainder(dividingBy: 1)
let msToNextSecond = Int(fracSeconds * 1000) + 1
eventLoop.scheduleRepeatedTask(initialDelay: .milliseconds(msToNextSecond), delay: .seconds(1)) { task in
new.updateTimestamp()
}
or should we be looking more along the lines of using clock_gettime
?
@grundoon yeah that looks good to me. Are you gonna submit a PR?
Well, if this would ever finish I could maybe get to actually testing... π€£
grundoon:http grundoon$ swift --version
Apple Swift version 5.0 (swiftlang-1001.0.60.3 clang-1001.0.37.8)
Target: x86_64-apple-darwin18.2.0
ABI version: 0.7
grundoon:http grundoon$ swift package update
Fetching https://github.com/apple/swift-nio.git
Fetching https://github.com/tanner0101/swift-nio-ssl.git
β
Track here: https://github.com/vapor/async-kit/issues/8
Filing for discussion.
Motivation is primarily to allow a standard and straightforward method of creating
ThreadSpecificVariable
s for use onapp.eventLoop
(rather than have to modify each app separately).For example, scheduling a repeating task via the main eventLoop often uses a pattern such as
Which, while simple, encourages discarding the
Scheduled<T>
result because there's no obvious place to store it without modifying the stock Vapor Application itself (or pushing that struct into persistent storage, ick). Without storage, cancelling the task requires additional logic to determine whether the recursion should be executed, and regardless will only take effect after the current scheduled task has completed, at some future(in:)
point.Adding a
ThreadSpecificVariable
of typeDictionary<String, Any> = [:]
(ob bikeshedding: say, nameduserDict
here) would allow for scenarios such asOther considerations:
EventLoop
s (the pool, not just justapp.eventLoop
) would facilitate moving such things as theRFC1123
date header cache out of the responder chain, to a more efficient home.