Skip to content

Commit

Permalink
feat: better relay
Browse files Browse the repository at this point in the history
  • Loading branch information
theprimeagen committed May 26, 2024
1 parent 576bcd8 commit 8346a17
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ out
assert.debug
debug.doom
fly.toml
lua
.git
2 changes: 1 addition & 1 deletion examples/v2/doom/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func main() {
Cols: d.Cols / 2,
})

enc.AddEncoder(encoder.XorRLE)
//enc.AddEncoder(encoder.XorRLE)
enc.AddEncoder(encoder.Huffman)

relay.send(net.CreateOpen(d.Rows, d.Cols))
Expand Down
36 changes: 2 additions & 34 deletions pkg/v2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ func NewRelay(ws uint16, uuid string, stats *metrics.Metrics) *Relay {
}
}

func (relay *Relay) WithSendCount(count int) *Relay {
relay.send = count
return relay
}

//TODO(post doom): Fix this shit
/** THIS IS SHITTY **/
/** Relay should really just be something in which i hand connections and that
Expand Down Expand Up @@ -98,43 +93,16 @@ func (relay *Relay) NewConnections() chan *Conn {
return relay.conns
}

var count = 0
func (relay *Relay) relayRange(listeners []*Conn, data []byte, wait *sync.WaitGroup) {
for _, conn := range listeners {
conn.msg(data)
}

wait.Done()
}

func (relay *Relay) relay(data []byte) {
// quick write to prevent blocking if there is no listener
select {
case relay.ch <- data:
default:
}

relay.mutex.RLock()
msgCount := (len(relay.listeners) / relay.send + 1)
wait := sync.WaitGroup{}

curr := make([]*Conn, 0, msgCount)
for _, conn := range relay.listeners {
if len(curr) == msgCount {
wait.Add(1)

go relay.relayRange(curr, data, &wait)
curr = make([]*Conn, 0, msgCount)
}
curr = append(curr, conn)
for _, conn := range relay.listeners {
conn.msg(data)
}

if len(curr) > 0 {
wait.Add(1)
go relay.relayRange(curr, data, &wait)
}

wait.Wait()
relay.mutex.RUnlock()
}

Expand Down

0 comments on commit 8346a17

Please sign in to comment.