diff --git a/config.go b/config.go index 05d1b74..a4263ff 100644 --- a/config.go +++ b/config.go @@ -2,9 +2,8 @@ package diameter import ( "encoding/json" + "errors" "time" - - log "github.com/sirupsen/logrus" ) type CapacityExchangeConfig struct { @@ -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"` } @@ -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 @@ -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 } @@ -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") + } +} diff --git a/diameter.go b/diameter.go index 00792a5..6d120ac 100644 --- a/diameter.go +++ b/diameter.go @@ -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 { @@ -58,9 +59,9 @@ 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)), @@ -68,9 +69,10 @@ func (*Diameter) XClient(arg map[string]interface{}) (*DiameterClient, error) { } return &DiameterClient{ - client: client, - conn: nil, - hopIds: hopIds, + client: client, + conn: nil, + hopIds: hopIds, + requestTimeout: config.RequestTimeout.Duration, }, nil } @@ -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") @@ -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) diff --git a/example/example.js b/example/example.js index 2d08eda..04d8c34 100644 --- a/example/example.js +++ b/example/example.js @@ -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 () {