Skip to content

Commit

Permalink
add custom Duration type to handle duration string
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Nov 19, 2023
1 parent e1b1082 commit d209ff8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
46 changes: 37 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package diameter

import (
"encoding/json"
"errors"
"time"

log "github.com/sirupsen/logrus"
)

type CapacityExchangeConfig struct {
Expand All @@ -15,10 +14,11 @@ type CapacityExchangeConfig struct {
}

type DiameterConfig struct {
RequestTimeout *Duration `json:"requestTimeout,omitempty"`
MaxRetransmits *uint `json:"maxRetransmits,omitempty"`
RetransmitInterval *time.Duration `json:"retransmitInterval,omitempty"`
RetransmitInterval *Duration `json:"retransmitInterval,omitempty"`
EnableWatchdog *bool `json:"enableWatchdog,omitempty"`
WatchdogInterval *time.Duration `json:"watchdogInterval,omitempty"`
WatchdogInterval *Duration `json:"watchdogInterval,omitempty"`
WatchdogStream *uint `json:"watchdogStream,omitempty"`
CapacityExchange *CapacityExchangeConfig `json:"capacityExchange,omitempty"`
}
Expand All @@ -36,18 +36,16 @@ func processConfig(arg map[string]interface{}) (*DiameterConfig, error) {

setDiameterConfigDefaults(&config)

log.Infof("Config %+v\n", config)
log.Infof("CE Config %+v\n", config.CapacityExchange)

return &config, nil
}

func setDiameterConfigDefaults(config *DiameterConfig) {
// Default values
var defaultRequestTimeout = Duration{1 * time.Second}
var defaultMaxRetransmits uint = 1
var defaultRetransmitInterval = 1 * time.Second
var defaultRetransmitInterval = Duration{1 * time.Second}
var defaultEnableWatchdog = true
var defaultWatchdogInterval = 5 * time.Second
var defaultWatchdogInterval = Duration{5 * time.Second}
var defaultWatchdogStream uint = 0

var defaultVendorID uint32 = 13
Expand All @@ -56,6 +54,9 @@ func setDiameterConfigDefaults(config *DiameterConfig) {
var defaultOriginRealm = "origin.realm"

// Set defaults for DiameterConfig
if config.RequestTimeout == nil {
config.RequestTimeout = &defaultRequestTimeout
}
if config.MaxRetransmits == nil {
config.MaxRetransmits = &defaultMaxRetransmits
}
Expand Down Expand Up @@ -89,3 +90,30 @@ func setDiameterConfigDefaults(config *DiameterConfig) {
config.CapacityExchange.OriginRealm = &defaultOriginRealm
}
}

type Duration struct {
time.Duration
}

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}

switch value := v.(type) {
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
27 changes: 12 additions & 15 deletions diameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
)

type DiameterClient struct {
client *sm.Client
conn diam.Conn
hopIds map[uint32]chan *diam.Message
client *sm.Client
conn diam.Conn
hopIds map[uint32]chan *diam.Message
requestTimeout time.Duration
}

type DiameterMessage struct {
Expand Down Expand Up @@ -58,19 +59,20 @@ func (*Diameter) XClient(arg map[string]interface{}) (*DiameterClient, error) {
Dict: dict.Default,
Handler: mux,
MaxRetransmits: *config.MaxRetransmits,
RetransmitInterval: *config.RetransmitInterval,
RetransmitInterval: *&config.RetransmitInterval.Duration,
EnableWatchdog: *config.EnableWatchdog,
WatchdogInterval: *config.WatchdogInterval,
WatchdogInterval: *&config.WatchdogInterval.Duration,
WatchdogStream: *config.WatchdogStream,
AuthApplicationID: []*diam.AVP{
diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)),
},
}

return &DiameterClient{
client: client,
conn: nil,
hopIds: hopIds,
client: client,
conn: nil,
hopIds: hopIds,
requestTimeout: config.RequestTimeout.Duration,
}, nil
}

Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *DiameterClient) Connect(address string) error {
return nil
}

func (c *DiameterClient) Send(msg *DiameterMessage, requestTimeoutMillis int) (uint32, error) {
func (c *DiameterClient) Send(msg *DiameterMessage) (uint32, error) {

if c.conn == nil {
return 0, errors.New("Not connected")
Expand All @@ -115,12 +117,7 @@ func (c *DiameterClient) Send(msg *DiameterMessage, requestTimeoutMillis int) (u
c.hopIds[hopByHopID] = make(chan *diam.Message)

// Timeout settings
var timeout <-chan time.Time
if requestTimeoutMillis == 0 {
timeout = time.After(60 * time.Second)
} else {
timeout = time.After(time.Duration(requestTimeoutMillis) * time.Millisecond)
}
timeout := time.After(c.requestTimeout)

// Send CCR
_, err := req.WriteTo(c.conn)
Expand Down
4 changes: 1 addition & 3 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ dict.load("dict/extra.xml")

// Init Client
let client = diam.Client({
MaxRetransmits: 1,
EnableWatchdog: false,
requestTimeout: "50ms",
})

let dataType = diam.DataType()

export default function () {
Expand Down

0 comments on commit d209ff8

Please sign in to comment.