github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/logger/debugger_mem.go (about)

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