Skip to content

Commit

Permalink
[feature] Migrate the main.go directory
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed Jul 12, 2021
1 parent 1245114 commit 3bd8b0e
Show file tree
Hide file tree
Showing 30 changed files with 1,310 additions and 1,317 deletions.
Binary file modified bin/koala
100644 → 100755
Binary file not shown.
41 changes: 41 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Koala Rule Engine Core
*
* @package: main
* @desc: koala engine - Main router
*
* koala频率控制服务 (规则引擎)
* 用途:是为了解决用户提交等相关频率控制的一个通用服务,主要是为了替换传统写死代码的频率控制模块以达到 高性能、灵活配置的要求。
* 方案:支持高度灵活的规则配置;并实现了规则配置的动态加载; 后端cache采用带连接池的redis。
*
* @author: heiyeluren
* @github: https://github.com/heiyeluren
* @blog: https://blog.csdn.net/heiyeshuwu
*
*/

package main

import (
"github.com/heiyeluren/koala/koala"
)

/**
* koala服务进程的main函数
*/
func main() {
// Start koala
koala.Run()

// 启动 rule统计协程
go koala.CounterAgent()

// 启动 规则更新协程,定期检查 policy 更新
go koala.PolicyLoader()

// 启动 http监听协程
go koala.FrontListen()

// hold 住 main协程
select {}
}
10 changes: 5 additions & 5 deletions conf/koala.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ externalWriteTimeout = 500
#--------------

#redis服务地址
redis_server = 127.0.0.1:3721
redis_server = 127.0.0.1:6379

#redis服务password
redis_auth = Yhs8jCfcys
redis_auth =

#redis连接池,最大空闲连接数
redis_pool_maxIdle = 5
Expand All @@ -52,10 +52,10 @@ log_warning_file_path = log/koala.log.wf
log_cron_time = day

#日志文件生存周期, 单位:天
log_file_ilfe_time = 7
log_file_life_time = 7

#日志chan队列的buffer长度,建议不要少于10240,不建议多于1024000,最长:67021478(超过这个值会无法启动)
log_chan_buff_size = 10240
#日志channel队列的buffer长度,建议不要少于10240,不建议多于1024000,最长:67021478(超过这个值会无法启动)
log_channel_buff_size = 10240

#日志刷盘的时间间隔,单位:毫秒,建议500~5000毫秒,建议不超过30秒
log_flush_timer = 1000
Expand Down
34 changes: 17 additions & 17 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package main

import (
"errors"
"github.com/garyburd/redigo/redis"
"github.com/gomodule/redigo/redis"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -52,7 +52,7 @@ type KoalaRule struct {
/**
* 浏览;count规则缓存查询、比较
*/
func (this *KoalaRule) countBrowse(cacheKey string) (bool, error) {
func (k *KoalaRule) countBrowse(cacheKey string) (bool, error) {
redisConn := RedisPool.Get()
defer redisConn.Close()

Expand All @@ -67,7 +67,7 @@ func (this *KoalaRule) countBrowse(cacheKey string) (bool, error) {
return false, err
}
//println(cacheKey, " --> value:", cacheValue)
if this.count == 0 || this.count > int32(cacheValue) {
if k.count == 0 || k.count > int32(cacheValue) {
return false, nil
}
return true, nil
Expand All @@ -76,7 +76,7 @@ func (this *KoalaRule) countBrowse(cacheKey string) (bool, error) {
/**
* 更新;count规则缓存更新
*/
func (this *KoalaRule) countUpdate(cacheKey string) error {
func (k *KoalaRule) countUpdate(cacheKey string) error {
redisConn := RedisPool.Get()
defer redisConn.Close()

Expand All @@ -87,18 +87,18 @@ func (this *KoalaRule) countUpdate(cacheKey string) error {
}
// set new cache
if exists == 0 {
var expiretime int32
var expireTime int32
// 86400 按照 自然天计算 过期时间
if this.time == 86400 {
if k.time == 86400 {
y, m, d := time.Now().Date()
loc, _ := time.LoadLocation("Asia/Shanghai")
dayEnd := time.Date(y, m, d, 23, 59, 59, 0, loc).Unix()
expiretime = int32(dayEnd - time.Now().Unix())
expireTime = int32(dayEnd - time.Now().Unix())
} else {
expiretime = this.time
expireTime = k.time
}

if _, err := redis.String(redisConn.Do("SETEX", cacheKey, expiretime, 1)); err != nil {
if _, err := redis.String(redisConn.Do("SETEX", cacheKey, expireTime, 1)); err != nil {
return err
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func (this *KoalaRule) countUpdate(cacheKey string) error {
* leak模式--查询
*
*/
func (this *KoalaRule) leakBrowse(cacheKey string) (bool, error) {
func (k *KoalaRule) leakBrowse(cacheKey string) (bool, error) {
redisConn := RedisPool.Get()
defer redisConn.Close()

Expand All @@ -159,7 +159,7 @@ func (this *KoalaRule) leakBrowse(cacheKey string) (bool, error) {
if listLen, err = redis.Int(redisConn.Do("LLEN", cacheKey)); err != nil {
return false, err
}
if listLen == 0 || listLen <= int(this.count) {
if listLen == 0 || listLen <= int(k.count) {
return false, nil
}

Expand All @@ -168,10 +168,10 @@ func (this *KoalaRule) leakBrowse(cacheKey string) (bool, error) {
}()

now := time.Now().Unix()
if edgeElement, err = redis.Int64(redisConn.Do("LINDEX", cacheKey, this.count)); err != nil {
if edgeElement, err = redis.Int64(redisConn.Do("LINDEX", cacheKey, k.count)); err != nil {
return false, err
}
if int32(now-edgeElement) <= this.time {
if int32(now-edgeElement) <= k.time {
return true, nil
}
return false, nil
Expand All @@ -181,11 +181,11 @@ func (this *KoalaRule) leakBrowse(cacheKey string) (bool, error) {
* leak模式--清理
* 清理队尾过期多余元素
*/
func (this *KoalaRule) leakClear(cacheKey string, listLen int) {
func (k *KoalaRule) leakClear(cacheKey string, listLen int) {
redisConn := RedisPool.Get()
defer redisConn.Close()

for listLen > int(this.count+1) {
for listLen > int(k.count+1) {
if _, err := redis.Int64(redisConn.Do("RPOP", cacheKey)); err != nil {
return
}
Expand All @@ -196,7 +196,7 @@ func (this *KoalaRule) leakClear(cacheKey string, listLen int) {
/**
* leak模式--更新
*/
func (this *KoalaRule) leakUpdate(cacheKey string) error {
func (k *KoalaRule) leakUpdate(cacheKey string) error {
redisConn := RedisPool.Get()
defer redisConn.Close()

Expand All @@ -214,7 +214,7 @@ func (this *KoalaRule) leakUpdate(cacheKey string) error {
* leak模式--反馈
* 根据指令,减少桶内若干元素
*/
func (this *KoalaRule) leakFeedback(cacheKey string, feedback int) error {
func (k *KoalaRule) leakFeedback(cacheKey string, feedback int) error {
redisConn := RedisPool.Get()
defer redisConn.Close()

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/heiyeluren/koala

go 1.16

require github.com/gomodule/redigo v1.8.5
require (
github.com/gomodule/redigo v1.8.5
gopkg.in/yaml.v2 v2.3.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion koala/KoalaKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

package main
package koala

import (
"errors"
Expand Down
11 changes: 5 additions & 6 deletions koala/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
* @package: main
* @desc: koala engine - admin web api
*
* @author: heiyeluren
* @author: heiyeluren
* @github: https://github.com/heiyeluren
* @blog: https://blog.csdn.net/heiyeshuwu
*
*/

package main
package koala

/*
import (
"fmt"
"io/ioutil"
"os"
"utility/logger"
"utility/network"
"github.com/heiyeluren/koala/utility"
)
暂时删除
func (this *FrontServer) DoRuleRewrite(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoRuleRewrite(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
ext_stream := request.Pstr("rule_stream")
if ext_stream == "" {
response.Puts(`{"errno": -1, "errmsg": "no rule stream"}`)
Expand All @@ -46,7 +45,7 @@ func (this *FrontServer) DoRuleRewrite(request *network.HttpRequest, response *n
*/

/*
func (this *FrontServer) DoDumpCounter(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoDumpCounter(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
retString := ""
rule_no := int32(request.Gint("rule_no"))
counters := GetCurrentCounters()
Expand Down
8 changes: 4 additions & 4 deletions koala/cacheSvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
*
*/

package main
package koala

import (
"time"

"github.com/gomodule/redigo/redis"
)

// initRedisPool redis连接池初始化函数
func initRedisPool() {
// InitRedisPool redis连接池初始化函数
func InitRedisPool() {
// redis服务 host:port
var server string = Config.Get("redis_server")
// redis服务 口令
Expand All @@ -43,7 +43,7 @@ func initRedisPool() {
if err != nil {
return nil, err
}
if _, err := c.Do("AUTH", password); err != nil {
if _, err = c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions koala/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
*
*/

package main
package koala

import (
"flag"

"github.com/heiyeluren/koala/utility/configs"
"github.com/heiyeluren/koala/utility"
)

// 启动前加载配置文件
func newConfig() *configs.Config {
// NewConfig 启动前加载配置文件
func NewConfig() *utility.Config {
var F string
flag.StringVar(&F, "f", "", "config file")
flag.Parse()
if F == "" {
panic("usage: ./koala -f etc/koala.conf")
}
config := configs.NewConfig()
config := utility.NewConfig()
if err := config.Load(F); err != nil {
panic(err.Error())
}
Expand Down
19 changes: 9 additions & 10 deletions koala/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
* @package: main
* @desc: koala engine - Test case code
*
* @author: heiyeluren
* @author: heiyeluren
* @github: https://github.com/heiyeluren
* @blog: https://blog.csdn.net/heiyeshuwu
*
*/

package main
package koala

// 测试使用 暂时删除
/*
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/gomodule/redigo/redis"
"runtime"
"strconv"
"time"
"utility/logger"
"utility/network"
"github.com/heiyeluren/koala/utility"
)
func (this *FrontServer) DoLogTest(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoLogTest(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
logHandle.Debug("[clientip=192.168.0.1 errno=0 errmsg=ok debuginfo]")
logHandle.Trace("[clientip=192.168.0.1 errno=0 errmsg=ok traceinfo]")
logHandle.Warning("warning test")
Expand All @@ -34,14 +33,14 @@ func (this *FrontServer) DoLogTest(request *network.HttpRequest, response *netwo
response.SetCode(200)
}
func (this *FrontServer) DoDumpPolicy(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoDumpPolicy(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
println(request.GetRemoteIP())
var dumpString string = ""
var localPolicy *Policy = GlobalPolicy
for _, singleRule := range localPolicy.ruleTable {
var singleDump string = ""
singleDump += "methord_ " + singleRule.methord
singleDump += "methord_ " + singleRule.method
singleDump += " _keys_ " + dumpkeys(singleRule.keys)
singleDump += " _base_ " + strconv.Itoa(int(singleRule.base))
singleDump += " _time_ " + strconv.Itoa(int(singleRule.time))
Expand All @@ -62,7 +61,7 @@ func dumpkeys(keys map[string]KoalaKey) string {
return ret
}
func (this *FrontServer) DoRedisCmd(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoRedisCmd(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
redisConn := RedisPool.Get()
defer redisConn.Close()
Expand All @@ -86,7 +85,7 @@ func (this *FrontServer) DoRedisCmd(request *network.HttpRequest, response *netw
response.SetCode(200)
}
func (this *FrontServer) DoRedisMget(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
func (s *FrontServer) DoRedisMget(request *network.HttpRequest, response *network.HttpResponse, logHandle *logger.Logger) {
redisConn := RedisPool.Get()
defer redisConn.Close()
Expand Down
Loading

0 comments on commit 3bd8b0e

Please sign in to comment.