From 32cdfc3db442009676b67f2df0923241823ba0e7 Mon Sep 17 00:00:00 2001 From: atavism Date: Mon, 12 Aug 2024 08:40:31 -0700 Subject: [PATCH 1/2] lock masquerade during json.Marshal --- masquerade.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/masquerade.go b/masquerade.go index 0abade8..8bfd6bf 100644 --- a/masquerade.go +++ b/masquerade.go @@ -1,6 +1,7 @@ package fronted import ( + "encoding/json" "fmt" "hash/crc32" "net" @@ -48,6 +49,18 @@ type masquerade struct { mx sync.RWMutex } +func (m *masquerade) MarshalJSON() ([]byte, error) { + m.mx.RLock() + defer m.mx.RUnlock() + // Type alias for masquerade + type Alias masquerade + return json.Marshal(&struct { + *Alias + }{ + Alias: (*Alias)(m), + }) +} + func (m *masquerade) lastSucceeded() time.Time { m.mx.RLock() defer m.mx.RUnlock() From 318e3367a61dac40478c9b6fce63c1895c4fb0f3 Mon Sep 17 00:00:00 2001 From: atavism Date: Mon, 12 Aug 2024 08:47:20 -0700 Subject: [PATCH 2/2] Add comments --- masquerade.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/masquerade.go b/masquerade.go index 8bfd6bf..686ff93 100644 --- a/masquerade.go +++ b/masquerade.go @@ -49,16 +49,13 @@ type masquerade struct { mx sync.RWMutex } +// MarshalJSON marshals masquerade into json func (m *masquerade) MarshalJSON() ([]byte, error) { m.mx.RLock() defer m.mx.RUnlock() - // Type alias for masquerade - type Alias masquerade - return json.Marshal(&struct { - *Alias - }{ - Alias: (*Alias)(m), - }) + // Type alias for masquerade so that we don't infinitely recurse when marshaling the struct + type alias masquerade + return json.Marshal((*alias)(m)) } func (m *masquerade) lastSucceeded() time.Time {