github.com/whatap/golib@v0.0.22/util/queue/RequestQueue.go (about)

     1  package queue
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/whatap/golib/util/dateutil"
     8  	"github.com/whatap/golib/util/list"
     9  )
    10  
    11  type RequestQueue struct {
    12  	queue      list.LinkedList
    13  	capacity   int
    14  	lock       *sync.Cond
    15  	Failed     func(interface{})
    16  	Overflowed func(interface{})
    17  }
    18  
    19  func NewRequestQueue(size int) *RequestQueue {
    20  	q := new(RequestQueue)
    21  	q.lock = sync.NewCond(new(sync.Mutex))
    22  	q.capacity = size
    23  	return q
    24  }
    25  func (this *RequestQueue) Get() interface{} {
    26  	this.lock.L.Lock()
    27  	defer this.lock.L.Unlock()
    28  
    29  	for this.queue.Size() <= 0 {
    30  		this.lock.Wait()
    31  	}
    32  	x := this.queue.RemoveFirst()
    33  	return x
    34  }
    35  func (this *RequestQueue) GetTimeout(timeout int) interface{} {
    36  	t := timeout
    37  	timeto := dateutil.SystemNow() + int64(timeout)
    38  
    39  	// 최대 3~ 4 회 루프
    40  	var v interface{} = nil
    41  	for v = this.GetNoWait(); v == nil; v = this.GetNoWait() {
    42  		time.Sleep(time.Duration(t/3) * time.Millisecond)
    43  
    44  		t = int(timeto - dateutil.SystemNow())
    45  		if t <= 0 {
    46  			break
    47  		}
    48  	}
    49  
    50  	return v
    51  }
    52  
    53  func (this *RequestQueue) GetNoWait() interface{} {
    54  	this.lock.L.Lock()
    55  	defer this.lock.L.Unlock()
    56  
    57  	if this.queue.Size() > 0 {
    58  		return this.queue.RemoveFirst()
    59  	} else {
    60  		return nil
    61  	}
    62  }
    63  
    64  func (this *RequestQueue) Put(v interface{}) bool {
    65  	this.lock.L.Lock()
    66  	defer this.lock.L.Unlock()
    67  	if this.capacity <= 0 || this.queue.Size() < this.capacity {
    68  		this.queue.Add(v)
    69  		this.lock.Broadcast()
    70  		return true
    71  	} else {
    72  		if this.Failed != nil {
    73  			this.Failed(v)
    74  		}
    75  		//this.lock.Signal()
    76  		this.lock.Broadcast()
    77  		return false
    78  	}
    79  }
    80  func (this *RequestQueue) PutForce(v interface{}) bool {
    81  	this.lock.L.Lock()
    82  	defer this.lock.L.Unlock()
    83  
    84  	if this.capacity <= 0 || this.queue.Size() < this.capacity {
    85  		this.queue.Add(v)
    86  		this.lock.Broadcast()
    87  		return true
    88  	} else {
    89  		for this.queue.Size() >= this.capacity {
    90  			o := this.queue.RemoveFirst()
    91  			if this.Overflowed != nil {
    92  				this.Overflowed(o)
    93  			}
    94  		}
    95  		this.queue.Add(v)
    96  		this.lock.Broadcast()
    97  		return false
    98  	}
    99  }
   100  
   101  func (this *RequestQueue) Clear() {
   102  	this.lock.L.Lock()
   103  	defer this.lock.L.Unlock()
   104  	this.queue.Clear()
   105  }
   106  func (this *RequestQueue) Size() int {
   107  	return this.queue.Size()
   108  }
   109  
   110  func (this *RequestQueue) GetCapacity() int {
   111  	return this.capacity
   112  }
   113  
   114  func (this *RequestQueue) SetCapacity(size int) {
   115  	this.capacity = size
   116  }