github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/runtimelib/lua/softlimits.quotas.lua (about)

     1  -- By default runtime.contextdue returns false
     2  print(runtime.contextdue())
     3  --> =false
     4  
     5  -- runtime.contextdue returns true if a cpu soft limit has been reached
     6  print(runtime.callcontext({stop={cpu=100}}, function()
     7      print(runtime.contextdue())
     8      --> =false
     9      local ctx = runtime.context()
    10      print(ctx.stop.cpu)
    11      --> =100
    12      while not runtime.contextdue() do end
    13      print(ctx.used.cpu >= 100, ctx.used.cpu <= 200)
    14      --> =true	true
    15  end))
    16  --> =done
    17  
    18  -- runtime.contextdue returns true if a mem soft limit has been reached
    19  print(runtime.callcontext({stop={memory=1000}}, function()
    20      print(runtime.contextdue())
    21      --> =false
    22      local ctx = runtime.context()
    23      print(ctx.stop.memory)
    24      --> =1000
    25      local a = "x"
    26      while not runtime.contextdue() do 
    27          a = a .. a -- consume some memory
    28      end
    29      print(ctx.used.memory >= 1000, ctx.used.memory <= 2000)
    30      --> =true	true
    31  end))
    32  --> =done
    33  
    34  -- runtime.contextdue returns true if a time soft limit has been reached
    35  print(runtime.callcontext({stop={millis=20}}, function()
    36      print(runtime.contextdue())
    37      --> =false
    38      local ctx = runtime.context()
    39      print(ctx.stop.millis)
    40      --> =20
    41      while not runtime.contextdue() do end
    42      print(ctx.used.millis >= 20, ctx.used.millis <= 30)
    43      --> =true	true
    44  end))
    45  --> =done
    46  
    47  -- soft limits cannot exceed hard limits, either in the same context or in the
    48  -- parent context
    49  runtime.callcontext({kill={millis=1000}}, function() 
    50      runtime.callcontext({stop={millis=2000}}, function()
    51          print(runtime.context().stop.millis <= 1000)
    52          --> =true
    53      end)
    54  end)
    55  runtime.callcontext({kill={millis=1000}, stop={millis=5000}}, function()
    56      print(runtime.context().stop.millis <= 1000)
    57      --> =true
    58  end)
    59  
    60  -- soft limits cannot increase from the parent's soft limit.
    61  runtime.callcontext({stop={cpu=1000}, kill={cpu=2000}}, function()
    62      runtime.callcontext({stop={cpu=3000}}, function()
    63          print(runtime.context().stop.cpu)
    64          --> =1000
    65      end)
    66  end)