Skip to content

Commit e3ad12c

Browse files
anastasiastv0g
authored andcommitted
Rewrite NewServer to reduce cyclomatic complexity
Co-author: Steffen Vogel <post@steffenvogel.de>
1 parent f25e17d commit e3ad12c

1 file changed

Lines changed: 72 additions & 76 deletions

File tree

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-
inboundMTU int
31+
inboundMTU int
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-
for i := range s.packetConnConfigs {
70-
go func(i int, p PacketConnConfig) {
71-
permissionHandler := p.PermissionHandler
72-
if permissionHandler == 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-
if err != nil {
83-
s.log.Errorf("exit read loop on error: %s", err.Error())
84-
return
85-
}
86-
s.allocationManagers[i] = allocationManager
87-
defer func() {
88-
if err := 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-
for i, listener := range s.listenerConfigs {
98-
go func(i int, l ListenerConfig) {
99-
permissionHandler := l.PermissionHandler
100-
if permissionHandler == 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-
if err != nil {
111-
s.log.Errorf("exit read loop on error: %s", err.Error())
112-
return
113-
}
114-
s.allocationManagers[i] = allocationManager
115-
defer func() {
116-
if err := 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-
if err != nil {
124-
s.log.Debugf("exit accept loop on error: %s", err.Error())
125-
return
126-
}
127-
128-
go s.readLoop(NewSTUNConn(conn), allocationManager)
129-
}
130-
}(i+len(s.packetConnConfigs), listener)
67+
for _, cfg := range s.packetConnConfigs {
68+
am, err := s.createAllocationManager(cfg.RelayAddressGenerator, cfg.PermissionHandler)
69+
if err != nil {
70+
return nil, fmt.Errorf("failed to create AllocationManager: %w", err)
71+
}
72+
73+
go s.readPacketConn(cfg, am)
74+
}
75+
76+
for _, cfg := range s.listenerConfigs {
77+
am, err := s.createAllocationManager(cfg.RelayAddressGenerator, cfg.PermissionHandler)
78+
if err != nil {
79+
return nil, fmt.Errorf("failed to create AllocationManager: %w", err)
80+
}
81+
82+
go s.readListener(cfg, am)
13183
}
13284

13385
return s, 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 := range s.allocationManagers {
140-
if manager != nil {
141-
allocations += manager.AllocationCount()
142-
}
90+
allocs := 0
91+
for _, am := range s.allocationManagers {
92+
allocs += am.AllocationCount()
14393
}
144-
return allocations
94+
return allocs
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
var errors []error
150100

151-
for _, p := range s.packetConnConfigs {
152-
if err := p.PacketConn.Close(); err != nil {
101+
for _, cfg := range s.packetConnConfigs {
102+
if err := cfg.PacketConn.Close(); err != nil {
153103
errors = append(errors, err)
154104
}
155105
}
156106

157-
for _, l := range s.listenerConfigs {
158-
if err := l.Listener.Close(); err != nil {
107+
for _, cfg := range s.listenerConfigs {
108+
if err := 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 := range errors {
169-
err = fmt.Errorf("%s; close error (%w) ", err.Error(), e)
119+
err = fmt.Errorf("%s; close error (%w) ", err, e)
170120
}
171121

172122
return err
173123
}
174124

125+
func (s *Server) readPacketConn(p PacketConnConfig, am *allocation.Manager) {
126+
s.readLoop(p.PacketConn, am)
127+
128+
if err := am.Close(); err != nil {
129+
s.log.Errorf("Failed to close AllocationManager: %s", err)
130+
}
131+
}
132+
133+
func (s *Server) readListener(l ListenerConfig, am *allocation.Manager) {
134+
defer func() {
135+
if err := 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+
if err != nil {
143+
s.log.Debugf("Failed to accept: %s", err)
144+
return
145+
}
146+
147+
go s.readLoop(NewSTUNConn(conn), am)
148+
}
149+
}
150+
151+
func (s *Server) createAllocationManager(addrGenerator RelayAddressGenerator, handler PermissionHandler) (*allocation.Manager, error) {
152+
if handler == 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+
if err != nil {
163+
return am, err
164+
}
165+
166+
s.allocationManagers = append(s.allocationManagers, am)
167+
168+
return am, 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)