Commit 630f8262 authored by Tsaiilin's avatar Tsaiilin

format

parent ec243b8b
...@@ -3,6 +3,7 @@ package trace ...@@ -3,6 +3,7 @@ package trace
import ( import (
"sync/atomic" "sync/atomic"
"time" "time"
"virjar.com/majora-go/safe"
"github.com/adamweixuan/getty" "github.com/adamweixuan/getty"
"go.uber.org/zap" "go.uber.org/zap"
...@@ -21,7 +22,7 @@ var ( ...@@ -21,7 +22,7 @@ var (
) )
func init() { func init() {
go func() { safe.SageGo(func() {
for { for {
e := <-sessionEventChan e := <-sessionEventChan
if e.Err != nil { if e.Err != nil {
...@@ -32,7 +33,7 @@ func init() { ...@@ -32,7 +33,7 @@ func init() {
zap.Any("event", e.Event)) zap.Any("event", e.Event))
} }
} }
}() })
} }
// Event 事件 // Event 事件
...@@ -63,36 +64,30 @@ type Recorder interface { ...@@ -63,36 +64,30 @@ type Recorder interface {
Enable() bool Enable() bool
} }
type NopRecorder struct{} type nopRecorder struct{}
func (n *NopRecorder) RecordEvent(eventName string, message string) { func (n *nopRecorder) RecordEvent(eventName string, message string) {
} }
func (n *NopRecorder) RecordErrorEvent(eventName string, message string, err error) { func (n *nopRecorder) RecordErrorEvent(eventName string, message string, err error) {
} }
func (n *NopRecorder) Enable() bool { func (n *nopRecorder) Enable() bool {
return false return false
} }
type RecorderImpl struct { type recorderImpl struct {
sessionId string sessionId string
} }
func NewRecorderImpl(sessionId string) *RecorderImpl {
return &RecorderImpl{
sessionId: sessionId,
}
}
func (r *RecorderImpl) RecordEvent(eventName string, message string) { func (r *recorderImpl) RecordEvent(eventName string, message string) {
r.RecordErrorEvent(eventName, message, nil) r.RecordErrorEvent(eventName, message, nil)
} }
func (r *RecorderImpl) RecordErrorEvent(eventName string, message string, err error) { func (r *recorderImpl) RecordErrorEvent(eventName string, message string, err error) {
event := &Event{ event := &Event{
Timestamp: time.Now(), Timestamp: time.Now(),
EventName: eventName, EventName: eventName,
...@@ -114,15 +109,15 @@ func (r *RecorderImpl) RecordErrorEvent(eventName string, message string, err er ...@@ -114,15 +109,15 @@ func (r *RecorderImpl) RecordErrorEvent(eventName string, message string, err er
sessionEventChan <- sessionEvent sessionEventChan <- sessionEvent
} }
func (r *RecorderImpl) Enable() bool { func (r *recorderImpl) Enable() bool {
return true return true
} }
var defaultNopRecorder = NopRecorder{} var defaultNopRecorder = nopRecorder{}
var slots = make([]int64, 30) var slots = make([]int64, 30)
func AcquireRecorder(sessionId string) Recorder { func acquireRecorder(sessionId string) Recorder {
now := time.Now() now := time.Now()
slotIndex := now.Minute() / 2 slotIndex := now.Minute() / 2
...@@ -135,17 +130,12 @@ func AcquireRecorder(sessionId string) Recorder { ...@@ -135,17 +130,12 @@ func AcquireRecorder(sessionId string) Recorder {
} }
if atomic.CompareAndSwapInt64(slot, slotTime, timeMinute) { if atomic.CompareAndSwapInt64(slot, slotTime, timeMinute) {
return &RecorderImpl{sessionId: sessionId} return &recorderImpl{sessionId: sessionId}
} }
return &defaultNopRecorder return &defaultNopRecorder
} }
type slot struct {
time int64
recorder Recorder
}
type Session struct { type Session struct {
Recorder Recorder Recorder Recorder
} }
...@@ -155,7 +145,7 @@ func NewSession(sessionId string) *Session { ...@@ -155,7 +145,7 @@ func NewSession(sessionId string) *Session {
sessionId = sessionIdNop sessionId = sessionIdNop
} }
return &Session{ return &Session{
Recorder: AcquireRecorder(sessionId), Recorder: acquireRecorder(sessionId),
} }
} }
......
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