mirror of https://github.com/rocboss/paopao-ce
pull/385/head
parent
2889228fef
commit
771a942b67
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2023 ROC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/alimy/tryst/event"
|
||||||
|
"github.com/alimy/tryst/pool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Event = event.Event
|
||||||
|
|
||||||
|
type EventManager interface {
|
||||||
|
Start()
|
||||||
|
Stop()
|
||||||
|
OnEvent(event Event)
|
||||||
|
}
|
||||||
|
|
||||||
|
type simpleEventManager struct {
|
||||||
|
em event.EventManager
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleEventManager) Start() {
|
||||||
|
s.em.Start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleEventManager) Stop() {
|
||||||
|
s.em.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleEventManager) OnEvent(event Event) {
|
||||||
|
s.em.OnEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEventManager(fn pool.RespFn[Event], opts ...pool.Option) EventManager {
|
||||||
|
return &simpleEventManager{
|
||||||
|
em: event.NewEventManager(fn, opts...),
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
// Copyright 2023 ROC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/alimy/tryst/event"
|
||||||
|
"github.com/alimy/tryst/pool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type simpleMetricManager struct {
|
||||||
|
mm event.EventManager
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleMetricManager) Start() {
|
||||||
|
s.mm.Start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleMetricManager) Stop() {
|
||||||
|
s.mm.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *simpleMetricManager) OnMeasure(metric Metric) {
|
||||||
|
s.mm.OnEvent(metric)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMetricManager(fn pool.RespFn[Metric], opts ...pool.Option) MetricManager {
|
||||||
|
return &simpleMetricManager{
|
||||||
|
mm: event.NewEventManager(fn, opts...),
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2023 ROC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package chain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rocboss/paopao-ce/internal/conf"
|
||||||
|
"github.com/rocboss/paopao-ce/internal/core"
|
||||||
|
"github.com/rocboss/paopao-ce/internal/metrics"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OnlineUserMetric struct {
|
||||||
|
metrics.BaseMetric
|
||||||
|
ac core.AppCache
|
||||||
|
uid int64
|
||||||
|
expire int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnUserOnlineMetric(ac core.AppCache, uid int64) {
|
||||||
|
metrics.OnMeasure(&OnlineUserMetric{
|
||||||
|
ac: ac,
|
||||||
|
uid: uid,
|
||||||
|
expire: conf.CacheSetting.OnlineUserExpire,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *OnlineUserMetric) Name() string {
|
||||||
|
return "OnlineUserMetric"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *OnlineUserMetric) Action() (err error) {
|
||||||
|
// 暂时仅做标记,不存储其他相关信息
|
||||||
|
m.ac.Set(conf.KeyOnlineUser.Get(m.uid), []byte{}, m.expire)
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in new issue