Verified Commit 928c63ad authored by Cui's avatar Cui

first commit

parents
.idea/
vendor/
module gitlab.qingclass.cn/cuiweiqiang/utils
require (
github.com/sirupsen/logrus v1.3.0
github.com/valyala/fasthttp v1.0.0
)
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.0.0 h1:BwIoZQbBsTo3v2F5lz5Oy3TlTq4wLKTLV260EVTEWco=
github.com/valyala/fasthttp v1.0.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:47
*/
package utils
import (
"io"
"sync"
"time"
"github.com/sirupsen/logrus"
)
// Hook represents a Logstash hook.
// It has two fields: writer to write the entry to Logstash and
// formatter to format the entry to a Logstash format before sending.
//
// To initialize it use the `New` function.
//
type Hook struct {
writer io.Writer
formatter logrus.Formatter
}
// New returns a new logrus.Hook for Logstash.
//
// To create a new hook that sends logs to `tcp://logstash.corp.io:9999`:
//
// conn, _ := net.Dial("tcp", "logstash.corp.io:9999")
// hook := logrustash.New(conn, logrustash.DefaultFormatter())
func NewHook(w io.Writer, f logrus.Formatter) logrus.Hook {
return Hook{
writer: w,
formatter: f,
}
}
// Fire takes, formats and sends the entry to Logstash.
// Hook's formatter is used to format the entry into Logstash format
// and Hook's writer is used to write the formatted entry to the Logstash instance.
func (h Hook) Fire(e *logrus.Entry) error {
dataBytes, err := h.formatter.Format(e)
if err != nil {
return err
}
_, err = h.writer.Write(dataBytes)
return err
}
// Levels returns all logrus levels.
func (h Hook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Using a pool to re-use of old entries when formatting Logstash messages.
// It is used in the Fire function.
var entryPool = sync.Pool{
New: func() interface{} {
return &logrus.Entry{}
},
}
// copyEntry copies the entry `e` to a new entry and then adds all the fields in `fields` that are missing in the new entry data.
// It uses `entryPool` to re-use allocated entries.
func copyEntry(e *logrus.Entry, fields logrus.Fields) *logrus.Entry {
ne := entryPool.Get().(*logrus.Entry)
ne.Message = e.Message
ne.Level = e.Level
ne.Time = e.Time
ne.Data = logrus.Fields{}
for k, v := range fields {
ne.Data[k] = v
}
for k, v := range e.Data {
ne.Data[k] = v
}
return ne
}
// releaseEntry puts the given entry back to `entryPool`. It must be called if copyEntry is called.
func releaseEntry(e *logrus.Entry) {
entryPool.Put(e)
}
// LogstashFormatter represents a Logstash format.
// It has logrus.Formatter which formats the entry and logrus.Fields which
// are added to the JSON message if not given in the entry data.
//
// Note: use the `DefaultFormatter` function to set a default Logstash formatter.
type LogstashFormatter struct {
logrus.Formatter
logrus.Fields
}
var (
logstashFields = logrus.Fields{"@version": "1", "type": "log"}
logstashFieldMap = logrus.FieldMap{
logrus.FieldKeyTime: "@timestamp",
logrus.FieldKeyMsg: "message",
}
)
// DefaultFormatter returns a default Logstash formatter:
// A JSON format with "@version" set to "1" (unless set differently in `fields`,
// "type" to "log" (unless set differently in `fields`),
// "@timestamp" to the log time and "message" to the log message.
//
// Note: to set a different configuration use the `LogstashFormatter` structure.
func DefaultFormatter(fields logrus.Fields) logrus.Formatter {
for k, v := range logstashFields {
if _, ok := fields[k]; !ok {
fields[k] = v
}
}
return LogstashFormatter{
Formatter: &logrus.JSONFormatter{FieldMap: logstashFieldMap},
Fields: fields,
}
}
// Format formats an entry to a Logstash format according to the given Formatter and Fields.
//
// Note: the given entry is copied and not changed during the formatting process.
func (f LogstashFormatter) Format(e *logrus.Entry) ([]byte, error) {
ne := copyEntry(e, f.Fields)
dataBytes, err := f.Formatter.Format(ne)
releaseEntry(ne)
return dataBytes, err
}
type CSTFormatter struct {
logrus.Formatter
}
func (u CSTFormatter) Format(e *logrus.Entry) ([]byte, error) {
timeZone := time.FixedZone("CST", 8*3600)
e.Time = e.Time.In(timeZone)
return u.Formatter.Format(e)
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:46
*/
package utils
import (
"net"
"github.com/sirupsen/logrus"
)
var Logger *logrus.Logger
func New(address, applicationName string) {
Logger = logrus.New()
conn, err := net.Dial("udp", address)
if err != nil {
panic(err)
}
hook := NewHook(conn, DefaultFormatter(logrus.Fields{
"application": applicationName,
}))
Logger.Hooks.Add(hook)
Logger.SetFormatter(CSTFormatter{&logrus.TextFormatter{}})
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:42
*/
package utils
import (
"crypto/rand"
"math/big"
)
func RandInt64(min, max int64) int64 {
maxBigInt := big.NewInt(max)
i, _ := rand.Int(rand.Reader, maxBigInt)
if i.Int64() < min {
RandInt64(min, max)
}
return i.Int64()
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:44
*/
package utils
import (
"testing"
)
func TestRandInt64(t *testing.T) {
res := RandInt64(0, 10)
if res < 10 && res > 10 {
t.Log("success")
} else {
t.Failed()
}
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:54
*/
package utils
import (
"crypto/tls"
"github.com/valyala/fasthttp"
"io"
"io/ioutil"
"net"
"net/http"
"time"
)
func HttpGet(uri string) ([]byte, int, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(uri)
if err != nil {
return nil, -1, err
}
respBody, _ := ioutil.ReadAll(resp.Body)
defer IOClose(resp.Body)
return respBody, resp.StatusCode, nil
}
func HttpGetIO(uri string) (io.ReadCloser, int, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(uri)
if err != nil {
return nil, -1, err
}
return resp.Body, resp.StatusCode, nil
}
func FasthttpPost(url string, bodyjson []byte) ([]byte, int, error) {
total := 500
req := &fasthttp.Request{}
resp := &fasthttp.Response{}
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.SetBody(bodyjson)
req.SetRequestURI(url)
c := &fasthttp.Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
Dial: func(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, time.Second*10)
},
MaxConnsPerHost: total,
}
err := c.Do(req, resp)
return resp.Body(), resp.StatusCode(), err
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:55
*/
package utils
import (
"testing"
)
func Test_FasthttpPost(t *testing.T) {
uri := "https://www.baidu.com"
_, status, err := FasthttpPost(uri, []byte("{\"hello\":\"world\"}"))
if status != 302 {
t.Error(status, err)
t.FailNow()
}
}
func Benchmark_FasthttpPost(b *testing.B) {
uri := "https://www.baidu.com"
for i := 0; i < b.N; i++ {
_, _, _ = FasthttpPost(uri, []byte("{\"hello\":\"world\"}"))
}
}
/*
* @Author: cuiweiqiang
* @Date: 2019-01-07 10:51
*/
package utils
import (
"github.com/sirupsen/logrus"
"io"
)
func IOClose(fio io.ReadCloser) {
log := Logger.WithFields(logrus.Fields{
"position": "utils/toos.go",
"func": "ioClose",
})
err := fio.Close()
if err != nil {
log.WithField("error", err).Error("io.ReadCloser close error")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment