Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added timeout channel for Client Send #3

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions diameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ func (c *DiameterClient) Connect(address string) error {
return nil
}

func (d *Diameter) Send(client *DiameterClient, msg *DiameterMessage) (uint32, error) {
func (d *Diameter) Send(
client *DiameterClient,
msg *DiameterMessage,
requestTimeoutMillis int,
) (uint32, error) {

if client.conn == nil {
return 0, errors.New("Not connected")
Expand All @@ -113,14 +117,27 @@ func (d *Diameter) Send(client *DiameterClient, msg *DiameterMessage) (uint32, e
hopByHopID := req.Header.HopByHopID
client.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)
}

// Send CCR
_, err := req.WriteTo(client.conn)
if err != nil {
return uint32(0), err
}

// Wait for CCA
resp := <-client.hopIds[hopByHopID]
var resp *diam.Message
select {
case resp = <-client.hopIds[hopByHopID]:
case <-timeout:
return uint32(5012), errors.New("Response timeout")
}
//log.Infof("Received CCA \n%s", resp)

delete(client.hopIds, hopByHopID)
Expand Down
Loading