FILE / ScuroNeko/mtg

hub/closeable_channel.go

Исходный файл и его история в репозитории.
FILE c9743b5675df007ed13fa7b89acd4b4cbafc960f
Files
mtg/hub/closeable_channel.go
T
2019-10-07 12:13:07 +03:00

60 lines
1.1 KiB
Go

package hub
import (
"context"
"errors"
"time"
"github.com/9seconds/mtg/conntypes"
)
const closeableChannelReadTimeout = 2 * time.Minute
type ChannelReadCloser interface {
Read() (conntypes.Packet, error)
Close()
}
type closeableChannel struct {
channel chan conntypes.Packet
ctx context.Context
cancel context.CancelFunc
}
func (c *closeableChannel) Read() (conntypes.Packet, error) {
timer := time.NewTimer(closeableChannelReadTimeout)
defer timer.Stop()
select {
case <-timer.C:
return nil, errors.New("timeout")
case <-c.ctx.Done():
return nil, errors.New("channel was closed")
case packet := <-c.channel:
return packet, nil
}
}
func (c *closeableChannel) write(packet conntypes.Packet) error {
select {
case <-c.ctx.Done():
return errors.New("channel was closed")
case c.channel <- packet:
return nil
}
}
func (c *closeableChannel) Close() {
c.cancel()
c.channel = nil
}
func newCloseableChannel(ctx context.Context) *closeableChannel {
ctx, cancel := context.WithCancel(ctx)
return &closeableChannel{
channel: make(chan conntypes.Packet),
ctx: ctx,
cancel: cancel,
}
}