Skip to content

Commit ed50014

Browse files
authored
Adapt Service protocol from swift-service-lifecycle (#37)
* closes #36 * shorer init
1 parent 1808018 commit ed50014

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let package = Package(
3232
dependencies: [
3333
.package(url: "https://github.com/apple/swift-nio.git", from: "2.56.0"),
3434
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
35+
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
3536
],
3637
targets: [
3738
.target(
@@ -41,6 +42,7 @@ let package = Package(
4142
.product(name: "NIOPosix", package: "swift-nio"),
4243
.product(name: "NIOEmbedded", package: "swift-nio"),
4344
.product(name: "Logging", package: "swift-log"),
45+
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
4446
]
4547
),
4648
.testTarget(

Sources/SwiftMemcache/MemcachedConnection.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
import NIOCore
1717
import NIOPosix
18+
import ServiceLifecycle
1819

1920
/// An actor to create a connection to a Memcache server.
2021
///
2122
/// This actor can be used to send commands to the server.
22-
public actor MemcachedConnection {
23+
public actor MemcachedConnection: Service {
2324
private typealias StreamElement = (MemcachedRequest, CheckedContinuation<MemcachedResponse, Error>)
2425
private let host: String
2526
private let port: Int

Sources/swift-memcache-gsoc-example/Program.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,38 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Logging
1516
import NIOCore
1617
import NIOPosix
18+
import ServiceLifecycle
1719
import SwiftMemcache
1820

1921
@main
2022
struct Program {
21-
// Create an event loop group with a single thread
22-
static let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
23+
// Use the shared singleton instance of MultiThreadedEventLoopGroup
24+
static let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
25+
// Initialize the logger
26+
static let logger = Logger(label: "memcache")
2327

2428
static func main() async throws {
25-
// Instantiate a new MemcachedConnection actor with host, port, and event loop group
26-
let memcachedConnection = MemcachedConnection(host: "127.0.0.1", port: 11211, eventLoopGroup: eventLoopGroup)
29+
// Instantiate a new MemcacheConnection actor with host, port, and event loop group
30+
let memcacheConnection = MemcachedConnection(host: "127.0.0.1", port: 11211, eventLoopGroup: eventLoopGroup)
31+
32+
// Initialize the service group
33+
let serviceGroup = ServiceGroup(services: [memcacheConnection], logger: self.logger)
2734

2835
try await withThrowingTaskGroup(of: Void.self) { group in
2936
// Add the connection actor's run function to the task group
3037
// This opens the connection and handles requests until the task is cancelled or the connection is closed
31-
group.addTask { try await memcachedConnection.run() }
38+
group.addTask { try await serviceGroup.run() }
3239

3340
// Set a value for a key.
3441
let setValue = "bar"
35-
try await memcachedConnection.set("foo", value: setValue)
42+
try await memcacheConnection.set("foo", value: setValue)
3643

3744
// Get the value for a key.
3845
// Specify the expected type for the value returned from Memcache.
39-
let getValue: String? = try await memcachedConnection.get("foo")
46+
let getValue: String? = try await memcacheConnection.get("foo")
4047

4148
// Assert that the get operation was successful by comparing the value set and the value returned from Memcache.
4249
// If they are not equal, this will throw an error.

0 commit comments

Comments
 (0)