sing/common/bufio/race.go
2022-08-13 10:48:38 +08:00

36 lines
654 B
Go

package bufio
import (
"io"
"sync"
"github.com/sagernet/sing/common/buf"
N "github.com/sagernet/sing/common/network"
)
type RaceWriter struct {
upstream N.ExtendedWriter
access sync.Mutex
}
func NewRaceWriter(writer io.Writer) *RaceWriter {
return &RaceWriter{
upstream: NewExtendedWriter(writer),
}
}
func (w *RaceWriter) WriteBuffer(buffer *buf.Buffer) error {
w.access.Lock()
defer w.access.Unlock()
return w.upstream.WriteBuffer(buffer)
}
func (w *RaceWriter) Write(p []byte) (n int, err error) {
w.access.Lock()
defer w.access.Unlock()
return w.upstream.Write(p)
}
func (w *RaceWriter) Upstream() any {
return w.upstream
}