1
0
Fork 0
burlesque/request.go

55 lines
898 B
Go
Raw Normal View History

2014-07-10 12:19:39 +00:00
package main
import (
"reflect"
)
type (
Request struct {
Queues []string
Callback func(*Response)
}
Response struct {
Queue string
Message Message
}
)
var (
pool = []*Request{}
)
func Register(q string, msg Message) {
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
}
}
}
GetQueue(q).Push(msg)
}
func Process(r *Request) {
for _, queueName := range r.Queues {
q := GetQueue(queueName)
2014-07-12 10:42:26 +00:00
msg, ok := q.TryFetch()
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
}
}
}