POC enhanced dispatcher

Currently it has multiple O(n) lookups over slices. Should transform it to
map[string]nothing to make it O(1) and possibly prettify the code a bit.
configurable-file-paths
Robert Gerus 2015-11-11 00:51:12 +01:00
parent 0a346f6727
commit 67e6d39d5e
1 changed files with 22 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"log"
"strings"
. "github.com/arachnist/gorepost/config"
"github.com/arachnist/gorepost/irc"
)
@ -21,13 +22,33 @@ func RemoveCallback(command, name string) {
delete(Callbacks[command], name)
}
func elementInSlice(s []interface{}, e interface{}) bool {
for _, se := range s {
if se == e {
return true
}
}
return false
}
func Dispatcher(quit chan struct{}, output chan irc.Message, input chan irc.Message) {
log.Println("spawned Dispatcher")
for {
select {
case msg := <-input:
if msg.Context["Source"] != "" {
if elementInSlice(C.Lookup(msg.Context, "Ignore").([]interface{}), msg.Context["Source"]) {
log.Println("Context:", msg.Context, "Ignoring", msg.Context["Source"])
continue
}
}
if Callbacks[msg.Command] != nil {
for _, f := range Callbacks[msg.Command] {
for i, f := range Callbacks[msg.Command] {
if elementInSlice(C.Lookup(msg.Context, "DisabledPlugins").([]interface{}), i) {
log.Println("Context:", msg.Context, "Plugin disabled", i)
continue
}
go f(output, msg)
}
}