go-gun/gun/peer.go

260 lines
6.1 KiB
Go
Raw Normal View History

2019-02-20 20:54:46 +00:00
package gun
import (
"context"
2019-02-22 18:40:02 +00:00
"encoding/json"
2019-02-22 06:46:19 +00:00
"fmt"
2019-02-20 20:54:46 +00:00
"net/url"
"sync"
2019-02-25 05:14:26 +00:00
"time"
2019-02-20 20:54:46 +00:00
"github.com/gorilla/websocket"
)
2019-02-22 09:23:14 +00:00
type ErrPeer struct {
Err error
2019-02-25 05:14:26 +00:00
Peer *Peer
2019-02-22 09:23:14 +00:00
}
2019-02-25 05:14:26 +00:00
func (e *ErrPeer) Error() string { return fmt.Sprintf("Error on peer %v: %v", e.Peer, e.Err) }
2019-02-22 09:23:14 +00:00
2019-02-25 05:14:26 +00:00
type Peer struct {
url string
newConn func() (PeerConn, error)
sleepOnErr time.Duration // TODO: would be better as backoff
id string
connCurrent PeerConn
connBad bool // If true, don't try anything
connLock sync.Mutex
}
func newPeer(url string, newConn func() (PeerConn, error), sleepOnErr time.Duration) (*Peer, error) {
p := &Peer{url: url, newConn: newConn, sleepOnErr: sleepOnErr}
var err error
if p.connCurrent, err = newConn(); err != nil {
return nil, err
}
return p, nil
}
func (p *Peer) ID() string { return p.id }
func (p *Peer) String() string {
id := ""
if p.id != "" {
id = "(id: " + p.id + ")"
}
connStatus := "connected"
if p.Conn() == nil {
connStatus = "disconnected"
}
return fmt.Sprintf("Peer%v %v (%v)", id, p.url, connStatus)
}
func (p *Peer) reconnect() (err error) {
p.connLock.Lock()
defer p.connLock.Unlock()
if p.connCurrent == nil && p.connBad {
p.connBad = false
if p.connCurrent, err = p.newConn(); err != nil {
p.connBad = true
time.AfterFunc(p.sleepOnErr, func() { p.reconnect() })
}
}
return
}
// Can be nil peer if currently bad or closed
func (p *Peer) Conn() PeerConn {
p.connLock.Lock()
defer p.connLock.Unlock()
return p.connCurrent
}
func (p *Peer) markConnErrored(conn PeerConn) {
p.connLock.Lock()
defer p.connLock.Unlock()
if conn == p.connCurrent {
p.connCurrent = nil
p.connBad = true
conn.Close()
time.AfterFunc(p.sleepOnErr, func() { p.reconnect() })
}
}
func (p *Peer) send(ctx context.Context, msg *Message, moreMsgs ...*Message) (ok bool, err error) {
conn := p.Conn()
if conn == nil {
return false, nil
}
// Clone them with peer "to"
updatedMsg := &Message{}
*updatedMsg = *msg
updatedMsg.To = p.url
updatedMoreMsgs := make([]*Message, len(moreMsgs))
for i, moreMsg := range moreMsgs {
updatedMoreMsg := &Message{}
*updatedMoreMsg = *moreMsg
updatedMoreMsg.To = p.url
updatedMoreMsgs[i] = updatedMoreMsg
}
if err = conn.Send(ctx, updatedMsg, updatedMoreMsgs...); err != nil {
p.markConnErrored(conn)
return false, err
} else {
return true, nil
}
}
func (p *Peer) receive(ctx context.Context) (ok bool, msgs []*Message, err error) {
if conn := p.Conn(); conn == nil {
return false, nil, nil
} else if msgs, err = conn.Receive(ctx); err != nil {
p.markConnErrored(conn)
return false, nil, err
} else {
return true, msgs, nil
}
}
func (p *Peer) Close() error {
p.connLock.Lock()
defer p.connLock.Unlock()
var err error
if p.connCurrent != nil {
err = p.connCurrent.Close()
p.connCurrent = nil
}
p.connBad = false
return err
}
func (p *Peer) Closed() bool {
p.connLock.Lock()
defer p.connLock.Unlock()
return p.connCurrent == nil && !p.connBad
}
type PeerConn interface {
2019-02-22 18:40:02 +00:00
Send(ctx context.Context, msg *Message, moreMsgs ...*Message) error
// Chan is closed on first err, when context is closed, or when peer is closed
Receive(ctx context.Context) ([]*Message, error)
2019-02-20 20:54:46 +00:00
Close() error
}
2019-02-25 05:14:26 +00:00
var PeerURLSchemes map[string]func(context.Context, *url.URL) (PeerConn, error)
2019-02-25 04:23:15 +00:00
func init() {
2019-02-25 05:14:26 +00:00
PeerURLSchemes = map[string]func(context.Context, *url.URL) (PeerConn, error){
"http": func(ctx context.Context, peerURL *url.URL) (PeerConn, error) {
2019-02-25 04:23:15 +00:00
schemeChangedURL := &url.URL{}
*schemeChangedURL = *peerURL
schemeChangedURL.Scheme = "ws"
2019-02-25 05:14:26 +00:00
return NewPeerConnWebSocket(ctx, schemeChangedURL)
2019-02-25 04:23:15 +00:00
},
2019-02-25 05:14:26 +00:00
"ws": func(ctx context.Context, peerURL *url.URL) (PeerConn, error) {
return NewPeerConnWebSocket(ctx, peerURL)
2019-02-25 04:23:15 +00:00
},
}
2019-02-20 20:54:46 +00:00
}
2019-02-25 05:14:26 +00:00
func NewPeerConn(ctx context.Context, peerURL string) (PeerConn, error) {
2019-02-22 06:46:19 +00:00
if parsedURL, err := url.Parse(peerURL); err != nil {
return nil, err
} else if peerNew := PeerURLSchemes[parsedURL.Scheme]; peerNew == nil {
return nil, fmt.Errorf("Unknown peer URL scheme %v", parsedURL.Scheme)
} else {
return peerNew(ctx, parsedURL)
}
}
2019-02-25 05:14:26 +00:00
type PeerConnWebSocket struct {
2019-02-22 18:40:02 +00:00
Underlying *websocket.Conn
WriteLock sync.Mutex
2019-02-20 20:54:46 +00:00
}
2019-02-25 05:14:26 +00:00
func NewPeerConnWebSocket(ctx context.Context, peerUrl *url.URL) (*PeerConnWebSocket, error) {
2019-02-20 20:54:46 +00:00
conn, _, err := websocket.DefaultDialer.DialContext(ctx, peerUrl.String(), nil)
if err != nil {
return nil, err
}
2019-02-25 05:14:26 +00:00
return &PeerConnWebSocket{Underlying: conn}, nil
2019-02-20 20:54:46 +00:00
}
2019-02-22 06:46:19 +00:00
2019-02-25 05:14:26 +00:00
func (p *PeerConnWebSocket) Send(ctx context.Context, msg *Message, moreMsgs ...*Message) error {
2019-02-22 18:40:02 +00:00
// If there are more, send all as an array of JSON strings, otherwise just the msg
var toWrite interface{}
if len(moreMsgs) == 0 {
toWrite = msg
} else {
b, err := json.Marshal(msg)
if err != nil {
return err
}
msgs := []string{string(b)}
for _, nextMsg := range moreMsgs {
if b, err = json.Marshal(nextMsg); err != nil {
return err
}
msgs = append(msgs, string(b))
}
toWrite = msgs
}
// Send async so we can wait on context
errCh := make(chan error, 1)
go func() {
p.WriteLock.Lock()
defer p.WriteLock.Unlock()
errCh <- p.Underlying.WriteJSON(toWrite)
}()
2019-02-22 18:40:02 +00:00
select {
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}
2019-02-25 05:14:26 +00:00
func (p *PeerConnWebSocket) Receive(ctx context.Context) ([]*Message, error) {
2019-02-22 18:40:02 +00:00
bytsCh := make(chan []byte, 1)
errCh := make(chan error, 1)
go func() {
if _, b, err := p.Underlying.ReadMessage(); err != nil {
errCh <- err
} else {
bytsCh <- b
}
}()
select {
case err := <-errCh:
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
case byts := <-bytsCh:
// If it's a JSON array, it means it's an array of JSON strings, otherwise it's one message
if byts[0] != '[' {
var msg Message
if err := json.Unmarshal(byts, &msg); err != nil {
return nil, err
}
return []*Message{&msg}, nil
}
var jsonStrs []string
if err := json.Unmarshal(byts, &jsonStrs); err != nil {
return nil, err
}
msgs := make([]*Message, len(jsonStrs))
for i, jsonStr := range jsonStrs {
if err := json.Unmarshal([]byte(jsonStr), &(msgs[i])); err != nil {
return nil, err
}
}
return msgs, nil
}
2019-02-22 06:46:19 +00:00
}
2019-02-25 05:14:26 +00:00
func (p *PeerConnWebSocket) Close() error {
2019-02-22 18:40:02 +00:00
return p.Underlying.Close()
2019-02-22 06:46:19 +00:00
}