2019-08-02 01:09:01 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/event"
|
|
|
|
"cwtch.im/cwtch/protocol/connections"
|
2021-03-19 21:20:22 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2019-08-02 01:09:01 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tickTime = 10 * time.Second
|
2021-06-24 01:29:23 +00:00
|
|
|
const maxBackoff int = 32 // 320 seconds or ~5 min
|
2019-08-02 01:09:01 +00:00
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
type connectionType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
peerConn connectionType = iota
|
|
|
|
serverConn
|
|
|
|
)
|
|
|
|
|
|
|
|
type contact struct {
|
2019-08-02 01:09:01 +00:00
|
|
|
id string
|
|
|
|
state connections.ConnectionState
|
2019-09-26 23:43:34 +00:00
|
|
|
ctype connectionType
|
2019-08-02 01:09:01 +00:00
|
|
|
|
|
|
|
ticks int
|
|
|
|
backoff int
|
|
|
|
}
|
|
|
|
|
|
|
|
type contactRetry struct {
|
2019-09-26 23:43:34 +00:00
|
|
|
bus event.Manager
|
|
|
|
queue event.Queue
|
|
|
|
networkUp bool
|
2021-03-19 21:20:22 +00:00
|
|
|
running bool
|
2019-08-02 01:09:01 +00:00
|
|
|
breakChan chan bool
|
2021-03-19 21:20:22 +00:00
|
|
|
onion string
|
2021-05-07 20:54:12 +00:00
|
|
|
lastCheck time.Time
|
2019-08-02 01:09:01 +00:00
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
connections sync.Map //[string]*contact
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
// NewConnectionRetry returns a Plugin that when started will retry connecting to contacts with a backoff timing
|
2021-03-19 21:20:22 +00:00
|
|
|
func NewConnectionRetry(bus event.Manager, onion string) Plugin {
|
|
|
|
cr := &contactRetry{bus: bus, queue: event.NewQueue(), breakChan: make(chan bool), connections: sync.Map{}, networkUp: false, onion: onion}
|
2019-08-02 01:09:01 +00:00
|
|
|
return cr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *contactRetry) Start() {
|
2021-03-19 21:20:22 +00:00
|
|
|
if !cr.running {
|
|
|
|
go cr.run()
|
|
|
|
} else {
|
|
|
|
log.Errorf("Attempted to start Contact Retry plugin twice for %v", cr.onion)
|
|
|
|
}
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *contactRetry) run() {
|
2021-03-19 21:20:22 +00:00
|
|
|
cr.running = true
|
2019-08-14 20:56:45 +00:00
|
|
|
cr.bus.Subscribe(event.PeerStateChange, cr.queue)
|
2019-09-26 23:43:34 +00:00
|
|
|
cr.bus.Subscribe(event.ACNStatus, cr.queue)
|
|
|
|
cr.bus.Subscribe(event.ServerStateChange, cr.queue)
|
|
|
|
|
2019-08-02 01:09:01 +00:00
|
|
|
for {
|
2021-05-07 20:54:12 +00:00
|
|
|
if time.Since(cr.lastCheck) > tickTime {
|
|
|
|
cr.retryDisconnected()
|
|
|
|
cr.lastCheck = time.Now()
|
|
|
|
}
|
2019-08-02 01:09:01 +00:00
|
|
|
select {
|
2019-08-14 20:56:45 +00:00
|
|
|
case e := <-cr.queue.OutChan():
|
2019-08-02 01:09:01 +00:00
|
|
|
switch e.EventType {
|
|
|
|
case event.PeerStateChange:
|
2021-03-19 21:20:22 +00:00
|
|
|
state := connections.ConnectionStateToType()[e.Data[event.ConnectionState]]
|
2019-08-02 01:09:01 +00:00
|
|
|
peer := e.Data[event.RemotePeer]
|
2019-09-26 23:43:34 +00:00
|
|
|
cr.handleEvent(peer, state, peerConn)
|
|
|
|
|
|
|
|
case event.ServerStateChange:
|
2021-03-19 21:20:22 +00:00
|
|
|
state := connections.ConnectionStateToType()[e.Data[event.ConnectionState]]
|
2019-09-26 23:43:34 +00:00
|
|
|
server := e.Data[event.GroupServer]
|
|
|
|
cr.handleEvent(server, state, serverConn)
|
|
|
|
|
|
|
|
case event.ACNStatus:
|
2021-04-13 22:12:12 +00:00
|
|
|
prog := e.Data[event.Progress]
|
2021-06-02 18:34:57 +00:00
|
|
|
if prog == "100" && !cr.networkUp {
|
2019-09-26 23:43:34 +00:00
|
|
|
cr.networkUp = true
|
|
|
|
cr.connections.Range(func(k, v interface{}) bool {
|
|
|
|
p := v.(*contact)
|
|
|
|
p.ticks = 0
|
|
|
|
p.backoff = 1
|
2020-09-21 21:26:28 +00:00
|
|
|
if p.ctype == peerConn {
|
|
|
|
cr.bus.Publish(event.NewEvent(event.RetryPeerRequest, map[event.Field]string{event.RemotePeer: p.id}))
|
|
|
|
}
|
2021-04-28 19:47:55 +00:00
|
|
|
if p.ctype == serverConn {
|
|
|
|
cr.bus.Publish(event.NewEvent(event.RetryServerRequest, map[event.Field]string{event.GroupServer: p.id}))
|
|
|
|
}
|
2019-09-26 23:43:34 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
} else if prog != "100" {
|
|
|
|
cr.networkUp = false
|
|
|
|
}
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(tickTime):
|
2021-05-07 20:54:12 +00:00
|
|
|
continue
|
2019-08-02 01:09:01 +00:00
|
|
|
|
|
|
|
case <-cr.breakChan:
|
2021-03-19 21:20:22 +00:00
|
|
|
cr.running = false
|
2019-08-02 01:09:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:54:12 +00:00
|
|
|
func (cr *contactRetry) retryDisconnected() {
|
|
|
|
cr.connections.Range(func(k, v interface{}) bool {
|
|
|
|
p := v.(*contact)
|
|
|
|
|
|
|
|
if p.state == connections.DISCONNECTED {
|
|
|
|
p.ticks++
|
2021-05-07 23:16:22 +00:00
|
|
|
if p.ticks >= p.backoff {
|
2021-05-07 20:54:12 +00:00
|
|
|
p.ticks = 0
|
|
|
|
if cr.networkUp {
|
|
|
|
if p.ctype == peerConn {
|
|
|
|
cr.bus.Publish(event.NewEvent(event.RetryPeerRequest, map[event.Field]string{event.RemotePeer: p.id}))
|
|
|
|
}
|
|
|
|
if p.ctype == serverConn {
|
|
|
|
cr.bus.Publish(event.NewEvent(event.RetryServerRequest, map[event.Field]string{event.GroupServer: p.id}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
func (cr *contactRetry) handleEvent(id string, state connections.ConnectionState, ctype connectionType) {
|
|
|
|
if _, exists := cr.connections.Load(id); !exists {
|
2021-05-07 23:16:22 +00:00
|
|
|
p := &contact{id: id, state: connections.DISCONNECTED, backoff: 0, ticks: 0, ctype: ctype}
|
2019-09-26 23:43:34 +00:00
|
|
|
cr.connections.Store(id, p)
|
2019-08-02 01:09:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
pinf, _ := cr.connections.Load(id)
|
|
|
|
p := pinf.(*contact)
|
2019-08-02 01:09:01 +00:00
|
|
|
if state == connections.DISCONNECTED || state == connections.FAILED || state == connections.KILLED {
|
|
|
|
p.state = connections.DISCONNECTED
|
2021-05-07 23:16:22 +00:00
|
|
|
if p.backoff == 0 {
|
|
|
|
p.backoff = 1
|
2021-06-24 01:29:23 +00:00
|
|
|
} else if p.backoff < maxBackoff {
|
2019-08-02 01:09:01 +00:00
|
|
|
p.backoff *= 2
|
|
|
|
}
|
|
|
|
p.ticks = 0
|
|
|
|
} else if state == connections.CONNECTING || state == connections.CONNECTED {
|
|
|
|
p.state = state
|
|
|
|
} else if state == connections.AUTHENTICATED {
|
|
|
|
p.state = state
|
2021-05-07 23:16:22 +00:00
|
|
|
p.backoff = 0
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *contactRetry) Shutdown() {
|
|
|
|
cr.breakChan <- true
|
2021-03-19 21:20:22 +00:00
|
|
|
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|