Debian Codegraph @

server/listener/listener.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package listener has code to listen for device connections
18 // and setup sessions for them.
19 package listener
20
21 import (
22 "crypto/tls"
23 "net"
24 "time"
25
26 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
27 )
28
29 // A DeviceListenerConfig offers the DeviceListener configuration.
30 type DeviceListenerConfig interface {
31 // Addr to listen on.
32 Addr() string
33 // TLS config
34 TLSServerConfig() *tls.Config
35 }
36
37 // DeviceListener listens and setup sessions from device connections.
38 type DeviceListener struct {
39 net.Listener
40 }
41
42 // DeviceListen creates a DeviceListener for device connections based
43 // on config. If lst is not nil DeviceListen just wraps it with a TLS
44 // layer instead of starting creating a new listener.
45 func DeviceListen(lst net.Listener, cfg DeviceListenerConfig) (*DeviceListener, error) {
46 if lst == nil {
47 var err error
48 lst, err = net.Listen("tcp", cfg.Addr())
49 if err != nil {
50 return nil, err
51 }
52 }
53 tlsCfg := cfg.TLSServerConfig()
54 return &DeviceListener{tls.NewListener(lst, tlsCfg)}, nil
55 }
56
57 // handleTemporary checks and handles if the error is just a temporary network
58 // error.
59 func handleTemporary(err error) bool {
60 if netError, isNetError := err.(net.Error); isNetError {
61 if netError.Temporary() {
62 // wait, xxx exponential backoff?
63 time.Sleep(100 * time.Millisecond)
64 return true
65 }
66 }
67 return false
68 }
69
70 // SessionResourceManager allows to limit resource usage tracking connections.
71 type SessionResourceManager interface {
72 ConsumeConn()
73 }
74
75 // NOP SessionResourceManager.
76 type NopSessionResourceManager struct{}
77
78 func (r *NopSessionResourceManager) ConsumeConn() {}
79
80 // AcceptLoop accepts connections and starts sessions for them.
81 func (dl *DeviceListener) AcceptLoop(session func(net.Conn) error, resource SessionResourceManager, logger logger.Logger) error {
82 for {
83 resource.ConsumeConn()
84 conn, err := dl.Listener.Accept()
85 if err != nil {
86 if handleTemporary(err) {
87 logger.Errorf("device listener: %s -- retrying", err)
88 continue
89 }
90 return err
91 }
92 go func() {
93 defer func() {
94 if err := recover(); err != nil {
95 logger.PanicStackf("terminating device connection on: %v", err)
96 }
97 }()
98 session(conn)
99 }()
100 }
101 }