-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
151 lines (138 loc) · 3.01 KB
/
connection.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"net"
"sync"
protos "github.com/Pixelgaffer/dico-proto"
"github.com/golang/protobuf/proto"
log "github.com/Sirupsen/logrus"
)
var connections struct {
sync.Mutex
all []*Connection
}
// Connection holds client-specific data
type Connection struct {
worker *Worker
conn *net.Conn
handshake protos.Handshake
send chan proto.Message
recv chan proto.Message
doneCh chan interface{}
}
func (c *Connection) init(handshake protos.Handshake) {
stats.Pulse()
log.WithFields(log.Fields{
"runs_tasks": handshake.GetRunsTasks(),
"manages_tasks": handshake.GetManagesTasks(),
"recieves_stats": handshake.GetRecievesStats(),
}).Info("new connection")
c.send = make(chan proto.Message, 10) // TODO
c.recv = make(chan proto.Message)
c.doneCh = make(chan interface{})
c.handshake = handshake
if handshake.GetRunsTasks() {
c.worker = &Worker{
connection: c,
taskStatusChan: make(chan *protos.TaskStatus),
taskResultChan: make(chan *protos.TaskResult),
}
go c.worker.consume()
}
}
func (c *Connection) handle() {
var msg proto.Message
for {
select {
case <-c.doneCh:
return
case msg = <-c.recv:
}
log.WithField("msg", msg).Debug("new msg")
switch v := msg.(type) {
case *protos.SubmitTask:
if v.GetMulti() {
generateTasks(v.GetOptions())
} else {
t := &Task{}
t.id = getNextTaskID()
t.options = v.GetOptions()
taskChan <- t
}
case *protos.TaskStatus:
c.worker.taskStatusChan <- v
case *protos.TaskResult:
c.worker.taskResultChan <- v
case *protos.SubmitCode:
addJobType(v)
default:
log.WithFields(log.Fields{
"type": proto.MessageName(msg),
"message": v,
}).Error("connection.handle invalid type")
}
}
}
func (c *Connection) alive() bool {
select {
case <-c.doneCh:
return false
default:
return true
}
}
func (c *Connection) kill() {
if c.alive() {
log.WithField("conn", c.name()).Info("connection died.")
close(c.doneCh)
stats.Pulse()
} else {
log.Info(".kill() on dead connection")
}
}
func (c *Connection) name() string {
conn := *c.conn
if c.handshake.GetName() == "" {
return fmt.Sprintf("[%v]", conn.RemoteAddr())
}
return fmt.Sprintf("%v [%v]", c.handshake.GetName(), conn.RemoteAddr())
}
func workers() chan *Worker {
connections.Lock()
c := make(chan *Worker, len(connections.all))
go func() {
for _, conn := range connections.all {
if conn.alive() && conn.handshake.GetRunsTasks() {
c <- conn.worker
}
}
close(c)
connections.Unlock()
}()
return c
}
func managers() chan *Connection {
connections.Lock()
c := make(chan *Connection, len(connections.all))
go func() {
for _, conn := range connections.all {
if conn.alive() && conn.handshake.GetRecievesStats() {
c <- conn
}
}
close(c)
connections.Unlock()
}()
return c
}
func gcConnections() {
connections.Lock()
var nC []*Connection
for _, conn := range connections.all {
if conn.alive() {
nC = append(nC, conn)
}
}
connections.all = nC
connections.Unlock()
}