Skip to content

Commit

Permalink
Improving AutomaticRetry and making possible to retry other requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Dec 20, 2023
1 parent 6965dfb commit 02935f7
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 343 deletions.
7 changes: 6 additions & 1 deletion PubNubMembership/Sources/Membership+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import PubNubUser
public protocol PubNubMembershipInterface {
/// A copy of the configuration object used for this session
var configuration: PubNubConfiguration { get }

/// Session used for performing request/response REST calls
var networkSession: SessionReplaceable { get }

Expand Down Expand Up @@ -285,6 +284,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchMultipleValueResponseDecoder<PubNubMembership.PartialSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -337,6 +337,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchMultipleValueResponseDecoder<PubNubMembership.PartialUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -382,6 +383,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -418,6 +420,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -480,6 +483,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -516,6 +520,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
5 changes: 4 additions & 1 deletion PubNubSpace/Sources/Space+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import PubNub
public protocol PubNubSpaceInterface {
/// A copy of the configuration object used for this session
var configuration: PubNubConfiguration { get }

/// Session used for performing request/response REST calls
var networkSession: SessionReplaceable { get }

Expand Down Expand Up @@ -230,6 +229,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchMultipleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand All @@ -254,6 +254,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchSingleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -290,6 +291,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchSingleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -334,6 +336,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
6 changes: 4 additions & 2 deletions PubNubUser/Sources/User+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
//

import Foundation

import PubNub

/// Protocol interface to manage `PubNubUser` entities using closures
public protocol PubNubUserInterface {
/// A copy of the configuration object used for this session
var configuration: PubNubConfiguration { get }

/// Session used for performing request/response REST calls
var networkSession: SessionReplaceable { get }

Expand Down Expand Up @@ -238,6 +236,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)?
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchMultipleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -265,6 +264,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchSingleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) {
Expand Down Expand Up @@ -305,6 +305,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchSingleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -353,6 +354,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
12 changes: 9 additions & 3 deletions Sources/PubNub/APIs/File+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public extension PubNub {
) {
route(
FileManagementRouter(.list(channel: channel, limit: limit, next: next), configuration: configuration),
requestOperator: configuration.automaticRetry?[.files],
responseDecoder: FileListResponseDecoder(),
custom: requestConfig
) { result in
Expand Down Expand Up @@ -77,6 +78,7 @@ public extension PubNub {
) {
route(
FileManagementRouter(.delete(channel: channel, fileId: fileId, filename: filename), configuration: configuration),
requestOperator: configuration.automaticRetry?[.files],
responseDecoder: FileGeneralSuccessResponseDecoder(),
custom: requestConfig
) { result in
Expand Down Expand Up @@ -154,6 +156,7 @@ public extension PubNub {
.generateURL(channel: channel, body: .init(name: remoteFilename)),
configuration: configuration
),
requestOperator: configuration.automaticRetry?[.files],
responseDecoder: FileGenerateResponseDecoder(),
custom: requestConfig
) { [configuration] result in
Expand Down Expand Up @@ -242,9 +245,12 @@ public extension PubNub {
configuration: configuration
)

route(router,
responseDecoder: PublishResponseDecoder(),
custom: request.customRequestConfig) { result in
route(
router,
requestOperator: configuration.automaticRetry?[.files],
responseDecoder: PublishResponseDecoder(),
custom: request.customRequestConfig
) { result in
completion?(result.map { $0.payload.timetoken })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class DelayedHeartbeatEffect: DelayedEffectHandler {
guard let automaticRetry = configuration.automaticRetry else {
return nil
}
guard automaticRetry[.presence] != nil else {
return nil
}
guard automaticRetry.retryLimit > retryAttempt else {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class SubscribeRequest {
guard let automaticRetry = configuration.automaticRetry else {
return nil
}
guard automaticRetry[.subscribe] != nil else {
return nil
}
guard automaticRetry.retryLimit > retryAttempt else {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/PubNub/Helpers/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public extension Constant {
/// Produces a `User-Agent` header according to
/// [RFC7231 section 5.5.3](https://tools.ietf.org/html/rfc7231#section-5.5.3)
static let userAgentHeaderKey = "User-Agent"

/// A header indicating how long to wait before making a new request
/// [RFC6585 section 4](https://datatracker.ietf.org/doc/html/rfc6585#section-4)
static let retryAfterHeaderKey = "Retry-After"

internal static let defaultUserAgentHeader: String = {
let userAgent: String = {
Expand Down
4 changes: 3 additions & 1 deletion Sources/PubNub/Networking/Replaceables+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public protocol SessionReplaceable {

func route<Decoder>(
_ router: HTTPRouter,
requestOperator: RequestOperator?,
responseDecoder: Decoder,
responseQueue: DispatchQueue,
completion: @escaping (Result<EndpointResponse<Decoder.Payload>, Error>) -> Void
Expand All @@ -152,11 +153,12 @@ public protocol SessionReplaceable {
public extension SessionReplaceable {
func route<Decoder>(
_ router: HTTPRouter,
requestOperator: RequestOperator? = nil,
responseDecoder: Decoder,
responseQueue: DispatchQueue = .main,
completion: @escaping (Result<EndpointResponse<Decoder.Payload>, Error>) -> Void
) where Decoder: ResponseDecoder {
request(with: router, requestOperator: nil)
request(with: router, requestOperator: requestOperator)
.validate()
.response(
on: responseQueue,
Expand Down
Loading

0 comments on commit 02935f7

Please sign in to comment.