Skip to content

Commit

Permalink
BlockMessageLog现在支持正则表达式匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Apr 2, 2018
1 parent 843a23b commit 11ac96d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
24 changes: 24 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"path"
"reflect"
"regexp"
)

// 消息元信息
Expand Down Expand Up @@ -107,6 +108,25 @@ func MessageMetaByFullName(name string) *MessageMeta {
return nil
}

func MessageMetaVisit(nameRule string, callback func(meta *MessageMeta) bool) error {
exp, err := regexp.Compile(nameRule)
if err != nil {
return err
}

for name, meta := range metaByFullName {
if exp.MatchString(name) {

if !callback(meta) {
return nil
}

}
}

return nil
}

// 根据类型查找消息元信息
func MessageMetaByType(t reflect.Type) *MessageMeta {

Expand Down Expand Up @@ -171,6 +191,10 @@ func MessageToID(msg interface{}) int {

func MessageSize(msg interface{}) int {

if msg == nil {
return 0
}

// 获取消息元信息
meta := MessageMetaByType(reflect.TypeOf(msg))
if meta == nil {
Expand Down
18 changes: 7 additions & 11 deletions msglog/blocker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package msglog

import (
"errors"
"github.com/davyxu/cellnet"
"sync"
)
Expand All @@ -17,18 +16,15 @@ func IsBlockedMessageByID(msgid int) bool {
return ok
}

var (
ErrMessageNotFound = errors.New("msg not exists")
)
func BlockMessageLog(nameRule string) (err error, matchCount int) {

func BlockMessageLog(msgName string) error {
meta := cellnet.MessageMetaByFullName(msgName)
err = cellnet.MessageMetaVisit(nameRule, func(meta *cellnet.MessageMeta) bool {

if meta == nil {
return ErrMessageNotFound
}
blockedMsgByID.Store(int(meta.ID), meta)
matchCount++

blockedMsgByID.Store(int(meta.ID), meta)
return true
})

return nil
return
}

0 comments on commit 11ac96d

Please sign in to comment.