-2

In PHP we have ini_set('max_execution_time', 180) by which we can change the execution time on the fly. Is there anything similar to this in Go?

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
Abhaya
  • 19
  • 4
    No, there's not. It wouldn't really make sense in Go, either, since Go isn't single-threaded like PHP. – Jonathan Hall Nov 06 '19 at 10:19
  • 1
    The only similar timeouts Go offers are the various Timeouts in the [`http.Server`](https://golang.org/pkg/net/http/#Server) struct. And they can only be set on startup (although you could construct a program that would re-start a server when config changes, without restarting the whole process--but probably not worth it) – Jonathan Hall Nov 06 '19 at 10:21
  • 3
    A more dynamic approach would be to set a context timeout (perhaps in middleware) per connection. That could easily be done without restarting, but would only affect new connections. – Jonathan Hall Nov 06 '19 at 10:22
  • The context approach as suggested by @Flimzy is how I go about it. I have a `middleware` per se, which calls `context.WithTimeout` to create a new context. To make it dynamic, you can pick the value from an env variable to avoid restarting – Ayush Gupta Nov 06 '19 at 11:05
  • 1
    @AyushGupta: You cannot reset an ENV variable without restarting, though. – Jonathan Hall Nov 06 '19 at 11:08
  • @Flimzy oh is that? Then maybe another mechanism. Personally I would want(expect, perhaps) to pick the latest env variable value, but I guess caching that makes sense too(I believe it's cached at a shell level). Good to know, thanks – Ayush Gupta Nov 06 '19 at 11:16
  • 2
    @AyushGupta: See here: https://stackoverflow.com/q/205064/13860 TL;DR; There are hacky ways to change an env variable while the process is running, but there's no standard way to tell the process to re-read the env variables, so in practice, it's impossible. (And I'm sure the Go runtime doesn't support it, so you'd be stuck doing some low-level system calls to re-read the env) – Jonathan Hall Nov 06 '19 at 11:18

1 Answers1

0

Heres a script that allows you to set a program timeout and dynamically change timeout during execution. https://play.golang.org/p/qRvVMPnp9g2

package main

import (
    "fmt"
    "time"
    "os"
)

var (
    oldDuration time.Duration = 10 * time.Second
    timer *time.Timer = time.NewTimer(oldDuration)
    start time.Time = time.Now()
)

// The init function is called before main
func init(){
    // function asynchronously monitors and terminates script after timeout
    go func(){
        <- timer.C
        fmt.Println("Exit")
        os.Exit(1)
    }()
}

func main() {

    // Do some work
    <-time.After(2 * time.Second)
    fmt.Println("Hello World")

    // Change timeout interval dynamically
    setTimeout(5 * time.Second)

    // Do more work
    <-time.After(5 * time.Second)
    fmt.Println("Shouldn't be reached as script terminates after 5 seconds of which 2 seconds have been used earlier")
}

func setTimeout(newDuration time.Duration){

     // Stop timer - prevent program terminating in setTimeout function
     timer.Stop()

     // Compute remaining time
     var timePassed time.Duration = time.Since(start)
     var timeToTermination time.Duration = newDuration - timePassed

     // Restart timer - continuing from however many second 
     // have elapsed since the program started
     oldDuration = newDuration
     timer.Reset(timeToTermination)
}
Charana
  • 1,074
  • 1
  • 13
  • 26