source: code/trunk/user.go@ 407

Last change on this file since 407 was 406, checked in by contact, 5 years ago

Replace networkHistory.offlineClients with clients

Keep the ring buffer alive even if all clients are connected. Keep the
ID of the latest delivered message even for online clients.

As-is, this is a net downgrade: memory usage increases because ring
buffers aren't free'd anymore. However upcoming commits will replace the
ring buffer with log files. This change makes reading from log files
easier.

File size: 13.4 KB
RevLine 
[101]1package soju
2
3import (
[385]4 "crypto/sha256"
5 "encoding/binary"
[395]6 "encoding/hex"
[218]7 "fmt"
[101]8 "time"
[103]9
10 "gopkg.in/irc.v3"
[101]11)
12
[165]13type event interface{}
14
15type eventUpstreamMessage struct {
[103]16 msg *irc.Message
17 uc *upstreamConn
18}
19
[218]20type eventUpstreamConnectionError struct {
21 net *network
22 err error
23}
24
[196]25type eventUpstreamConnected struct {
26 uc *upstreamConn
27}
28
[179]29type eventUpstreamDisconnected struct {
30 uc *upstreamConn
31}
32
[218]33type eventUpstreamError struct {
34 uc *upstreamConn
35 err error
36}
37
[165]38type eventDownstreamMessage struct {
[103]39 msg *irc.Message
40 dc *downstreamConn
41}
42
[166]43type eventDownstreamConnected struct {
44 dc *downstreamConn
45}
46
[167]47type eventDownstreamDisconnected struct {
48 dc *downstreamConn
49}
50
[376]51type eventStop struct{}
52
[253]53type networkHistory struct {
[406]54 clients map[string]uint64 // indexed by client name
55 ring *Ring // can be nil if there are no offline clients
[253]56}
57
[101]58type network struct {
59 Network
[202]60 user *user
61 stopped chan struct{}
[131]62
[253]63 conn *upstreamConn
[267]64 channels map[string]*Channel
[253]65 history map[string]*networkHistory // indexed by entity
66 offlineClients map[string]struct{} // indexed by client name
67 lastError error
[101]68}
69
[267]70func newNetwork(user *user, record *Network, channels []Channel) *network {
71 m := make(map[string]*Channel, len(channels))
72 for _, ch := range channels {
[283]73 ch := ch
[267]74 m[ch.Name] = &ch
75 }
76
[101]77 return &network{
[253]78 Network: *record,
79 user: user,
80 stopped: make(chan struct{}),
[267]81 channels: m,
[253]82 history: make(map[string]*networkHistory),
83 offlineClients: make(map[string]struct{}),
[101]84 }
85}
86
[218]87func (net *network) forEachDownstream(f func(*downstreamConn)) {
88 net.user.forEachDownstream(func(dc *downstreamConn) {
89 if dc.network != nil && dc.network != net {
90 return
91 }
92 f(dc)
93 })
94}
95
[311]96func (net *network) isStopped() bool {
97 select {
98 case <-net.stopped:
99 return true
100 default:
101 return false
102 }
103}
104
[385]105func userIdent(u *User) string {
106 // The ident is a string we will send to upstream servers in clear-text.
107 // For privacy reasons, make sure it doesn't expose any meaningful user
108 // metadata. We just use the base64-encoded hashed ID, so that people don't
109 // start relying on the string being an integer or following a pattern.
110 var b [64]byte
111 binary.LittleEndian.PutUint64(b[:], uint64(u.ID))
112 h := sha256.Sum256(b[:])
[395]113 return hex.EncodeToString(h[:16])
[385]114}
115
[101]116func (net *network) run() {
117 var lastTry time.Time
118 for {
[311]119 if net.isStopped() {
[202]120 return
121 }
122
[398]123 if dur := time.Now().Sub(lastTry); dur < retryConnectDelay {
124 delay := retryConnectDelay - dur
[101]125 net.user.srv.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
126 time.Sleep(delay)
127 }
128 lastTry = time.Now()
129
130 uc, err := connectToUpstream(net)
131 if err != nil {
132 net.user.srv.Logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err)
[218]133 net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
[101]134 continue
135 }
136
[385]137 if net.user.srv.Identd != nil {
138 net.user.srv.Identd.Store(uc.RemoteAddr().String(), uc.LocalAddr().String(), userIdent(&net.user.User))
139 }
140
[101]141 uc.register()
[197]142 if err := uc.runUntilRegistered(); err != nil {
[399]143 text := err.Error()
144 if regErr, ok := err.(registrationError); ok {
145 text = string(regErr)
146 }
147 uc.logger.Printf("failed to register: %v", text)
148 net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", text)}
[197]149 uc.Close()
150 continue
151 }
[101]152
[311]153 // TODO: this is racy with net.stopped. If the network is stopped
154 // before the user goroutine receives eventUpstreamConnected, the
155 // connection won't be closed.
[196]156 net.user.events <- eventUpstreamConnected{uc}
[165]157 if err := uc.readMessages(net.user.events); err != nil {
[101]158 uc.logger.Printf("failed to handle messages: %v", err)
[218]159 net.user.events <- eventUpstreamError{uc, fmt.Errorf("failed to handle messages: %v", err)}
[101]160 }
161 uc.Close()
[179]162 net.user.events <- eventUpstreamDisconnected{uc}
[385]163
164 if net.user.srv.Identd != nil {
165 net.user.srv.Identd.Delete(uc.RemoteAddr().String(), uc.LocalAddr().String())
166 }
[101]167 }
168}
169
[309]170func (net *network) stop() {
[311]171 if !net.isStopped() {
[202]172 close(net.stopped)
173 }
174
[279]175 if net.conn != nil {
176 net.conn.Close()
[202]177 }
178}
179
[222]180func (net *network) createUpdateChannel(ch *Channel) error {
[267]181 if current, ok := net.channels[ch.Name]; ok {
182 ch.ID = current.ID // update channel if it already exists
183 }
184 if err := net.user.srv.db.StoreChannel(net.ID, ch); err != nil {
[222]185 return err
186 }
[284]187 prev := net.channels[ch.Name]
[267]188 net.channels[ch.Name] = ch
[284]189
190 if prev != nil && prev.Detached != ch.Detached {
191 history := net.history[ch.Name]
192 if ch.Detached {
193 net.user.srv.Logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
194 net.forEachDownstream(func(dc *downstreamConn) {
195 net.offlineClients[dc.clientName] = struct{}{}
196
197 dc.SendMessage(&irc.Message{
198 Prefix: dc.prefix(),
199 Command: "PART",
200 Params: []string{dc.marshalEntity(net, ch.Name), "Detach"},
201 })
202 })
203 } else {
204 net.user.srv.Logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)
205
206 var uch *upstreamChannel
207 if net.conn != nil {
208 uch = net.conn.channels[ch.Name]
209 }
210
211 net.forEachDownstream(func(dc *downstreamConn) {
212 dc.SendMessage(&irc.Message{
213 Prefix: dc.prefix(),
214 Command: "JOIN",
215 Params: []string{dc.marshalEntity(net, ch.Name)},
216 })
217
218 if uch != nil {
219 forwardChannel(dc, uch)
220 }
221
222 if history != nil {
223 dc.sendNetworkHistory(net)
224 }
225 })
226 }
227 }
228
[267]229 return nil
[222]230}
231
232func (net *network) deleteChannel(name string) error {
[267]233 if err := net.user.srv.db.DeleteChannel(net.ID, name); err != nil {
234 return err
235 }
236 delete(net.channels, name)
237 return nil
[222]238}
239
[101]240type user struct {
241 User
242 srv *Server
243
[165]244 events chan event
[377]245 done chan struct{}
[103]246
[101]247 networks []*network
248 downstreamConns []*downstreamConn
[177]249
250 // LIST commands in progress
[179]251 pendingLISTs []pendingLIST
[101]252}
253
[177]254type pendingLIST struct {
255 downstreamID uint64
256 // list of per-upstream LIST commands not yet sent or completed
257 pendingCommands map[int64]*irc.Message
258}
259
[101]260func newUser(srv *Server, record *User) *user {
261 return &user{
[165]262 User: *record,
263 srv: srv,
264 events: make(chan event, 64),
[377]265 done: make(chan struct{}),
[101]266 }
267}
268
269func (u *user) forEachNetwork(f func(*network)) {
270 for _, network := range u.networks {
271 f(network)
272 }
273}
274
275func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
276 for _, network := range u.networks {
[279]277 if network.conn == nil {
[101]278 continue
279 }
[279]280 f(network.conn)
[101]281 }
282}
283
284func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
285 for _, dc := range u.downstreamConns {
286 f(dc)
287 }
288}
289
290func (u *user) getNetwork(name string) *network {
291 for _, network := range u.networks {
292 if network.Addr == name {
293 return network
294 }
[201]295 if network.Name != "" && network.Name == name {
296 return network
297 }
[101]298 }
299 return nil
300}
301
[313]302func (u *user) getNetworkByID(id int64) *network {
303 for _, net := range u.networks {
304 if net.ID == id {
305 return net
306 }
307 }
308 return nil
309}
310
[101]311func (u *user) run() {
[377]312 defer close(u.done)
313
[101]314 networks, err := u.srv.db.ListNetworks(u.Username)
315 if err != nil {
316 u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
317 return
318 }
319
320 for _, record := range networks {
[283]321 record := record
[267]322 channels, err := u.srv.db.ListChannels(record.ID)
323 if err != nil {
324 u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
[359]325 continue
[267]326 }
327
328 network := newNetwork(u, &record, channels)
[101]329 u.networks = append(u.networks, network)
330
331 go network.run()
332 }
[103]333
[165]334 for e := range u.events {
335 switch e := e.(type) {
[196]336 case eventUpstreamConnected:
[198]337 uc := e.uc
[199]338
339 uc.network.conn = uc
340
[198]341 uc.updateAway()
[218]342
343 uc.forEachDownstream(func(dc *downstreamConn) {
[276]344 dc.updateSupportedCaps()
[223]345 sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
[296]346
347 dc.updateNick()
[218]348 })
349 uc.network.lastError = nil
[179]350 case eventUpstreamDisconnected:
[313]351 u.handleUpstreamDisconnected(e.uc)
352 case eventUpstreamConnectionError:
353 net := e.net
[199]354
[313]355 stopped := false
356 select {
357 case <-net.stopped:
358 stopped = true
359 default:
[179]360 }
[199]361
[313]362 if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
[218]363 net.forEachDownstream(func(dc *downstreamConn) {
[223]364 sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
[218]365 })
366 }
367 net.lastError = e.err
368 case eventUpstreamError:
369 uc := e.uc
370
371 uc.forEachDownstream(func(dc *downstreamConn) {
[223]372 sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
[218]373 })
374 uc.network.lastError = e.err
[165]375 case eventUpstreamMessage:
376 msg, uc := e.msg, e.uc
[175]377 if uc.isClosed() {
[133]378 uc.logger.Printf("ignoring message on closed connection: %v", msg)
379 break
380 }
[103]381 if err := uc.handleMessage(msg); err != nil {
382 uc.logger.Printf("failed to handle message %q: %v", msg, err)
383 }
[166]384 case eventDownstreamConnected:
385 dc := e.dc
[168]386
387 if err := dc.welcome(); err != nil {
388 dc.logger.Printf("failed to handle new registered connection: %v", err)
389 break
390 }
391
[166]392 u.downstreamConns = append(u.downstreamConns, dc)
[198]393
394 u.forEachUpstream(func(uc *upstreamConn) {
395 uc.updateAway()
396 })
[276]397
398 dc.updateSupportedCaps()
[167]399 case eventDownstreamDisconnected:
400 dc := e.dc
[204]401
[167]402 for i := range u.downstreamConns {
403 if u.downstreamConns[i] == dc {
404 u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
405 break
406 }
407 }
[198]408
[253]409 // Save history if we're the last client with this name
410 skipHistory := make(map[*network]bool)
411 u.forEachDownstream(func(conn *downstreamConn) {
412 if dc.clientName == conn.clientName {
413 skipHistory[conn.network] = true
414 }
415 })
416
417 dc.forEachNetwork(func(net *network) {
418 if skipHistory[net] || skipHistory[nil] {
419 return
420 }
421
422 net.offlineClients[dc.clientName] = struct{}{}
423 })
424
[198]425 u.forEachUpstream(func(uc *upstreamConn) {
426 uc.updateAway()
427 })
[165]428 case eventDownstreamMessage:
429 msg, dc := e.msg, e.dc
[133]430 if dc.isClosed() {
431 dc.logger.Printf("ignoring message on closed connection: %v", msg)
432 break
433 }
[103]434 err := dc.handleMessage(msg)
435 if ircErr, ok := err.(ircError); ok {
436 ircErr.Message.Prefix = dc.srv.prefix()
437 dc.SendMessage(ircErr.Message)
438 } else if err != nil {
439 dc.logger.Printf("failed to handle message %q: %v", msg, err)
440 dc.Close()
441 }
[376]442 case eventStop:
443 u.forEachDownstream(func(dc *downstreamConn) {
444 dc.Close()
445 })
446 for _, n := range u.networks {
447 n.stop()
448 }
449 return
[165]450 default:
451 u.srv.Logger.Printf("received unknown event type: %T", e)
[103]452 }
453 }
[101]454}
455
[313]456func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
457 uc.network.conn = nil
458
459 for _, ml := range uc.messageLoggers {
460 if err := ml.Close(); err != nil {
461 uc.logger.Printf("failed to close message logger: %v", err)
462 }
463 }
464
465 uc.endPendingLISTs(true)
466
467 uc.forEachDownstream(func(dc *downstreamConn) {
468 dc.updateSupportedCaps()
469 })
470
471 if uc.network.lastError == nil {
472 uc.forEachDownstream(func(dc *downstreamConn) {
473 sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
474 })
475 }
476}
477
478func (u *user) addNetwork(network *network) {
479 u.networks = append(u.networks, network)
480 go network.run()
481}
482
483func (u *user) removeNetwork(network *network) {
484 network.stop()
485
486 u.forEachDownstream(func(dc *downstreamConn) {
487 if dc.network != nil && dc.network == network {
488 dc.Close()
489 }
490 })
491
492 for i, net := range u.networks {
493 if net == network {
494 u.networks = append(u.networks[:i], u.networks[i+1:]...)
495 return
496 }
497 }
498
499 panic("tried to remove a non-existing network")
500}
501
502func (u *user) createNetwork(record *Network) (*network, error) {
503 if record.ID != 0 {
[144]504 panic("tried creating an already-existing network")
505 }
506
[313]507 network := newNetwork(u, record, nil)
[101]508 err := u.srv.db.StoreNetwork(u.Username, &network.Network)
509 if err != nil {
510 return nil, err
511 }
[144]512
[313]513 u.addNetwork(network)
[144]514
[101]515 return network, nil
516}
[202]517
[313]518func (u *user) updateNetwork(record *Network) (*network, error) {
519 if record.ID == 0 {
520 panic("tried updating a new network")
521 }
[202]522
[313]523 network := u.getNetworkByID(record.ID)
524 if network == nil {
525 panic("tried updating a non-existing network")
526 }
527
528 if err := u.srv.db.StoreNetwork(u.Username, record); err != nil {
529 return nil, err
530 }
531
532 // Most network changes require us to re-connect to the upstream server
533
534 channels := make([]Channel, 0, len(network.channels))
535 for _, ch := range network.channels {
536 channels = append(channels, *ch)
537 }
538
539 updatedNetwork := newNetwork(u, record, channels)
540
541 // If we're currently connected, disconnect and perform the necessary
542 // bookkeeping
543 if network.conn != nil {
544 network.stop()
545 // Note: this will set network.conn to nil
546 u.handleUpstreamDisconnected(network.conn)
547 }
548
549 // Patch downstream connections to use our fresh updated network
550 u.forEachDownstream(func(dc *downstreamConn) {
551 if dc.network != nil && dc.network == network {
552 dc.network = updatedNetwork
[202]553 }
[313]554 })
[202]555
[313]556 // We need to remove the network after patching downstream connections,
557 // otherwise they'll get closed
558 u.removeNetwork(network)
[202]559
[313]560 // This will re-connect to the upstream server
561 u.addNetwork(updatedNetwork)
562
563 return updatedNetwork, nil
564}
565
566func (u *user) deleteNetwork(id int64) error {
567 network := u.getNetworkByID(id)
568 if network == nil {
569 panic("tried deleting a non-existing network")
[202]570 }
571
[313]572 if err := u.srv.db.DeleteNetwork(network.ID); err != nil {
573 return err
574 }
575
576 u.removeNetwork(network)
577 return nil
[202]578}
[252]579
580func (u *user) updatePassword(hashed string) error {
581 u.User.Password = hashed
[324]582 return u.srv.db.StoreUser(&u.User)
[252]583}
[376]584
585func (u *user) stop() {
586 u.events <- eventStop{}
[377]587 <-u.done
[376]588}
Note: See TracBrowser for help on using the repository browser.