[SOLVED] How to get timeout in Lua https

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1128
Joined: Thu Dec 10, 2020 1:57 am

[SOLVED] How to get timeout in Lua https

Post by Gunroar:Cannon() »

slime wrote: Fri Sep 06, 2024 7:55 pm love 12 provides lua-https. love 11 doesn't provide it itself but you can get or make a standalone build of it.
I can't seem to set the timeout for lua-https, so that it will stop stalling/hanging the game after one second and not ~10 seconds like it currently does.

Setting https.TIMEOUT/http.TIMEOUT doesn't work, neither does setting it in the options argument. I've browsed and looked around and besides these I can't find it.
Last edited by Gunroar:Cannon() on Fri Sep 20, 2024 2:45 pm, edited 1 time in total.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
slime
Solid Snayke
Posts: 3159
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How to get timeout in Lua https

Post by slime »

What made you assume there is a timeout API?

If you want to prevent stalls, you'd need to use a thread since any https request, even successful ones that happen quickly, will block the thread they're called from until the request is done. That being said, what sort of situation are you creating where you're regularly expecting a timeout?
User avatar
Gunroar:Cannon()
Party member
Posts: 1128
Joined: Thu Dec 10, 2020 1:57 am

Re: How to get timeout in Lua https

Post by Gunroar:Cannon() »

I thought there were timeouts because I saw some stuff when I browsed it. I know it's not the same lib, but there were many different pages like this.

I'm doing a thing where I check and submit scores to Lootlocker after a game. If there is no connection at all it's fine because it just loads and skips immediately, but if there is a bad connection, or one without data, the display I put up that says "Checking online scores"* will show for a long time (i.e. the game will hang for a really long time) even though I know at that point it shouldn't take more than a few seconds.

Also I thought, being a newb in most things, that all https/http libs had a way to include timeout. Like how they all have a way to include body/data.

So the only way to introduce timeouts is with threads?

* I put a display that greys over the whole game and shows that text so players will atleast know why the game is hanging.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
slime
Solid Snayke
Posts: 3159
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How to get timeout in Lua https

Post by slime »

Gunroar:Cannon() wrote: Wed Sep 18, 2024 9:57 am I thought there were timeouts because I saw some stuff when I browsed it. I know it's not the same lib, but there were many different pages like this.
Yeah, that's for a totally different library with different APIs.
User avatar
Gunroar:Cannon()
Party member
Posts: 1128
Joined: Thu Dec 10, 2020 1:57 am

Re: How to get timeout in Lua https

Post by Gunroar:Cannon() »

slime wrote: Wed Sep 18, 2024 10:56 am Yeah, that's for a totally different library with different APIs.
I implemented it...

Code: Select all

local _https = require("https")

local https = {
    request = function(url, body, timeout)
        local threadCode = string.format([[       
            local url, body = ...
            
            local _https = require("https")
            
            local code, response, b, c = _https.request(url, body)
            love.thread.getChannel("code"):push(code)
            love.thread.getChannel("response"):push(response)
        ]])
        
        thr = love.thread.newThread(threadCode)
        thr:start(url, body)
        
        local code = love.thread.getChannel("code"):pop()
        
        if code then
            return code, love.thread.getChannel("response"):pop()
        end
        
        love.timer.sleep(timeout or body.timeout or 2)
        
        local code = love.thread.getChannel("code"):pop()
        if code then
            return code, love.thread.getChannel("response"):pop()
        else
            return 0, "timed out"
        end
        
    end
}
Nothing too fancy, just a basic mock up (it works though).
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests