Monday, November 29, 2021

Swift: Three dot operator

You may see three dot ... operator in swift for two different cases:

1. ... as variadic parameter in function definition A variadic parameter accepts zero or more values of a specified type. The parameters are separated with comma. The value of a variadic parameter in the function’s body is an array with the specified element type. A function can only have at most one variadic parameter.

func arithmeticMean(_ numbers: Double...) -> Double {
  var total: Double = 0
    for number in numbers {
      total += number
    }
  return total / Double(numbers.count)
}

arithmeticMean(1, 2, 3, 4, 5)
arithmeticMean()
2. ... as a closed range operator in statement The closed range operator a...b creates a ClosedRange object that contains elements from a to b inclusive. The value of a must not be greater than b. It is most used to enumerate all elements in a for loop

for index in 1...5 {
  print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
And from the Swift documentation:
A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (...) after the parameter’s type name. The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type. For example, a variadic parameter with a name of numbers and a type of Double... is made available within the function’s body as a constant array called numbers of type [Double].

Sunday, November 28, 2021

Which HTTP errors should never trigger an automatic retry ?


TLDR: 1xx, 2xx, 3xx, 4xx, 5xx

There are some errors that should not be retried because they seem permanent:

  • 400 Bad Request
  • 401 Unauthorized
  • 402 Payment Required
  • 403 Forbidden
  • 405 Method Not Allowed
  • 406 Not Acceptable
  • 407 Proxy Authentication Required
  • 409 Conflict - it depends
  • 410 Gone
  • 411 Length Required
  • 412 Precondition Failed
  • 413 Payload Too Large 
  • 414 URI Too Long
  • 415 Unsupported Media Type
  • 416 Range Not Satisfiable
  • 417 Expectation Failed
  • 418 I'm a teapot - not sure about this one
  • 421 Misdirected Request
  • 422 Unprocessable Entity
  • 423 Locked - it depends on how long a resource is locked in average (?)
  • 424 Failed Dependency
  • 426 Upgrade Required - can the client be upgraded automatically?
  • 428 Precondition Required - I don't thing that the precondition can be fulfilled the second time without retrying from the beginning of the whole process but it depends
  • 429 Too Many Requests - it depends but it should not be retried to fast
  • 431 Request Header Fields TooLarge
  • 451 Unavailable For Legal Reasons

So, most of the 4** Client errors should not be retried.

4xx codes mean that an error has been made at the caller's side. That could be a bad URL, bad authentication credentials or anything that indicates it was a bad request. Therefore, without fixing that problem, there isn't an use of retry. The error is in caller's domain and caller should fix it instead of hoping that it will fix itself.

The 5** Servers errors that should not be retried:

  • 500 Internal Server Error - it depends on the cause of the error
  • 501 Not Implemented
  • 502 Bad Gateway - I saw used for temporary errors so it depends
  • 505 HTTP Version Not Supported
  • 506 Variant Also Negotiates
  • 507 Insufficient Storage
  • 508 Loop Detected
  • 510 Not Extended
  • 511 Network Authentication Required

5xx error codes should be retried as those are service errors. They could be short term (overflowing threads, dependent service refusing connections) or long term (system defect, dependent system outage, infrastructure unavailable). Sometimes, services reply back with the information (often headers) whether this is permanent or temporary; and sometimes a time parameter as to when to retry. Based on these parameters, callers can choose to retry or not.

1xx, 2xx and 3xx codes need not be retried for obvious reasons.

However, in order to make the microservices more resilient you should use the Circuit breaker pattern and fail fast when the upstream is down.

Wednesday, November 24, 2021

Dart: Isolate

Lately, I have been working on a janky-improvement issue in a Flutter application, so I have a chance to understand a little bit how Dart supports the asynchronous programming - despite of being a Single Thread Language.
Here is the key you will need to remember is: Dart does one task a time. Once a task is running, everything is blocked waiting for its finish.
When a Dart app is started, an instance of the isolate is created by DartVM automatically and you can run your "main" code on it - we can call it Main Isolate (Isolate is not a thread, though they share some similar context). The process is:
  1. Make 2 FIFO queues named "MicroTask Queue" and "Event Queue"
  2. Run main() function until it's complete
  3. Run Event Loop

Event loop

  • Event loop is an infinite loop. It is there to check if no task remains on Micro Task Queue, then it will push the tasks from Event Queue to main Isolate and working on it.
  • MicroTask Queue: short time tasks for internal event that need to be done before return back to Event Queue
  • Event Queue: all tasks from external events like I/O, gesture, drawing, timers, streams, future, async, await... So, onPressed is waiting for a tap, and the future is waiting for network data, but from Dart’s perspective, those are both just events in the Event Queue - these APIs are all just ways for you to tell Dart’s event loop, "Here’s some code, please run it later."

Isolate

You can think about Isolate as a simple computer, with some `Memory`, a Single Thread, and an Event Loop. The idea is to move the heavy task to another computer that you don't interact to so you don't feed it janky (:D)

What is differences between Isolate and Thread

  • AFAIK the isolate is like its name, quite isolation, have own resources, zone, limitation.
  • Does not share heap memory between each other
  • Uses Ports and Messages to communicate between them.
  • Only support primitive data (though complex data can be passed around by Dictionary type)

When to use Isolate

  • Big JSON encoding/ decoding
  • Encryption
  • Image/ video processing

How to create an Isolate

  • Create 3 instances isolate, sendPort, receivePort
  • Registering an entry point to send and receive data
IMO, you wont use Isolate usually in most common applications. Mostly one Isolate but asynchronous is enough. This is doable through the async and await.