2014-07-10 12:19:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Request struct {
|
|
|
|
Queues []string
|
|
|
|
Callback func(*Response)
|
2014-07-16 17:50:05 +00:00
|
|
|
Abort chan bool
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
Response struct {
|
|
|
|
Queue string
|
|
|
|
Message Message
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pool = []*Request{}
|
|
|
|
)
|
|
|
|
|
2014-07-16 17:50:05 +00:00
|
|
|
func Register(q string, msg Message) bool {
|
2014-07-10 12:19:39 +00:00
|
|
|
for i, r := range pool {
|
|
|
|
for _, queueName := range r.Queues {
|
|
|
|
if queueName == q {
|
|
|
|
go r.Callback(&Response{Queue: queueName, Message: msg})
|
|
|
|
pool = append(pool[:i], pool[i+1:]...)
|
|
|
|
return
|
2014-07-16 17:50:05 +00:00
|
|
|
|
|
|
|
return true
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 17:50:05 +00:00
|
|
|
|
|
|
|
ok := GetQueue(q).Push(msg)
|
|
|
|
return ok
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Process(r *Request) {
|
|
|
|
for _, queueName := range r.Queues {
|
|
|
|
q := GetQueue(queueName)
|
2014-07-16 17:50:05 +00:00
|
|
|
msg, ok := q.TryFetch(r.Abort)
|
2014-07-12 10:42:26 +00:00
|
|
|
if ok {
|
|
|
|
go r.Callback(&Response{Queue: queueName, Message: msg})
|
2014-07-10 12:19:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pool = append(pool, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Purge(r *Request) {
|
|
|
|
for i, req := range pool {
|
|
|
|
if reflect.ValueOf(r).Pointer() == reflect.ValueOf(req).Pointer() {
|
|
|
|
pool = append(pool[:i], pool[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|