H13 Evaporation Error on Heroku

My program seems to work fine on Heroku, but after reloading the page 3-4 times, it crashes and I get an error message H13: Connection closed without response. However, it works fine and error-free when I run it on my computer.

Here is my code:

#if os(Linux)
  import Glibc
#else
  import Darwin
#endif
import Vapor

let arrayA: [String] = ["some strings here"]

let arrayB: [String] = ["more strings there"]

let arrayC: [String] = ["and some more here"]

func buildName (from arrayA: [String], and arrayB: [String], and arrayC: [String]) -> String {
  #if os(Linux)
    let a: Int = Int(random() % (arrayA.count + 1))
    let b: Int = Int(random() % (arrayB.count + 1))
    let c: Int = Int(random() % (arrayC.count + 1))
  #else
    let a: Int = Int(arc4random_uniform(UInt32(arrayA.count)))
    let b: Int = Int(arc4random_uniform(UInt32(arrayB.count)))
    let c: Int = Int(arc4random_uniform(UInt32(arrayC.count)))
  #endif

  return (arrayA[a] + " " + arrayB[b] + " " + arrayC[c])
}

let defaultHead: String = "<head><meta charset='utf-8'></head>"

//create Droplet object
let drop = Droplet()

// REGISTER Routes and handlers
drop.get { req in
  return buildName(from: arrayA, and: arrayB, and: arrayC)
}

// Start the server
drop.run()

What am I doing wrong?

+4
source share
2 answers

let a: Int = Int(random() % (arrayA.count + 1))

this line will generate a number equal to ArrayA.count. That way he can create fatal error: Index out of range.

So, I think this is the main reason.

0
source

arc4random_uniform does not work on linux. use instead:

public static func randomInt(min: Int, max:Int) -> Int {
        #if os(Linux)
            return Glibc.random() % max
        #else
            return min + Int(arc4random_uniform(UInt32(max - min + 1)))
        #endif
    }
0
source

Source: https://habr.com/ru/post/1660415/


All Articles