filter.go 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package tinymq
  2. import (
  3. "reflect"
  4. "regexp"
  5. "runtime"
  6. "strings"
  7. )
  8. // 匹配所有的频道
  9. func AllChannelFilter() FilterFunc {
  10. return func(conn *Line) (ok bool) {
  11. return true
  12. }
  13. }
  14. // 正则频道过滤器
  15. func RegChannelFilter(channel *regexp.Regexp) FilterFunc {
  16. return func(conn *Line) (ok bool) {
  17. return channel.MatchString(conn.channel)
  18. }
  19. }
  20. // 字符串频道过滤器
  21. func StrChannelFilter(channel string) FilterFunc {
  22. return func(conn *Line) (ok bool) {
  23. return strings.Contains(conn.channel, channel)
  24. }
  25. }
  26. // 连接过滤器
  27. func LineLinkFilter(line *Line) FilterFunc {
  28. return func(conn *Line) (ok bool) {
  29. return line == conn
  30. }
  31. }
  32. // GetFunctionName returns the fully qualified name of the function passed as an interface.
  33. func GetFunctionName(temp any) string {
  34. // Use reflect to get the pointer to the function's code.
  35. pc := reflect.ValueOf(temp).Pointer()
  36. // Use runtime.FuncForPC to get function details.
  37. f := runtime.FuncForPC(pc)
  38. if f == nil {
  39. return ""
  40. }
  41. return f.Name()
  42. }