Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Fail when there already is a pending sluice request for the local add…
Browse files Browse the repository at this point in the history
…ress
  • Loading branch information
dhaavi committed Oct 2, 2023
1 parent a2f1839 commit 2a6600b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 2 additions & 3 deletions sluice/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
defaultSluiceTTL = 5 * time.Minute
defaultSluiceTTL = 30 * time.Second
)

var (
Expand Down Expand Up @@ -44,12 +44,11 @@ func AwaitRequest(connInfo *network.Connection, callbackFn RequestCallbackFunc)
return fmt.Errorf("sluice for network %s %w", network, ErrSluiceOffline)
}

sluice.AwaitRequest(&Request{
return sluice.AwaitRequest(&Request{
ConnInfo: connInfo,
CallbackFn: callbackFn,
Expires: time.Now().Add(defaultSluiceTTL),
})
return nil
}

func getNetworkFromConnInfo(connInfo *network.Connection) string {
Expand Down
10 changes: 9 additions & 1 deletion sluice/sluice.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func StartSluice(network, address string) {
}

// AwaitRequest pre-registers a connection.
func (s *Sluice) AwaitRequest(r *Request) {
func (s *Sluice) AwaitRequest(r *Request) error {
// Set default expiry.
if r.Expires.IsZero() {
r.Expires = time.Now().Add(defaultSluiceTTL)
Expand All @@ -63,8 +63,16 @@ func (s *Sluice) AwaitRequest(r *Request) {
s.lock.Lock()
defer s.lock.Unlock()

// Check if a pending request already exists for this local address.
key := net.JoinHostPort(r.ConnInfo.LocalIP.String(), strconv.Itoa(int(r.ConnInfo.LocalPort)))
_, exists := s.pendingRequests[key]
if exists {
return fmt.Errorf("a pending request for %s already exists", key)
}

// Add to pending requests.
s.pendingRequests[key] = r
return nil
}

func (s *Sluice) getRequest(address string) (r *Request, ok bool) {
Expand Down

0 comments on commit 2a6600b

Please sign in to comment.