Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/turnPublic

Commite3ad12c

Browse files
anastasiastv0g
anastasia
authored andcommitted
Rewrite NewServer to reduce cyclomatic complexity
Co-author: Steffen Vogel <post@steffenvogel.de>
1 parentf25e17d commite3ad12c

File tree

1 file changed

+72
-76
lines changed

1 file changed

+72
-76
lines changed

‎server.go

Lines changed: 72 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ type Server struct {
2828
packetConnConfigs []PacketConnConfig
2929
listenerConfigs []ListenerConfig
3030
allocationManagers []*allocation.Manager
31-
32-
inboundMTUint
31+
inboundMTUint
3332
}
3433

3534
// NewServer creates the Pion TURN server
@@ -57,7 +56,6 @@ func NewServer(config ServerConfig) (*Server, error) {
5756
channelBindTimeout:config.ChannelBindTimeout,
5857
packetConnConfigs:config.PacketConnConfigs,
5958
listenerConfigs:config.ListenerConfigs,
60-
allocationManagers:make([]*allocation.Manager,len(config.PacketConnConfigs)+len(config.ListenerConfigs)),
6159
nonces:&sync.Map{},
6260
inboundMTU:mtu,
6361
}
@@ -66,96 +64,48 @@ func NewServer(config ServerConfig) (*Server, error) {
6664
s.channelBindTimeout=proto.DefaultLifetime
6765
}
6866

69-
fori:=ranges.packetConnConfigs {
70-
gofunc(iint,pPacketConnConfig) {
71-
permissionHandler:=p.PermissionHandler
72-
ifpermissionHandler==nil {
73-
permissionHandler=DefaultPermissionHandler
74-
}
75-
76-
allocationManager,err:=allocation.NewManager(allocation.ManagerConfig{
77-
AllocatePacketConn:p.RelayAddressGenerator.AllocatePacketConn,
78-
AllocateConn:p.RelayAddressGenerator.AllocateConn,
79-
PermissionHandler:permissionHandler,
80-
LeveledLogger:s.log,
81-
})
82-
iferr!=nil {
83-
s.log.Errorf("exit read loop on error: %s",err.Error())
84-
return
85-
}
86-
s.allocationManagers[i]=allocationManager
87-
deferfunc() {
88-
iferr:=allocationManager.Close();err!=nil {
89-
s.log.Errorf("Failed to close AllocationManager: %s",err.Error())
90-
}
91-
}()
92-
93-
s.readLoop(p.PacketConn,allocationManager)
94-
}(i,s.packetConnConfigs[i])
95-
}
96-
97-
fori,listener:=ranges.listenerConfigs {
98-
gofunc(iint,lListenerConfig) {
99-
permissionHandler:=l.PermissionHandler
100-
ifpermissionHandler==nil {
101-
permissionHandler=DefaultPermissionHandler
102-
}
103-
104-
allocationManager,err:=allocation.NewManager(allocation.ManagerConfig{
105-
AllocatePacketConn:l.RelayAddressGenerator.AllocatePacketConn,
106-
AllocateConn:l.RelayAddressGenerator.AllocateConn,
107-
PermissionHandler:permissionHandler,
108-
LeveledLogger:s.log,
109-
})
110-
iferr!=nil {
111-
s.log.Errorf("exit read loop on error: %s",err.Error())
112-
return
113-
}
114-
s.allocationManagers[i]=allocationManager
115-
deferfunc() {
116-
iferr:=allocationManager.Close();err!=nil {
117-
s.log.Errorf("Failed to close AllocationManager: %s",err.Error())
118-
}
119-
}()
120-
121-
for {
122-
conn,err:=l.Listener.Accept()
123-
iferr!=nil {
124-
s.log.Debugf("exit accept loop on error: %s",err.Error())
125-
return
126-
}
127-
128-
gos.readLoop(NewSTUNConn(conn),allocationManager)
129-
}
130-
}(i+len(s.packetConnConfigs),listener)
67+
for_,cfg:=ranges.packetConnConfigs {
68+
am,err:=s.createAllocationManager(cfg.RelayAddressGenerator,cfg.PermissionHandler)
69+
iferr!=nil {
70+
returnnil,fmt.Errorf("failed to create AllocationManager: %w",err)
71+
}
72+
73+
gos.readPacketConn(cfg,am)
74+
}
75+
76+
for_,cfg:=ranges.listenerConfigs {
77+
am,err:=s.createAllocationManager(cfg.RelayAddressGenerator,cfg.PermissionHandler)
78+
iferr!=nil {
79+
returnnil,fmt.Errorf("failed to create AllocationManager: %w",err)
80+
}
81+
82+
gos.readListener(cfg,am)
13183
}
13284

13385
returns,nil
13486
}
13587

13688
// AllocationCount returns the number of active allocations. It can be used to drain the server before closing
13789
func (s*Server)AllocationCount()int {
138-
allocations:=0
139-
for_,manager:=ranges.allocationManagers {
140-
ifmanager!=nil {
141-
allocations+=manager.AllocationCount()
142-
}
90+
allocs:=0
91+
for_,am:=ranges.allocationManagers {
92+
allocs+=am.AllocationCount()
14393
}
144-
returnallocations
94+
returnallocs
14595
}
14696

14797
// Close stops the TURN Server. It cleans up any associated state and closes all connections it is managing
14898
func (s*Server)Close()error {
14999
varerrors []error
150100

151-
for_,p:=ranges.packetConnConfigs {
152-
iferr:=p.PacketConn.Close();err!=nil {
101+
for_,cfg:=ranges.packetConnConfigs {
102+
iferr:=cfg.PacketConn.Close();err!=nil {
153103
errors=append(errors,err)
154104
}
155105
}
156106

157-
for_,l:=ranges.listenerConfigs {
158-
iferr:=l.Listener.Close();err!=nil {
107+
for_,cfg:=ranges.listenerConfigs {
108+
iferr:=cfg.Listener.Close();err!=nil {
159109
errors=append(errors,err)
160110
}
161111
}
@@ -166,12 +116,58 @@ func (s *Server) Close() error {
166116

167117
err:=errFailedToClose
168118
for_,e:=rangeerrors {
169-
err=fmt.Errorf("%s; close error (%w) ",err.Error(),e)
119+
err=fmt.Errorf("%s; close error (%w) ",err,e)
170120
}
171121

172122
returnerr
173123
}
174124

125+
func (s*Server)readPacketConn(pPacketConnConfig,am*allocation.Manager) {
126+
s.readLoop(p.PacketConn,am)
127+
128+
iferr:=am.Close();err!=nil {
129+
s.log.Errorf("Failed to close AllocationManager: %s",err)
130+
}
131+
}
132+
133+
func (s*Server)readListener(lListenerConfig,am*allocation.Manager) {
134+
deferfunc() {
135+
iferr:=am.Close();err!=nil {
136+
s.log.Errorf("Failed to close AllocationManager: %s",err)
137+
}
138+
}()
139+
140+
for {
141+
conn,err:=l.Listener.Accept()
142+
iferr!=nil {
143+
s.log.Debugf("Failed to accept: %s",err)
144+
return
145+
}
146+
147+
gos.readLoop(NewSTUNConn(conn),am)
148+
}
149+
}
150+
151+
func (s*Server)createAllocationManager(addrGeneratorRelayAddressGenerator,handlerPermissionHandler) (*allocation.Manager,error) {
152+
ifhandler==nil {
153+
handler=DefaultPermissionHandler
154+
}
155+
156+
am,err:=allocation.NewManager(allocation.ManagerConfig{
157+
AllocatePacketConn:addrGenerator.AllocatePacketConn,
158+
AllocateConn:addrGenerator.AllocateConn,
159+
PermissionHandler:handler,
160+
LeveledLogger:s.log,
161+
})
162+
iferr!=nil {
163+
returnam,err
164+
}
165+
166+
s.allocationManagers=append(s.allocationManagers,am)
167+
168+
returnam,err
169+
}
170+
175171
func (s*Server)readLoop(p net.PacketConn,allocationManager*allocation.Manager) {
176172
buf:=make([]byte,s.inboundMTU)
177173
for {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp