mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-02-05 09:10:27 +08:00
Implement json.Marshaler
interface for LimitConfig
This commit is contained in:
parent
9b2aa9d908
commit
575805d475
@ -1,6 +1,8 @@
|
|||||||
package rcmgr
|
package rcmgr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -50,4 +52,11 @@ func TestLimitConfigParser(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Contains(t, cfg.Peer, peerID)
|
require.Contains(t, cfg.Peer, peerID)
|
||||||
require.Equal(t, int64(4097), cfg.Peer[peerID].Memory)
|
require.Equal(t, int64(4097), cfg.Peer[peerID].Memory)
|
||||||
|
|
||||||
|
// Roundtrip
|
||||||
|
jsonBytes, err := json.Marshal(&cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
cfgAfterRoundTrip, err := readLimiterConfigFromJSON(bytes.NewReader(jsonBytes), defaults)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, cfg, cfgAfterRoundTrip)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package rcmgr
|
package rcmgr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
@ -136,6 +137,23 @@ type LimitConfig struct {
|
|||||||
Stream BaseLimit `json:",omitempty"`
|
Stream BaseLimit `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *LimitConfig) MarshalJSON() ([]byte, error) {
|
||||||
|
// we want to marshal the encoded peer id
|
||||||
|
encodedPeerMap := make(map[string]BaseLimit, len(cfg.Peer))
|
||||||
|
for p, v := range cfg.Peer {
|
||||||
|
encodedPeerMap[peer.Encode(p)] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
type Alias LimitConfig
|
||||||
|
return json.Marshal(&struct {
|
||||||
|
*Alias
|
||||||
|
Peer map[string]BaseLimit `json:",omitempty"`
|
||||||
|
}{
|
||||||
|
Alias: (*Alias)(cfg),
|
||||||
|
Peer: encodedPeerMap,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *LimitConfig) Apply(c LimitConfig) {
|
func (cfg *LimitConfig) Apply(c LimitConfig) {
|
||||||
cfg.System.Apply(c.System)
|
cfg.System.Apply(c.System)
|
||||||
cfg.Transient.Apply(c.Transient)
|
cfg.Transient.Apply(c.Transient)
|
||||||
|
Loading…
Reference in New Issue
Block a user