Skip to content

Commit

Permalink
Merge pull request #765 from bfenetworks/release/v1.2.0
Browse files Browse the repository at this point in the history
Release/v1.2.0
  • Loading branch information
iyangsj committed Jun 21, 2021
2 parents 5b1daae + 9c7b97b commit ebcbb72
Show file tree
Hide file tree
Showing 93 changed files with 1,287 additions and 372 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ project_name: bfe
builds:
- binary: bfe
main: ./bfe.go
ldflags:
- -X main.version={{.Version}} -X main.commit={{.Commit}}
- -extldflags=-static
goos:
- linux
- darwin
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v1.2.0] - 2021-06-21

### Added
- Set GOMAXPROCS according to real CPU quota if running on container
- Support condition primitive bfe_time_range/bfe_periodic_time_range
- Support URI hash strategy
- mod_block: support global request rules and ALLOW action
- mod_header: support variables about client certificate
- mod_access: support log to single file and stdout

### Changed
- Change primitive params of req_context_value_in


## [v1.1.0] - 2021-04-08

### Added
Expand Down Expand Up @@ -238,6 +252,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Flexible plugin framework to extend functionality. Based on the framework, developer can add new features rapidly
- Detailed built-in metrics available for service status monitor

[v1.2.0]: https://github.com/bfenetworks/bfe/compare/v1.1.0...v1.2.0
[v1.1.0]: https://github.com/bfenetworks/bfe/compare/v1.0.0...v1.1.0
[v1.0.0]: https://github.com/bfenetworks/bfe/compare/v0.12.0...v1.0.0
[v0.12.0]: https://github.com/bfenetworks/bfe/compare/v0.11.0...v0.12.0
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
| Qing Liu | liuximu |
| Qingxin Yang | yangqingxin1993 |
| Quan Zhang | FriendshipBridge |
| Renzhang Wang | wangrzneu |
| Shan Xiao | arlingtonroad |
| Shengnan Yu | goldfish-fish |
| Shuai Yan | yanshuai615270 |
Expand All @@ -51,6 +52,7 @@
| Wenlong Chen | LeroChen |
| Wensi Yang | tianxinheihei |
| Xiaofei Yu | xiaofei0800 |
| Xiaogang Zhang | zhangxiaogang01 |
| Xiaoli Liu | liuxiaoli007 |
| Xiaonan chen | two |
| Xiaoye Jiang | kidleaf-jiang |
Expand All @@ -59,6 +61,7 @@
| Yang Liu | dut-yangliu |
| Yusheng Sun | wodedipanr |
| Yuan Liu | lewisay |
| Yufeng Zhu | zyf0411 |
| Yuqi Xiao | YuqiXiao |
| Zheng Liu | liuzheng |
| Zhichao Lin | lxiaozhic |
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ GOTEST := $(GO) test
GOVET := $(GO) vet
GOGET := $(GO) get
GOGEN := $(GO) generate
GOCLEAN := $(GO) clean
GOFLAGS := -race
STATICCHECK := staticcheck

Expand Down Expand Up @@ -57,7 +58,7 @@ prepare-gen:
# make compile, go build
compile: test build
build:
$(GOBUILD) -ldflags "-X main.version=$(BFE_VERSION) -X main.commit=$(GIT_COMMIT)"
$(GOBUILD) -ldflags "-X main.version=$(BFE_VERSION) -X main.commit=$(GIT_COMMIT) -extldflags=-static"

# make test, test your code
test: test-case vet-case
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.2.0
24 changes: 15 additions & 9 deletions bfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
import (
"github.com/baidu/go-lib/log"
"github.com/baidu/go-lib/log/log4go"
_ "go.uber.org/automaxprocs"
)

import (
Expand All @@ -35,13 +36,13 @@ import (
)

var (
help *bool = flag.Bool("h", false, "to show help")
confRoot *string = flag.String("c", "./conf", "root path of configuration")
logPath *string = flag.String("l", "./log", "dir path of log")
stdOut *bool = flag.Bool("s", false, "to show log in stdout")
showVersion *bool = flag.Bool("v", false, "to show version of bfe")
showVerbose *bool = flag.Bool("V", false, "to show verbose information about bfe")
debugLog *bool = flag.Bool("d", false, "to show debug log (otherwise >= info)")
help = flag.Bool("h", false, "to show help")
confRoot = flag.String("c", "./conf", "root path of configuration")
logPath = flag.String("l", "./log", "dir path of log")
stdOut = flag.Bool("s", false, "to show log in stdout")
showVersion = flag.Bool("v", false, "to show version of bfe")
showVerbose = flag.Bool("V", false, "to show verbose information about bfe")
debugLog = flag.Bool("d", false, "to show debug log (otherwise >= info)")
)

var version string
Expand Down Expand Up @@ -99,8 +100,13 @@ func main() {
bfe_util.AbnormalExit()
}

// set maximum number of cpus
runtime.GOMAXPROCS(config.Server.MaxCpus)
// maximum number of CPUs (GOMAXPROCS) defaults to runtime.CPUNUM
// if running on machine, or CPU quota if running on container
// (with the help of "go.uber.org/automaxprocs").
// here, we change maximum number of cpus if the MaxCpus is positive.
if config.Server.MaxCpus > 0 {
runtime.GOMAXPROCS(config.Server.MaxCpus)
}

// set log level
bfe_debug.SetDebugFlag(config.Server)
Expand Down
2 changes: 1 addition & 1 deletion bfe_balance/backend/bfe_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type BfeBackend struct {

sync.RWMutex // guards following fields
avail bool // whether the backend is usable
restarted bool // indicate if this backend is new bring-up by health-check
connNum int // number of connections backend hold
failNum int // number of consecutive failures of normal requests
succNum int // number of consecutive successes of health-check request

closeChan chan bool // tell health-check to stop

restarted bool // indicate if this backend is new bring-up by health-check
}

func NewBfeBackend() *BfeBackend {
Expand Down
4 changes: 2 additions & 2 deletions bfe_balance/backend/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func UpdateStatus(backend *BfeBackend, cluster string) bool {
// get conf of health check, which is separately stored for each cluster
checkConf := getCheckConf(cluster)
if checkConf == nil {
// just ingore if not found health check conf
// just ignore if not found health check conf
return false
}

Expand Down Expand Up @@ -66,7 +66,7 @@ loop:
default:
}

// get lastest conf for health check
// get the latest conf to do health check
checkConf := getCheckConf(cluster)
if checkConf == nil {
// never come here
Expand Down
5 changes: 4 additions & 1 deletion bfe_balance/bal_gslb/bal_gslb.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (bal *BalanceGslb) SetSlowStart(backendConf cluster_conf.BackendBasic) {
bal.lock.Unlock()
}

// Init inializes gslb cluster with config
// Init initializes gslb cluster with config
func (bal *BalanceGslb) Init(gslbConf gslb_conf.GslbClusterConf) error {
totalWeight := 0

Expand Down Expand Up @@ -284,6 +284,9 @@ func (bal *BalanceGslb) getHashKey(req *bfe_basic.Request) []byte {
if hashKey == nil {
hashKey = clientIP
}

case cluster_conf.RequestURI:
hashKey = []byte(req.HttpRequest.RequestURI)
}

// if hashKey is empty, use random value
Expand Down
26 changes: 25 additions & 1 deletion bfe_basic/condition/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,31 @@ func buildPrimitive(node *parser.CallExpr) (Condition, error) {
name: node.Fun.Name,
node: node,
fetcher: &ContextValueFetcher{node.Args[0].Value},
matcher: NewInMatcher(node.Args[1].Value, false),
matcher: NewInMatcher(node.Args[1].Value, node.Args[2].ToBool()),
}, nil

case "bfe_time_range":
matcher, err := NewTimeMatcher(node.Args[0].Value, node.Args[1].Value)
if err != nil {
return nil, err
}
return &PrimitiveCond{
name: node.Fun.Name,
node: node,
fetcher: &BfeTimeFetcher{},
matcher: matcher,
}, nil

case "bfe_periodic_time_range":
matcher, err := NewPeriodicTimeMatcher(node.Args[0].Value, node.Args[1].Value, node.Args[2].Value)
if err != nil {
return nil, err
}
return &PrimitiveCond{
name: node.Fun.Name,
node: node,
fetcher: &BfeTimeFetcher{},
matcher: matcher,
}, nil
default:
return nil, fmt.Errorf("unsupported primitive %s", node.Fun.Name)
Expand Down
4 changes: 3 additions & 1 deletion bfe_basic/condition/parser/semant.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ var funcProtos = map[string][]Token{
"ses_tls_sni_in": {STRING},
"ses_tls_client_auth": nil,
"ses_tls_client_ca_in": {STRING},
"req_context_value_in": {STRING, STRING},
"req_context_value_in": {STRING, STRING, BOOL},
"bfe_time_range": []Token{STRING, STRING},
"bfe_periodic_time_range": []Token{STRING, STRING, STRING},
}

func prototypeCheck(expr *CallExpr) error {
Expand Down
108 changes: 107 additions & 1 deletion bfe_basic/condition/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import (
"sort"
"strconv"
"strings"
"time"
)

import (
"github.com/bfenetworks/bfe/bfe_basic"
"github.com/bfenetworks/bfe/bfe_basic/condition/parser"
"github.com/bfenetworks/bfe/bfe_util"
"github.com/bfenetworks/bfe/bfe_util/net_util"
"github.com/spaolacci/murmur3"
)
Expand Down Expand Up @@ -635,7 +637,7 @@ func (rf *ResCodeFetcher) Fetch(req *bfe_basic.Request) (interface{}, error) {
type TrustedCIpMatcher struct{}

func (m *TrustedCIpMatcher) Match(req *bfe_basic.Request) bool {
return req.Session.IsTrustIP
return req.Session.TrustSource()
}

type SecureProtoMatcher struct{}
Expand Down Expand Up @@ -983,3 +985,107 @@ func (f *ContextValueFetcher) Fetch(req *bfe_basic.Request) (interface{}, error)

return req.GetContext(f.key), nil
}

// time range matcher
type TimeMatcher struct {
startTime time.Time
endTime time.Time
}

func NewTimeMatcher(startTimeStr string, endTimeStr string) (*TimeMatcher, error) {
startTime, err := bfe_util.ParseTime(startTimeStr)
if err != nil {
return nil, fmt.Errorf("startTime format invalid, err:%s", err.Error())
}
endTime, err := bfe_util.ParseTime(endTimeStr)
if err != nil {
return nil, fmt.Errorf("endTime format invalid, err:%s", err.Error())
}
if startTime.After(endTime) {
return nil, fmt.Errorf("startTime[%s] must <= endTime[%s]", startTimeStr, endTimeStr)
}
return &TimeMatcher{
startTime: startTime,
endTime: endTime,
}, nil
}

func (t *TimeMatcher) Match(v interface{}) bool {
tm, ok := v.(time.Time)
if !ok {
return false
}
if tm.Before(t.startTime) {
return false
}
if tm.After(t.endTime) {
return false
}
return true
}

type BfeTimeFetcher struct{}

// Fetch returns a time in UTC+0 time zone.
func (f *BfeTimeFetcher) Fetch(req *bfe_basic.Request) (interface{}, error) {
if req == nil || req.HttpRequest == nil {
return time.Now().In(time.UTC), nil
}
values, ok := req.HttpRequest.Header["X-Bfe-Debug-Time"]
if !ok {
return time.Now().In(time.UTC), nil
}
debugTimeStr := values[0]
debugTime, err := bfe_util.ParseTime(debugTimeStr)
if err != nil {
return nil, fmt.Errorf("debugTimeStr have invalid format, debugTimeStr:%s, :%s", debugTimeStr, err.Error())
}
return debugTime, nil
}

// periodic time range matcher
type PeriodicTimeMatcher struct {
startTime int // in seconds of a day
endTime int
offset int // timezone offset
}

// time string format: hhmmssZ, example 150405H, Z-> timezone defined in bfe_util.TimeZoneMap
func NewPeriodicTimeMatcher(startTimeStr, endTimeStr, periodStr string) (*PeriodicTimeMatcher, error) {
if periodStr != "" {
return nil, fmt.Errorf("periodStr is not supported, should not be set!")
}
ts1, offset1, err := bfe_util.ParseTimeOfDay(startTimeStr)
if err != nil {
return nil, fmt.Errorf("startTime format invalid, err:%s", err.Error())
}
startTime := ts1.Hour()*3600 + ts1.Minute()*60 + ts1.Second()
ts2, offset2, err := bfe_util.ParseTimeOfDay(endTimeStr)
if err != nil {
return nil, fmt.Errorf("endTime format invalid, err:%s", err.Error())
}
endTime := ts2.Hour()*3600 + ts2.Minute()*60 + ts2.Second()
if startTime > endTime {
return nil, fmt.Errorf("startTime[%s] must <= endTime[%s]", startTimeStr, endTimeStr)
}
if offset1 != offset2 {
return nil, fmt.Errorf("timezone of startime and endtime should be same!")
}
return &PeriodicTimeMatcher{
startTime: startTime,
endTime: endTime,
offset: offset1,
}, nil
}

func (t *PeriodicTimeMatcher) Match(v interface{}) bool {
tm, ok := v.(time.Time)
if !ok {
return false
}
// tm in UTC, convert it to correct time zone
tm = tm.In(time.FixedZone("zone", t.offset))
hour, minute, second := tm.Clock()
seconds := hour*3600 + minute*60 + second
return seconds >= t.startTime && seconds <= t.endTime
}

0 comments on commit ebcbb72

Please sign in to comment.