Skip to content

Commit

Permalink
use defer to shutdown the HTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnybremer authored Jul 4, 2024
1 parent 66dac44 commit 427ef17
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Examples/PrintPDF/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import IppClient

let httpClient = HTTPClient(configuration: .init(certificateVerification: .none))

defer {
// the HTTPClient needs to be shutdown after use to avoid memory leaks
try httpClient.syncShutdown()
}

let printer = IppPrinter(
httpClient: httpClient,
uri: "ipps://macmini.local/printers/EPSON_XP_7100_Series"
Expand All @@ -28,7 +33,6 @@ let response = try await printer.printJob(

guard response.statusCode == .successfulOk, let jobId = response[job: \.jobId] else {
print("Print job failed with status \(response.statusCode)")
try httpClient.syncShutdown()
exit(1)
}

Expand All @@ -38,14 +42,12 @@ while true {
let response = try await job.getJobAttributes(requestedAttributes: [.jobState])
guard let jobState = response[job: \.jobState] else {
print("Failed to get job state")
try httpClient.syncShutdown()
exit(1)
}

switch jobState {
case .aborted, .canceled, .completed:
print("Job ended with state \(jobState)")
try httpClient.syncShutdown()
exit(0)
default:
print("Job state is \(jobState)")
Expand Down

0 comments on commit 427ef17

Please sign in to comment.