Skip to content

Commit ce33903

Browse files
committed
connect: enforce contracts on forward create
1 parent 7d37fb4 commit ce33903

3 files changed

Lines changed: 37 additions & 23 deletions

File tree

connect/connect_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ func testConnect(
534534
switch contractTest {
535535
case contractTestSymmetric, contractTestAsymmetric:
536536
settings.ForwardEnforceActiveContracts = true
537+
default:
538+
settings.ForwardEnforceActiveContracts = false
537539
}
538540

539541
exchange := NewExchange(ctx, host, service, block, hostToServicePorts, routes, settings)

connect/resident.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func DefaultExchangeSettings() *ExchangeSettings {
144144
ExchangeResidentWaitTimeout: exchangeResidentWaitTimeout,
145145
ExchangeResidentPollTimeout: 15 * time.Second,
146146

147-
ForwardEnforceActiveContracts: false,
147+
ForwardEnforceActiveContracts: true,
148148

149149
ExchangeChaosSettings: *DefaultExchangeChaosSettings(),
150150
// default drain 300/minute
@@ -1583,37 +1583,36 @@ func (self *Resident) handleClientForward(path connect.TransferPath, transferFra
15831583

15841584
// FIXME deep packet inspection to look at the contract frames and verify contracts before forwarding
15851585

1586-
if self.exchange.settings.ForwardEnforceActiveContracts {
1587-
if !isAck(transferFrameBytes) {
1588-
hasActiveContract := self.residentContractManager.HasActiveContract(sourceId, destinationId)
1589-
if !hasActiveContract {
1590-
glog.Infof("[rf]abuse no active contract %s->%s\n", sourceId, destinationId)
1591-
// there is no active contract
1592-
// drop
1593-
self.abuseLimiter.delay()
1594-
return
1595-
}
1596-
}
1597-
}
1598-
15991586
c := func() bool {
16001587

16011588
nextForward := func() *ResidentForward {
1589+
if self.exchange.settings.ForwardEnforceActiveContracts {
1590+
// if !isAck(transferFrameBytes) {
1591+
hasActiveContract := self.residentContractManager.HasActiveContract(sourceId, destinationId)
1592+
if !hasActiveContract {
1593+
glog.Infof("[rf]abuse no active contract %s->%s\n", sourceId, destinationId)
1594+
// there is no active contract
1595+
// drop
1596+
self.abuseLimiter.delay()
1597+
return nil
1598+
}
1599+
// }
1600+
}
1601+
16021602
forward := NewResidentForward(self.ctx, self.exchange, destinationId)
16031603
go server.HandleError(func() {
1604-
forward.Run()
1605-
1606-
glog.V(1).Infof("[rf]close %s->%s\n", sourceId, destinationId)
1607-
1608-
// note we don't call close here because only the sender should call close
1609-
forward.Cancel()
1610-
func() {
1604+
defer func() {
16111605
self.stateLock.Lock()
16121606
defer self.stateLock.Unlock()
1607+
forward.Cancel()
16131608
if currentForward := self.forwards[destinationId]; forward == currentForward {
16141609
delete(self.forwards, destinationId)
16151610
}
16161611
}()
1612+
forward.Run()
1613+
1614+
glog.V(1).Infof("[rf]close %s->%s\n", sourceId, destinationId)
1615+
// note we don't call close here because only the sender should call close
16171616
}, forward.Cancel)
16181617
go server.HandleError(func() {
16191618
defer forward.Cancel()
@@ -1670,6 +1669,10 @@ func (self *Resident) handleClientForward(path connect.TransferPath, transferFra
16701669
forward = nextForward()
16711670
}
16721671

1672+
if forward == nil {
1673+
return false
1674+
}
1675+
16731676
select {
16741677
case <-self.ctx.Done():
16751678
return false

connect/resident_contract_manager.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ func (self *residentContractManager) HasActiveContract(sourceId server.Id, desti
7171

7272
next := func() (nextEntry *activeContractEntry) {
7373
c := func() bool {
74-
contractIds := model.GetOpenContractIdsWithNoPartialClose(self.ctx, sourceId, destinationId)
75-
return 0 < len(contractIds)
74+
contractIds1 := model.GetOpenContractIdsWithNoPartialClose(self.ctx, sourceId, destinationId)
75+
if 0 < len(contractIds1) {
76+
return true
77+
}
78+
79+
contractIds2 := model.GetOpenContractIdsWithNoPartialClose(self.ctx, destinationId, sourceId)
80+
if 0 < len(contractIds2) {
81+
return true
82+
}
83+
84+
return false
7685
}
7786
hasActiveContract := c()
7887

0 commit comments

Comments
 (0)