Created
December 15, 2015 01:45
-
-
Save taotetek/88cd49d0d3cd206ed26c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package captainslog | |
import "sync" | |
type Queue struct { | |
q []SyslogMsg | |
lock *sync.Mutex | |
} | |
func NewQueue(max int) *Queue { | |
return &Queue{ | |
lock: &sync.Mutex{}, | |
q: make([]SyslogMsg, 0, max), | |
} | |
} | |
func (q *Queue) Enqueue(s SyslogMsg) { | |
q.lock.Lock() | |
defer q.lock.Unlock() | |
q.q = append(q.q, s) | |
} | |
func (q *Queue) Dequeue() SyslogMsg { | |
q.lock.Lock() | |
defer q.lock.Unlock() | |
m := q.q[0] | |
q.q = q.q[1:] | |
return m | |
} | |
func (q *Queue) Len() int { | |
return len(q.q) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment