-
Notifications
You must be signed in to change notification settings - Fork 137
/
debugger_mem.go
49 lines (40 loc) · 1.05 KB
/
debugger_mem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package logger
import (
"sync"
"time"
)
// MemDebugger is a in-memory based [Debugger] implementation.
//
// This implem is local only and is not suited for any multi-instance setup.
type MemDebugger struct {
domains *sync.Map
}
// NewMemDebugger instantiate a new [MemDebugger].
func NewMemDebugger() *MemDebugger {
return &MemDebugger{domains: new(sync.Map)}
}
// AddDomain adds the specified domain to the debug list.
func (m *MemDebugger) AddDomain(domain string, ttl time.Duration) error {
m.domains.Store(domain, time.Now().Add(ttl))
return nil
}
// RemoveDomain removes the specified domain from the debug list.
func (m *MemDebugger) RemoveDomain(domain string) error {
m.domains.Delete(domain)
return nil
}
// ExpiresAt returns the expiration time for this domain.
//
// If this domain is not in debug mode, it returns `nil`.
func (m *MemDebugger) ExpiresAt(domain string) *time.Time {
res, ok := m.domains.Load(domain)
if !ok {
return nil
}
t := res.(time.Time)
if time.Now().After(t) {
m.RemoveDomain(domain)
return nil
}
return &t
}