-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Acquire read lock when marshalling container/task.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package container | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// In order to override the MarshalJSON method while still using Go's marshalling logic inside, an alias type | ||
// is needed to avoid infinite recursion. | ||
type jContainer Container | ||
|
||
// MarshalJSON wraps Go's marshalling logic with a necessary read lock. | ||
func (c *Container) MarshalJSON() ([]byte, error) { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
|
||
return json.Marshal((*jContainer)(c)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package task | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// In order to override the MarshalJSON method while still using Go's marshalling logic inside, an alias type | ||
// is needed to avoid infinite recursion. | ||
type jTask Task | ||
|
||
// MarshalJSON wraps Go's marshalling logic with a necessary read lock. | ||
func (t *Task) MarshalJSON() ([]byte, error) { | ||
t.lock.RLock() | ||
defer t.lock.RUnlock() | ||
|
||
return json.Marshal((*jTask)(t)) | ||
} |