-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (48 loc) · 1.74 KB
/
Copy pathmain.go
File metadata and controls
59 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Example exec-timeout shows two ways a runaway script is stopped: a server-side
// hard cap via Config.ExecTimeout, and a caller deadline via the context passed
// to Run. Both abort a pure-Lua infinite loop (the timeout interrupts pure-Lua
// loops). Both surface as context.DeadlineExceeded.
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
luart "github.com/htcom-code/go-lua-perf"
)
const runaway = `function spin() while true do end end`
// timeoutDemo runs an infinite-loop script under an ExecTimeout and returns the
// error (expected: context.DeadlineExceeded).
func timeoutDemo() error {
loader := luart.NewMapLoader()
loader.Set("spin", runaway, luart.HashVersion(runaway), "1.0.0")
rt := luart.New(loader, luart.Config{MaxStates: 2, ExecTimeout: 50 * time.Millisecond})
defer rt.Close()
_, err := rt.Run(context.Background(), "spin", "spin")
return err
}
// ctxDemo runs the same script but relies on a caller context deadline instead
// of ExecTimeout (also expected: context.DeadlineExceeded).
func ctxDemo() error {
loader := luart.NewMapLoader()
loader.Set("spin", runaway, luart.HashVersion(runaway), "1.0.0")
rt := luart.New(loader, luart.Config{MaxStates: 2})
defer rt.Close()
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err := rt.Run(ctx, "spin", "spin")
return err
}
func main() {
err := timeoutDemo()
fmt.Printf("ExecTimeout aborted runaway script: %v\n", err)
if !errors.Is(err, context.DeadlineExceeded) {
log.Fatalf("expected DeadlineExceeded, got %v", err)
}
err = ctxDemo()
fmt.Printf("context deadline aborted runaway script: %v\n", err)
if !errors.Is(err, context.DeadlineExceeded) {
log.Fatalf("expected DeadlineExceeded, got %v", err)
}
}