Skip to content

Commit

Permalink
Acquire read lock when marshalling container/task.
Browse files Browse the repository at this point in the history
  • Loading branch information
fenxiong committed Dec 2, 2019
1 parent c613380 commit 917631f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions agent/api/container/json.go
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))
}
17 changes: 17 additions & 0 deletions agent/api/task/json.go
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))
}

0 comments on commit 917631f

Please sign in to comment.