Commit 630f8262 authored by Tsaiilin's avatar Tsaiilin

format

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