diff --git a/go.mod b/go.mod index bd742f3..66edf6c 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,29 @@ module github.com/whatap/golib -go 1.14 +go 1.17 require ( - github.com/BurntSushi/toml v0.3.1 // indirect github.com/google/uuid v1.3.0 - github.com/kr/pretty v0.3.0 // indirect github.com/magiconair/properties v1.8.7 github.com/natefinch/lumberjack v2.0.0+incompatible github.com/shirou/gopsutil v3.21.11+incompatible github.com/stretchr/testify v1.8.1 + golang.org/x/text v0.7.0 +) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/kr/pretty v0.3.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect + github.com/tklauser/numcpus v0.6.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect - golang.org/x/text v0.7.0 + golang.org/x/sys v0.2.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/lang/EventLevel.go b/lang/EventLevel.go new file mode 100644 index 0000000..0319763 --- /dev/null +++ b/lang/EventLevel.go @@ -0,0 +1,11 @@ +package lang + +const ( + EVENT_LEVEL_CRITICAL = 30 + EVENT_LEVEL_WARNING = 20 + EVENT_LEVEL_INFO = 10 + EVENT_LEVEL_NONE = 0 +) + +var EventLevelName = map[int]string{30: "CRITICAL", 20: "WARNING", 10: "INFO", 0: "NONE"} +var EventLevelValue = map[string]int{"CRITICAL": 30, "WARNING": 20, "INFO": 10, "NONE": 0} diff --git a/lang/MessageConst.go b/lang/MessageConst.go new file mode 100644 index 0000000..ebf44b4 --- /dev/null +++ b/lang/MessageConst.go @@ -0,0 +1,26 @@ +package lang + +const ( + MESSAGE_FILE_WRITE_OPEN = "FileWriteOpen" + MESSAGE_FILE_READ_OPEN = "FileReadOpen" + MESSAGE_JSP = "JSP" + MESSAGE_CLOSE_CONNECTION = "Close Connection" + MESSAGE_TRANSACTION_BACKSTACK = "TRANSACTION BACKSTACK" + MESSAGE_PREPARED_SQL = "prepare-sql" + MESSAGE_OPEN_CONNECTION_STACK = "Open Connection Stack" + MESSAGE_CAP_ARG = "TraceArgs" + MESSAGE_CAP_RETURN = "TraceReturn" + MESSAGE_CAP_THIS = "TraceConstructor" + MESSAGE_CONN_ALLOC_DUP = "Connection allocated to multi-threads" + MESSAGE_THREAD_START = "Thread-Start" + MESSAGE_SOCKET_OPEN = "SocketOpen" + MESSAGE_CUSTOM_OPEN = "CustomOpen" + MESSAGE_CUSTOM_CLOSE = "CustomClose" + MESSAGE_ASYNC_CTX = "AsyncCtx" + MESSAGE_EXCEPTION = "EXCEPTION" + MESSAGE_THREAD_CHILD = "Child-Thread-Call" + MESSAGE_ORIGIN_URL = "OriginURL" + MESSAGE_MESSAGE = "MESSAGE" + MESSAGE_METHOD_DEBUG = "Debug-Method" + MESSAGE_VIRTUAL_TX = "Virtual-Tx" +) diff --git a/lang/pack/AbstractPack.go b/lang/pack/AbstractPack.go index fd47e38..1769390 100644 --- a/lang/pack/AbstractPack.go +++ b/lang/pack/AbstractPack.go @@ -68,6 +68,16 @@ func (this *AbstractPack) SetONODE(onode int32) { this.Onode = onode } +// Get Time +func (this *AbstractPack) GetTime() int64 { + return this.Time +} + +// Set Time +func (this *AbstractPack) SetTime(t int64) { + this.Time = t +} + func (this *AbstractPack) ToString() string { return fmt.Sprintln("\nPcode=", this.Pcode, ",Oid=", this.Oid, ",Okind=", this.Okind, ",ONode=", this.Onode, ",Time=", this.Time) } diff --git a/lang/pack/LogSinkZipPack.go b/lang/pack/LogSinkZipPack.go new file mode 100644 index 0000000..1014b0e --- /dev/null +++ b/lang/pack/LogSinkZipPack.go @@ -0,0 +1,103 @@ +package pack + +import ( + "github.com/whatap/golib/io" + "github.com/whatap/golib/util/compressutil" + "github.com/whatap/golib/util/stringutil" +) + +const ( + ZIPPED = 1 + UN_ZIPPED = 0 +) + +type LogSinkZipPack struct { + AbstractPack + + Records []byte + RecordCount int + Status byte +} + +func NewLogSinkZipPack() *LogSinkZipPack { + p := new(LogSinkZipPack) + return p +} + +func (this *LogSinkZipPack) GetPackType() int16 { + return PACK_LOGSINK_ZIP +} + +func (this *LogSinkZipPack) Write(dout *io.DataOutputX) { + this.AbstractPack.Write(dout) + + dout.WriteByte(this.Status) + dout.WriteDecimal(int64(this.RecordCount)) + dout.WriteBlob(this.Records) +} +func (this *LogSinkZipPack) Read(din *io.DataInputX) { + this.AbstractPack.Read(din) + + this.Status = din.ReadByte() + this.RecordCount = int(din.ReadDecimal()) + this.Records = din.ReadBlob() +} + +func (this *LogSinkZipPack) SetRecords(records []byte, zipMinSize int) { + this.Records, _ = this.doZip(records, zipMinSize) +} + +func (this *LogSinkZipPack) doZip(records []byte, zipMinSize int) ([]byte, error) { + if this.Status != UN_ZIPPED { + return records, nil + } + if len(records) < zipMinSize { + return records, nil + } + this.Status = ZIPPED + return compressutil.DoZip(records) +} + +func (this *LogSinkZipPack) doUnZip() ([]byte, error) { + if this.Status != ZIPPED { + return this.Records, nil + } + return compressutil.UnZip(this.Records) +} + +func (this *LogSinkZipPack) GetRecords() []*LogSinkPack { + items := make([]*LogSinkPack, 0) + if this.Records == nil { + return items + } + data, err := this.doUnZip() + if err != nil { + return items + } + + in := io.NewDataInputX(data) + for i := 0; i < this.RecordCount; i++ { + tmp := ReadPack(in) + if tmp != nil { + if p, ok := tmp.(*LogSinkPack); ok { + p.Pcode = this.Pcode + p.Oid = this.Oid + p.Okind = this.Okind + p.Onode = this.Onode + // time 은 자기시간 사용 + items = append(items, p) + } + } + } + + return items + +} + +func (this *LogSinkZipPack) ToString() string { + sb := stringutil.NewStringBuffer() + sb.Append("LogSinkZipPack ") + sb.Append(this.AbstractPack.ToString()) + sb.AppendFormat("records=%d bytes", len(this.Records)) + return sb.ToString() +} diff --git a/lang/pack/Pack.go b/lang/pack/Pack.go index b7bd51a..14c6156 100644 --- a/lang/pack/Pack.go +++ b/lang/pack/Pack.go @@ -77,6 +77,8 @@ const ( PACK_WEB_CHECK_COUNT = 0x1707 PACK_LOGSINK = 0x170a PACK_ZIP = 0x170b + PACK_AGENT_MAPPING = 0x170c + PACK_LOGSINK_ZIP = 0x170d // // 12288 // PACK_SM_BASE = 0x3000 @@ -124,6 +126,10 @@ type Pack interface { GetPCODE() int64 SetOKIND(okind int32) SetONODE(onode int32) + + // Time interface 추가 + GetTime() int64 + SetTime(t int64) } func CreatePack(t int16) Pack { diff --git a/lang/pack/ProfileStepSplitPack.go b/lang/pack/ProfileStepSplitPack.go index cf0cb0e..d35ba8d 100644 --- a/lang/pack/ProfileStepSplitPack.go +++ b/lang/pack/ProfileStepSplitPack.go @@ -43,15 +43,13 @@ func (this *ProfileStepSplitPack) Write(dout *io.DataOutputX) { dout.WriteBlob(this.Steps) } -func (this *ProfileStepSplitPack) Read(din *io.DataInputX) *ProfileStepSplitPack { +func (this *ProfileStepSplitPack) Read(din *io.DataInputX) { this.AbstractPack.Read(din) - // ver din.ReadByte() this.Txid = din.ReadLong() this.Inx = int(din.ReadDecimal()) this.Steps = din.ReadBlob() - return this } func (this *ProfileStepSplitPack) SetProfile(steps []step.Step) *ProfileStepSplitPack { diff --git a/lang/pack/udp/UdpTxEndPack.go b/lang/pack/udp/UdpTxEndPack.go index 1d85715..8808922 100644 --- a/lang/pack/udp/UdpTxEndPack.go +++ b/lang/pack/udp/UdpTxEndPack.go @@ -131,6 +131,10 @@ func (this *UdpTxEndPack) Write(dout *io.DataOutputX) { dout.WriteTextShortLength(this.McallerUrl) dout.WriteTextShortLength(this.McallerPoidKey) } + + if this.Ver >= 10107 { + dout.WriteTextShortLength(stringutil.ParseStringZeroToEmpty(int64(this.Status))) + } } } @@ -185,6 +189,11 @@ func (this *UdpTxEndPack) Read(din *io.DataInputX) { this.McallerUrl = din.ReadTextShortLength() this.McallerPoidKey = din.ReadTextShortLength() } + + if this.Ver >= 10107 { + // reponse code + this.Status = stringutil.ParseInt32(din.ReadTextShortLength()) + } } } func (this *UdpTxEndPack) Process() { diff --git a/lang/service/TxRecord.go b/lang/service/TxRecord.go index c087b85..68b59ff 100644 --- a/lang/service/TxRecord.go +++ b/lang/service/TxRecord.go @@ -2,6 +2,7 @@ package service import ( "github.com/whatap/golib/io" + "github.com/whatap/golib/lang/value" "github.com/whatap/golib/util/hash" ) @@ -55,7 +56,7 @@ type TxRecord struct { HttpMethod byte Domain int32 - Fields []*FIELD + Fields *value.MapValue Login int32 @@ -71,6 +72,11 @@ type TxRecord struct { //20210628 - Apdex byte + + McallerStepId int64 + OriginUrl string + + StepSplitCount int } func NewTxRecord() *TxRecord { @@ -145,11 +151,22 @@ func (this *TxRecord) Write(dout *io.DataOutputX) { if this.Fields == nil { o.WriteByte(0) } else { - sz := len(this.Fields) + sz := this.Fields.Size() o.WriteByte(byte(sz)) - for i := 0; i < sz; i++ { - o.WriteByte(byte(this.Fields[i].Id)) - o.WriteText(this.Fields[i].Value) + keys := this.Fields.Keys() + for keys.HasMoreElements() { + key := keys.NextString() + tmp := this.Fields.Get(key) + if tmp != nil { + if v, ok := tmp.(value.Value); ok { + o.WriteText(key) + value.WriteValue(o, v) + } + } else { + // empty string + o.WriteText(key) + value.WriteValue(o, value.NewTextValue("")) + } } } o.WriteDecimal(int64(this.Login)) @@ -171,6 +188,12 @@ func (this *TxRecord) Write(dout *io.DataOutputX) { // 2021.06.28 , java //2021.05.13 o.WriteByte(this.Apdex) + // 2023.07.17 , java //2021.12.10 + o.WriteDecimal(this.McallerStepId) + o.WriteText(this.OriginUrl) + + o.WriteDecimal(int64(this.StepSplitCount)) + ////////////// BLOB /////////////// dout.WriteBlob(o.ToByteArray()) } @@ -252,12 +275,11 @@ func (this *TxRecord) Read(din *io.DataInputX) *TxRecord { n := int(in.ReadByte()) if n > 0 { - this.Fields = make([]*FIELD, n) + this.Fields = value.NewMapValue() for i := 0; i < n; i++ { - field := NewFIELD() - field.Id = in.ReadByte() - field.Value = in.ReadText() - this.Fields[i] = field + key := in.ReadText() + v := value.ReadValue(in) + this.Fields.Put(key, v) } } //if in.Available() > 0 { @@ -286,6 +308,13 @@ func (this *TxRecord) Read(din *io.DataInputX) *TxRecord { //if in.Available() > 0 { this.Apdex = in.ReadByte() + //if (in.available() > 0) { + this.McallerStepId = in.ReadDecimal() + this.OriginUrl = in.ReadText() + + //if (in.available() > 0) { + this.StepSplitCount = int(in.ReadDecimal()) + return this } diff --git a/lang/service/WebMethod.go b/lang/service/WebMethod.go index 91e307d..dccf33d 100644 --- a/lang/service/WebMethod.go +++ b/lang/service/WebMethod.go @@ -1,13 +1,15 @@ package service -import () - const ( - GET byte = 1 - POST byte = 2 - PUT byte = 3 - DELETE byte = 4 + GET byte = 1 + POST byte = 2 + PUT byte = 3 + DELETE byte = 4 + PATCH byte = 5 + OPTIONS byte = 6 + HEAD byte = 7 + TRACE byte = 8 ) -var WebMethodName = map[string]byte{"GET": 1, "POST": 2, "PUT": 3, "DELETE": 4} -var WebMethodValue = map[byte]string{1: "GET", 2: "POST", 3: "PUT", 4: "DELETE"} +var WebMethodName = map[string]byte{"GET": 1, "POST": 2, "PUT": 3, "DELETE": 4, "PATCH": 5, "OPTIONS": 6, "HEAD": 7, "TRACE": 8} +var WebMethodValue = map[byte]string{1: "GET", 2: "POST", 3: "PUT", 4: "DELETE", 5: "PATCH", 6: "OPTIONS", 7: "HEAD", 8: "TRACE"} diff --git a/lang/step/Step.go b/lang/step/Step.go index 8c384da..b47f3cc 100644 --- a/lang/step/Step.go +++ b/lang/step/Step.go @@ -29,6 +29,7 @@ type Step interface { // interface 함수 추가 GetStartTime() int32 + SetStartTime(t int32) SetParent(v int32) GetParent() int32 @@ -77,6 +78,9 @@ type AbstractStep struct { func (this *AbstractStep) GetStartTime() int32 { return this.StartTime } +func (this *AbstractStep) SetStartTime(t int32) { + this.StartTime = t +} func (this *AbstractStep) SetParent(v int32) { this.Parent = v diff --git a/logger/logfile/logs/RUM-rumctl-20230821.log b/logger/logfile/logs/RUM-rumctl-20230821.log new file mode 100644 index 0000000..8e857b4 --- /dev/null +++ b/logger/logfile/logs/RUM-rumctl-20230821.log @@ -0,0 +1,126 @@ +2023/08/21 05:28:21 +2023/08/21 05:28:21 ## OPEN LOG FILE rumctl 20230821 05:28:21.798 ## +2023/08/21 05:28:21 +2023/08/21 05:28:21 [tytttt] [tytttt] + +2023/08/21 05:28:21 [Error] Error + +2023/08/21 05:28:21 +2023/08/21 05:28:21 ## OPEN LOG FILE rumctl 20230821 05:28:21.798 ## +2023/08/21 05:28:21 +2023/08/21 05:28:21 [tytttt] [tytttt] + +2023/08/21 05:28:21 [Error] Error + +2023/08/21 05:28:21 +2023/08/21 05:28:21 ## OPEN LOG FILE rumctl 20230821 05:28:21.798 ## +2023/08/21 05:28:21 +2023/08/21 05:28:21 [tytttt] [tytttt] + +2023/08/21 05:28:21 [Error] Error + +2023/08/21 05:32:13 +2023/08/21 05:32:13 ## OPEN LOG FILE rumctl 20230821 05:32:13.800 ## +2023/08/21 05:32:13 +2023/08/21 05:32:13 [tytttt] [tytttt] + +2023/08/21 05:32:13 [Error] Error + +2023/08/21 05:32:13 +2023/08/21 05:32:13 ## OPEN LOG FILE rumctl 20230821 05:32:13.800 ## +2023/08/21 05:32:13 +2023/08/21 05:32:13 [tytttt] [tytttt] + +2023/08/21 05:32:13 [Error] Error + +2023/08/21 05:32:13 +2023/08/21 05:32:13 ## OPEN LOG FILE rumctl 20230821 05:32:13.800 ## +2023/08/21 05:32:13 +2023/08/21 05:32:13 [tytttt] [tytttt] + +2023/08/21 05:32:13 [Error] Error + +2023/08/21 05:36:17 +2023/08/21 05:36:17 ## OPEN LOG FILE rumctl 20230821 05:36:17.804 ## +2023/08/21 05:36:17 +2023/08/21 05:36:17 [tytttt] [tytttt] + +2023/08/21 05:36:17 [Error] Error + +2023/08/21 05:36:17 +2023/08/21 05:36:17 ## OPEN LOG FILE rumctl 20230821 05:36:17.804 ## +2023/08/21 05:36:17 +2023/08/21 05:36:17 [tytttt] [tytttt] + +2023/08/21 05:36:17 [Error] Error + +2023/08/21 05:36:17 +2023/08/21 05:36:17 ## OPEN LOG FILE rumctl 20230821 05:36:17.804 ## +2023/08/21 05:36:17 +2023/08/21 05:36:17 [tytttt] [tytttt] + +2023/08/21 05:36:17 [Error] Error + +2023/08/21 05:40:19 +2023/08/21 05:40:19 ## OPEN LOG FILE rumctl 20230821 05:40:19.471 ## +2023/08/21 05:40:19 +2023/08/21 05:40:19 [tytttt] [tytttt] + +2023/08/21 05:40:19 [Error] Error + +2023/08/21 05:40:19 +2023/08/21 05:40:19 ## OPEN LOG FILE rumctl 20230821 05:40:19.471 ## +2023/08/21 05:40:19 +2023/08/21 05:40:19 [tytttt] [tytttt] + +2023/08/21 05:40:19 [Error] Error + +2023/08/21 05:40:19 +2023/08/21 05:40:19 ## OPEN LOG FILE rumctl 20230821 05:40:19.471 ## +2023/08/21 05:40:19 +2023/08/21 05:40:19 [tytttt] [tytttt] + +2023/08/21 05:40:19 [Error] Error + +2023/08/21 05:44:16 +2023/08/21 05:44:16 ## OPEN LOG FILE rumctl 20230821 05:44:16.430 ## +2023/08/21 05:44:16 +2023/08/21 05:44:16 [tytttt] [tytttt] + +2023/08/21 05:44:16 [Error] Error + +2023/08/21 05:44:16 +2023/08/21 05:44:16 ## OPEN LOG FILE rumctl 20230821 05:44:16.430 ## +2023/08/21 05:44:16 +2023/08/21 05:44:16 [tytttt] [tytttt] + +2023/08/21 05:44:16 [Error] Error + +2023/08/21 05:44:16 +2023/08/21 05:44:16 ## OPEN LOG FILE rumctl 20230821 05:44:16.431 ## +2023/08/21 05:44:16 +2023/08/21 05:44:16 [tytttt] [tytttt] + +2023/08/21 05:44:16 [Error] Error + +2023/08/21 05:48:16 +2023/08/21 05:48:16 ## OPEN LOG FILE rumctl 20230821 05:48:16.511 ## +2023/08/21 05:48:16 +2023/08/21 05:48:16 [tytttt] [tytttt] + +2023/08/21 05:48:16 [Error] Error + +2023/08/21 05:48:16 +2023/08/21 05:48:16 ## OPEN LOG FILE rumctl 20230821 05:48:16.511 ## +2023/08/21 05:48:16 +2023/08/21 05:48:16 [tytttt] [tytttt] + +2023/08/21 05:48:16 [Error] Error + +2023/08/21 05:48:16 +2023/08/21 05:48:16 ## OPEN LOG FILE rumctl 20230821 05:48:16.511 ## +2023/08/21 05:48:16 +2023/08/21 05:48:16 [tytttt] [tytttt] + +2023/08/21 05:48:16 [Error] Error + diff --git a/logger/logfile/logs/whatap-boot-20230821.log b/logger/logfile/logs/whatap-boot-20230821.log new file mode 100644 index 0000000..cb9cbd3 --- /dev/null +++ b/logger/logfile/logs/whatap-boot-20230821.log @@ -0,0 +1,42 @@ +2023/08/21 05:28:21 +2023/08/21 05:28:21 ## OPEN LOG FILE boot 20230821 05:28:21.798 ## +2023/08/21 05:28:21 +2023/08/21 05:28:21 [aaa] [aaa] + +2023/08/21 05:28:21 [Error] Error + +2023/08/21 05:32:13 +2023/08/21 05:32:13 ## OPEN LOG FILE boot 20230821 05:32:13.800 ## +2023/08/21 05:32:13 +2023/08/21 05:32:13 [aaa] [aaa] + +2023/08/21 05:32:13 [Error] Error + +2023/08/21 05:36:17 +2023/08/21 05:36:17 ## OPEN LOG FILE boot 20230821 05:36:17.804 ## +2023/08/21 05:36:17 +2023/08/21 05:36:17 [aaa] [aaa] + +2023/08/21 05:36:17 [Error] Error + +2023/08/21 05:40:19 +2023/08/21 05:40:19 ## OPEN LOG FILE boot 20230821 05:40:19.471 ## +2023/08/21 05:40:19 +2023/08/21 05:40:19 [aaa] [aaa] + +2023/08/21 05:40:19 [Error] Error + +2023/08/21 05:44:16 +2023/08/21 05:44:16 ## OPEN LOG FILE boot 20230821 05:44:16.430 ## +2023/08/21 05:44:16 +2023/08/21 05:44:16 [aaa] [aaa] + +2023/08/21 05:44:16 [Error] Error + +2023/08/21 05:48:16 +2023/08/21 05:48:16 ## OPEN LOG FILE boot 20230821 05:48:16.511 ## +2023/08/21 05:48:16 +2023/08/21 05:48:16 [aaa] [aaa] + +2023/08/21 05:48:16 [Error] Error + diff --git a/logsink/zip/DefaultZipMod.go b/logsink/zip/DefaultZipMod.go index b54fc6b..df6f8c2 100644 --- a/logsink/zip/DefaultZipMod.go +++ b/logsink/zip/DefaultZipMod.go @@ -3,6 +3,7 @@ package zip import ( "bytes" "compress/gzip" + "fmt" "io/ioutil" ) @@ -18,29 +19,33 @@ func (this *DefaultZipMod) ID() byte { return ZIP_MOD_DEFULAT_GZIP } -func (this *DefaultZipMod) Compress(in []byte) ([]byte, error) { +func (this *DefaultZipMod) Compress(in []byte) (output []byte, err error) { + if in == nil { + err = fmt.Errorf("error input data is nil ") + return + } buf := new(bytes.Buffer) - func() { - gz := gzip.NewWriter(buf) - defer gz.Close() - _, err := gz.Write(in) - if err != nil { - panic(err) - } - }() + gz := gzip.NewWriter(buf) + gz.Write(in) + gz.Flush() - return buf.Bytes(), nil + // gz.Close 가 호출 되어야만 buf.Bytes 내용이 정상 출력 됨 + err = gz.Close() + if err == nil { + output = buf.Bytes() + } + return } func (this *DefaultZipMod) Decompress(in []byte) ([]byte, error) { r, err := gzip.NewReader(ioutil.NopCloser(bytes.NewBuffer(in))) if err != nil { - return nil, err + return make([]byte, 0), err } defer r.Close() if b, err := ioutil.ReadAll(r); err != nil { - return nil, err + return make([]byte, 0), err } else { return b, nil } diff --git a/main.go b/main.go index 1d00684..9e79be5 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( _ "github.com/whatap/golib/net" _ "github.com/whatap/golib/net/oneway" _ "github.com/whatap/golib/net/udp" + _ "github.com/whatap/golib/util/ansi" _ "github.com/whatap/golib/util/bitutil" _ "github.com/whatap/golib/util/cmdutil" _ "github.com/whatap/golib/util/compare" diff --git a/util/compressutil/CompressUtil.go b/util/compressutil/CompressUtil.go new file mode 100644 index 0000000..8a0290f --- /dev/null +++ b/util/compressutil/CompressUtil.go @@ -0,0 +1,40 @@ +package compressutil + +import ( + "bytes" + "compress/gzip" + "fmt" + "io/ioutil" +) + +func DoZip(in []byte) (output []byte, err error) { + if in == nil { + err = fmt.Errorf("error input data is nil ") + return + } + buf := new(bytes.Buffer) + + gz := gzip.NewWriter(buf) + gz.Write(in) + gz.Flush() + + // gz.Close 가 호출 되어야만 buf.Bytes 내용이 정상 출력 됨 + err = gz.Close() + if err == nil { + output = buf.Bytes() + } + return +} + +func UnZip(in []byte) ([]byte, error) { + r, err := gzip.NewReader(ioutil.NopCloser(bytes.NewBuffer(in))) + if err != nil { + return make([]byte, 0), err + } + defer r.Close() + if b, err := ioutil.ReadAll(r); err != nil { + return make([]byte, 0), err + } else { + return b, nil + } +} diff --git a/util/dateutil/DateSyncTime.go b/util/dateutil/DateSyncTime.go index 408495b..e1f507e 100644 --- a/util/dateutil/DateSyncTime.go +++ b/util/dateutil/DateSyncTime.go @@ -2,41 +2,100 @@ package dateutil import ( // "fmt" + "sync" "time" ) +// #include +// //#include +// //int usleep(useconds_t usec); +import "C" + const ( TIME_SYNC_INTERVAL = 5000 ) var SyncTimeMillis int64 +var SyncTime time.Time var lastSyncTime int64 var syncTimeTicker *time.Ticker +var syncTimeLock sync.Mutex func StartSyncTime() { + syncTimeLock.Lock() + defer syncTimeLock.Unlock() + if syncTimeTicker != nil { + return + } + syncTimeTicker = time.NewTicker(time.Millisecond) + clock() SyncTimeMillis = systemMillis() lastSyncTime = SyncTimeMillis - clock() + } func StopSyncTime() { - syncTimeTicker.Stop() + syncTimeLock.Lock() + defer syncTimeLock.Unlock() + func() { + defer func() { + if r := recover(); r != nil { + } + }() + if syncTimeTicker != nil { + // can panic + syncTimeTicker.Stop() + } + }() +} + +func IsSyncTime() bool { + if syncTimeTicker != nil { + return true + } + return false } func clock() { - syncTimeTicker = time.NewTicker(time.Millisecond) go func() { + cnt := 0 + // per millisecond for t := range syncTimeTicker.C { - SyncTimeMillis = t.UnixNano() / 1000000 - if SyncTimeMillis > lastSyncTime+TIME_SYNC_INTERVAL { + cnt++ + SyncTime = t + if cnt > TIME_SYNC_INTERVAL { + cnt = 0 + SyncTime = time.Now() + now := systemMillis() + //fmt.Println(">>>> sync gap =", (now - SyncTimeMillis)) + lastSyncTime = now + SyncTimeMillis = now + } + } + }() +} + +func clockUsleep() { + go func() { + cnt := 0 + for { + cnt++ + SyncTime = time.Now() + if cnt > TIME_SYNC_INTERVAL { + cnt = 0 + SyncTime = time.Now() now := systemMillis() //fmt.Println(">>>> sync gap =", (now - SyncTimeMillis)) lastSyncTime = now SyncTimeMillis = now } + + C.usleep(1000) + } }() } + func systemMillis() int64 { return time.Now().UnixNano() / 1000000 } diff --git a/util/dateutil/DateUtil.go b/util/dateutil/DateUtil.go index 107e8fe..8f27214 100644 --- a/util/dateutil/DateUtil.go +++ b/util/dateutil/DateUtil.go @@ -1,11 +1,13 @@ package dateutil -//"log" -// "time" +import ( + //"log" + "time" +) -func init() { - StartSyncTime() -} +// func init() { +// StartSyncTime() +// } var helper = getDateTimeHelper("") @@ -47,8 +49,11 @@ func YmdNow() string { var delta int64 = 0 func SystemNow() int64 { - //return (time.Now().UnixNano() / 1000000) - return SyncTimeMillis + if IsSyncTime() { + return SyncTime.UnixNano() / 1000000 + } + return (time.Now().UnixNano() / 1000000) + //return SyncTimeMillis } func Now() int64 {