-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
69 lines (59 loc) · 1.37 KB
/
worker.go
File metadata and controls
69 lines (59 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tlsprotocol
import (
"net"
"sync"
)
// worker is a standalone socket that
// listens for connections and then sends
// those connections back to the parent
// listener for handling
type worker struct {
parent *Listener
running bool
socket net.Listener
lock sync.Mutex
}
// start sets the internal state of
// the worker to running and spawns
// a go routine for receiving connections
// from the configured socket
func (worker *worker) start() {
if worker.isRunning() {
return
}
worker.lock.Lock()
defer worker.lock.Unlock()
worker.running = true
go worker.listen()
}
// isRunning will return the value of
// `running` of the worker but in a race
// safe way
func (worker *worker) isRunning() bool {
worker.lock.Lock()
defer worker.lock.Unlock()
return worker.running
}
// listen will receive connections from
// the configured socket for the worker
// until the internal state of the worker
// is changed to no running
func (worker *worker) listen() {
for worker.isRunning() {
conn, err := worker.socket.Accept()
if err != nil {
worker.parent.errors <- err
continue
}
go worker.parent.connectionReceived(conn)
}
}
// stop sets the internal state of
// the worker to not running and closes
// the configured socket
func (worker *worker) stop() {
worker.lock.Lock()
defer worker.lock.Unlock()
worker.running = false
worker.socket.Close()
}