Skip to content

Gerrard-YNWA/lua-resty-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Name

lua-resty-queue, event driven ring queue for Openresty.

Description

This Lua library implements a ring queue based on ngx.semaphore which ensures none blocking behavior.

Install

Install by luarocks:

luarocks install lua-resty-queue

Methods

new

syntax: q, err = queue:new([option:table])

  • option:table
    • capacity: number (optional) - capacity for the ring buffer default 1024
    • blocked: boolean (optional) - whether the queue is a blocked queue
    • timeout: number (optional) - seconds for waiting semaphore default 3 while specified a blocked queue, notice that if the queue is not a blocked queue, this option has no effect.

In case of success this method returns a queue entity, in case of error, it returns nil with a string describing the error.

Back to TOP

size

syntax: len = queue:size()

Get the number of elements.

capacity

syntax: cap = queue:capacity()

Get the capacity of queue.

Back to TOP

full

syntax: cap = queue:full()

Checks whether the queue is full.

Back to TOP

empty

syntax: cap = queue:empty()

Checks whether the queue is empty.

Back to TOP

push

syntax: ok, err = queue:push(element, wait?)

  • element can be any type except nil
  • wait boolean (optional) - whether to wait, default wait on blocked queue. Notice that if the queue is not a blocked queue, this option has no effect.

Push element to the tail of queue. A blocked queue will be blocked until queue is not full by default and user can control return at once while the queue is full by settng wait option to false. In case of push failed, it returns nil with a string describing the error.

Back to TOP

pop

syntax: element = queue:pop(wait?)

  • element can be any type except nil
  • wait boolean (optional) - whether to wait,, default wait on blocked queue. Notice that if the queue is not a blocked queue, this option has no effect.

Pop element at the head of queue. A blocked queue will be blocked until queue is not empty by default and user can control return at once while the queue is empty by settng wait option to false.

Back to TOP

clear

syntax: queue:clear()

Clear the queue.

Back to TOP