I think the easiest thing to do is to just change `count` at the outset of the function based on how many jobs are allowed to be popped:

```lua
if max_concurrency > 0 then
  -- We need to find out how many locks are still valid.
  local num_still_locked = redis.call('zcount', key .. ', now, '+inf')
  -- Only allow the minimum of the two through
  count = math.min(max_concurrency - num_still_locked, count)
end
```