-
Notifications
You must be signed in to change notification settings - Fork 3
/
many_to_one.go
206 lines (195 loc) · 6.52 KB
/
many_to_one.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package imux
import (
log "github.com/Sirupsen/logrus"
"github.com/hkparker/TLB"
"net"
"reflect"
"sync"
)
// Chan of failures to write to destinations
var FailedSocketOuts = make(map[string]chan bool)
var fsoMux sync.Mutex
// WriteQueues for each outgoing socket on the server
var server_write_queues = make(map[string]*WriteQueue)
var swqMux sync.Mutex
// DataIMUX objects to read responses from each outgoing destination socket
var responders = make(map[string]DataIMUX)
var respondersMux sync.Mutex
// Tracks if goroutines have been created for each socket to read from the
// DataIMUXer for its session and write responses down
var loopers = make(map[net.Conn]bool)
var loopersMux sync.Mutex
// Create a new TLB server to accept chunks from anywhere and order them, writing them to corresponding sockets
func ManyToOne(listener net.Listener, dial_destination Redialer) {
tlb_server := tlb.NewServer(listener, tag_socket, type_store())
tlb_server.Accept("all", reflect.TypeOf(Chunk{}), func(iface interface{}, context tlb.TLBContext) {
if chunk, ok := iface.(*Chunk); ok {
log.WithFields(log.Fields{
"at": "ManyToOne",
"sequence_id": chunk.SequenceID,
"socket_id": chunk.SocketID,
"session_id": chunk.SessionID,
}).Debug("received chunk")
createFailReporterIfNeeded(chunk.SocketID, chunk.SessionID)
createResponderIMUXIfNeeded(chunk.SessionID)
writeResponseChunksIfNeeded(context.Socket, chunk.SessionID)
queue, err := queueForDestinationDialIfNeeded(chunk.SocketID, chunk.SessionID, dial_destination)
if err == nil {
queue.Chunks <- chunk
log.WithFields(log.Fields{
"at": "ManyToOne",
"sequence_id": chunk.SequenceID,
"socket_id": chunk.SocketID,
"session_id": chunk.SessionID,
}).Debug("wrote chunk")
} else {
log.WithFields(log.Fields{
"at": "ManyToOne",
"error": err.Error(),
"sequence_id": chunk.SequenceID,
"socket_id": chunk.SocketID,
"session_id": chunk.SessionID,
}).Error("dropped chunk")
if reporter, ok := FailedSocketOuts[chunk.SocketID]; ok {
reporter <- true
} else {
log.WithFields(log.Fields{
"at": "ManyToOne",
"socket_id": chunk.SocketID,
}).Error("unable to lookup fail socket out channel")
}
}
}
})
log.WithFields(log.Fields{
"at": "ManyToOne",
}).Debug("created new ManyToOne")
err := <-tlb_server.FailedServer
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("TLB server failed for ManyToOne")
}
// Create a chan if needed to pass events about this socket failing
func createFailReporterIfNeeded(socket_id, session_id string) {
fsoMux.Lock()
defer fsoMux.Unlock()
if _, present := FailedSocketOuts[socket_id]; !present {
FailedSocketOuts[socket_id] = make(chan bool, 0)
go func(socket_id, session_id string) {
for {
<-FailedSocketOuts[socket_id]
responders[session_id].Chunks <- Chunk{
SessionID: session_id,
SocketID: socket_id,
SequenceID: 0,
Close: true,
}
}
}(socket_id, session_id)
}
}
// If it does not exist, create a DataIMUX to read data from
// outgoing destination sockets with a common session
func createResponderIMUXIfNeeded(session_id string) {
respondersMux.Lock()
defer respondersMux.Unlock()
if _, present := responders[session_id]; !present {
responders[session_id] = NewDataIMUX(session_id)
log.WithFields(log.Fields{
"at": "createResponderIMUXIfNeeded",
"session_id": session_id,
}).Debug("created new responder imux for session")
}
}
// If it is not already happening, ensure that response chunks for a specified
// session_id are written back down this socket.
func writeResponseChunksIfNeeded(socket net.Conn, session_id string) {
loopersMux.Lock()
defer loopersMux.Unlock()
if _, looping := loopers[socket]; !looping {
log.WithFields(log.Fields{
"at": "writeResponseChunksIfNeeded",
"session_id": session_id,
}).Debug("creating write back routine for socket")
go func() {
writer, err := tlb.NewStreamWriter(socket, type_store(), reflect.TypeOf(Chunk{}))
if err != nil {
log.WithFields(log.Fields{
"at": "writeResponseChunksIfNeeded",
"session_id": session_id,
"error": err.Error(),
}).Error("error create return stream writer")
return
}
respondersMux.Lock()
chunk_stream, ok := responders[session_id]
respondersMux.Unlock()
if !ok {
log.WithFields(log.Fields{
"at": "writeResponseChunksIfNeeded",
"session_id": session_id,
}).Error("error looking up responder imux for session_id")
return
}
for {
new_chunk := <-chunk_stream.Chunks
err := writer.Write(new_chunk)
if err != nil {
responders[session_id].Stale <- new_chunk
log.WithFields(log.Fields{
"at": "writeResponseChunksIfNeeded",
"session_id": session_id,
"data_len": len(new_chunk.Data),
"error": err.Error(),
}).Error("error writing a chunk down transport socket")
break
} else {
log.WithFields(log.Fields{
"at": "writeResponseChunksIfNeeded",
"session_id": session_id,
"data_len": len(new_chunk.Data),
}).Debug("wrote a chunk down transport socket")
}
}
}()
loopers[socket] = true
}
}
// Get the queue a new chunk should go to, dialing the outgoing destination socket if this is the first time
// a socket ID has been observed.
func queueForDestinationDialIfNeeded(socket_id, session_id string, dial_destination func() (net.Conn, error)) (*WriteQueue, error) {
swqMux.Lock()
defer swqMux.Unlock()
queue, present := server_write_queues[socket_id]
if !present {
log.WithFields(log.Fields{
"at": "queueForDestinationDialIfNeeded",
"session_id": session_id,
"socket_id": socket_id,
}).Debug("dialing destination")
destination, err := dial_destination()
if err != nil {
log.WithFields(log.Fields{
"at": "queueForDestinationDialIfNeeded",
"session_id": session_id,
"socket_id": socket_id,
"error": err.Error(),
}).Error("error dialing destination")
return queue, err
}
queue = NewWriteQueue(destination)
server_write_queues[socket_id] = queue
respondersMux.Lock()
if imuxer, ok := responders[session_id]; ok {
go imuxer.ReadFrom(socket_id, destination)
} else {
log.WithFields(log.Fields{
"at": "queueForDestinationDialIfNeeded",
"session_id": session_id,
"socket_id": socket_id,
}).Fatal("no responding reader exists, should not be possible")
}
respondersMux.Unlock()
}
return queue, nil
}