Skip to content

Commit 70eb89e

Browse files
authored
Initialize permission timer before publication (#619)
1 parent 91fc7f1 commit 70eb89e

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎internal/allocation/allocation.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,17 @@ func (a *Allocation) GetPermission(addr net.Addr) *Permission {
9292
func (a *Allocation) AddPermission(perms *Permission) {
9393
fingerprint := ipnet.FingerprintAddr(perms.Addr)
9494

95-
a.permissionsLock.RLock()
95+
a.permissionsLock.Lock()
9696
existedPermission, ok := a.permissions[fingerprint]
97-
a.permissionsLock.RUnlock()
98-
9997
if ok {
10098
existedPermission.refresh(perms.timeout)
99+
a.permissionsLock.Unlock()
101100

102101
return
103102
}
104103

105104
perms.allocation = a
106-
a.permissionsLock.Lock()
105+
perms.start(perms.timeout)
107106
a.permissions[fingerprint] = perms
108107
a.permissionsLock.Unlock()
109108

@@ -114,8 +113,6 @@ func (a *Allocation) AddPermission(perms *Permission) {
114113
a.RelayAddr, u.IP)
115114
}
116115
}
117-
118-
perms.start(perms.timeout)
119116
}
120117

121118
// RemovePermission removes the net.Addr's fingerprint from the allocation's permissions.

‎internal/allocation/allocation_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ func TestAddPermission(t *testing.T) {
9595
assert.Equal(t, p, foundPermission)
9696
}
9797

98+
func TestAddPermissionPublishesInitializedTimer(t *testing.T) {
99+
callbackStarted := make(chan struct{})
100+
releaseCallback := make(chan struct{})
101+
addDone := make(chan struct{})
102+
alloc := NewAllocation(nil, &FiveTuple{}, EventHandler{
103+
OnPermissionCreated: func(_, _ net.Addr, _, _, _ string, _ net.Addr, _ net.IP) {
104+
close(callbackStarted)
105+
<-releaseCallback
106+
},
107+
}, nil)
108+
alloc.lifetimeTimer = time.NewTimer(time.Hour)
109+
110+
go func() {
111+
defer close(addDone)
112+
alloc.AddPermission(NewPermission(
113+
&net.UDPAddr{IP: net.IPv4(192, 0, 2, 1), Port: 3478},
114+
nil,
115+
time.Hour,
116+
))
117+
}()
118+
119+
<-callbackStarted
120+
assert.NoError(t, alloc.Close())
121+
close(releaseCallback)
122+
<-addDone
123+
}
124+
98125
func TestRemovePermission(t *testing.T) {
99126
alloc := NewAllocation(nil, nil, EventHandler{}, nil)
100127

0 commit comments

Comments
 (0)
⌂ 换网址