update at 2019-11-22 18:11:26 by ehlxr
This commit is contained in:
49
pkg/limiter.go
Normal file
49
pkg/limiter.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
log "unknwon.dev/clog/v2"
|
||||
)
|
||||
|
||||
type LimiterServer struct {
|
||||
Interval time.Duration
|
||||
MaxCount int
|
||||
Lock sync.Mutex
|
||||
ReqCount int
|
||||
}
|
||||
|
||||
func NewLimiterServer(interval time.Duration, maxCount int) *LimiterServer {
|
||||
limiter := &LimiterServer{
|
||||
Interval: interval,
|
||||
MaxCount: maxCount,
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
for {
|
||||
<-ticker.C
|
||||
limiter.Lock.Lock()
|
||||
log.Info("Reset LimiterServer Count...")
|
||||
|
||||
limiter.ReqCount = 0
|
||||
limiter.Lock.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
return limiter
|
||||
}
|
||||
|
||||
func (limiter *LimiterServer) Increase() {
|
||||
limiter.Lock.Lock()
|
||||
defer limiter.Lock.Unlock()
|
||||
|
||||
limiter.ReqCount += 1
|
||||
}
|
||||
|
||||
func (limiter *LimiterServer) IsAvailable() bool {
|
||||
limiter.Lock.Lock()
|
||||
defer limiter.Lock.Unlock()
|
||||
|
||||
return limiter.ReqCount < limiter.MaxCount
|
||||
}
|
20
pkg/limiter_test.go
Normal file
20
pkg/limiter_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLimiter(t *testing.T) {
|
||||
limiter := NewLimiterServer(1*time.Second, 5)
|
||||
|
||||
for {
|
||||
if limiter.IsAvailable() {
|
||||
limiter.Increase()
|
||||
|
||||
t.Log("hello...", limiter.ReqCount)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
55
pkg/parser.go
Normal file
55
pkg/parser.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
Opts struct {
|
||||
AppName string `short:"n" long:"monitor-app-name" env:"MONITOR_APP_NAME" description:"The name of the application being monitored, which will be added to the content before"`
|
||||
File string `short:"f" long:"monitor-file" env:"MONITOR_FILE" description:"The file to be monitored" required:"true"`
|
||||
KeyWord string `short:"k" long:"search-keyword" env:"SEARCH_KEYWORD" description:"Keyword to be search for" default:"ERRO"`
|
||||
KeyWordIgnoreCase bool `short:"c" long:"keyword-case-sensitive" env:"KEYWORD_IGNORE_CASE" description:"Whether Keyword ignore case"`
|
||||
Version bool `short:"v" long:"version" description:"Show version info"`
|
||||
Robot Robot `group:"DingTalk Robot Options" namespace:"robot" env-namespace:"ROBOT" `
|
||||
}
|
||||
)
|
||||
|
||||
type Robot struct {
|
||||
Token string `short:"t" long:"token" env:"TOKEN" description:"DingTalk robot access token" required:"true"`
|
||||
Secret string `short:"s" long:"secret" env:"SECRET" description:"DingTalk robot secret"`
|
||||
AtMobiles []string `short:"m" long:"at-mobiles" env:"AT_MOBILES" env-delim:"," description:"The mobile of the person will be at"`
|
||||
IsAtAll bool `short:"a" long:"at-all" env:"AT_ALL" description:"Whether at everyone"`
|
||||
}
|
||||
|
||||
func ParseArg() {
|
||||
parser := flags.NewParser(&Opts, flags.HelpFlag|flags.PassDoubleDash)
|
||||
parser.NamespaceDelimiter = "-"
|
||||
|
||||
if AppName != "" {
|
||||
parser.Name = AppName
|
||||
}
|
||||
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
if Opts.Version {
|
||||
// -v
|
||||
PrintVersion()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
// -h
|
||||
_, _ = fmt.Fprintln(os.Stdout, err)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// err
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
|
||||
parser.WriteHelp(os.Stderr)
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
30
pkg/version.go
Normal file
30
pkg/version.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
AppName string
|
||||
Version string
|
||||
BuildTime string
|
||||
GitCommit string
|
||||
GoVersion string
|
||||
|
||||
versionTpl = `%s
|
||||
Name: %s
|
||||
Version: %s
|
||||
BuildTime: %s
|
||||
GitCommit: %s
|
||||
GoVersion: %s
|
||||
|
||||
`
|
||||
bannerBase64 = "DQogX18gIF9fICBfX19fXyAgXyAgXyAgX19fXyAgX19fXyAgX19fX18gIF9fX18gDQooICBcLyAgKSggIF8gICkoIFwoICkoXyAgXykoXyAgXykoICBfICApKCAgXyBcDQogKSAgICAoICApKF8pKCAgKSAgKCAgXykoXyAgICkoICAgKShfKSggICkgICAvDQooXy9cL1xfKShfX19fXykoXylcXykoX19fXykgKF9fKSAoX19fX18pKF8pXF8pDQo="
|
||||
)
|
||||
|
||||
// PrintVersion Print out version information
|
||||
func PrintVersion() {
|
||||
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
|
||||
fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion)
|
||||
}
|
Reference in New Issue
Block a user