Add race writer

This commit is contained in:
世界 2022-08-13 10:48:38 +08:00
parent 05f9836bff
commit eb2fad956a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 57 additions and 7 deletions

21
Makefile Normal file
View file

@ -0,0 +1,21 @@
fmt:
gofumpt -l -w .
gofmt -s -w .
gci write -s "standard,prefix(github.com/sagernet/),default" .
lint_install:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
lint:
GOOS=linux golangci-lint run ./...
GOOS=windows golangci-lint run ./...
GOOS=darwin golangci-lint run ./...
GOOS=freebsd golangci-lint run ./...
test:
go test -v .
update:
git fetch
git reset FETCH_HEAD --hard
git clean -fdx

36
common/bufio/race.go Normal file
View file

@ -0,0 +1,36 @@
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
}

View file

@ -1,7 +0,0 @@
package box
//go:generate go install -v mvdan.cc/gofumpt@latest
//go:generate go install -v github.com/daixiang0/gci@v0.4.0
//go:generate gofumpt -l -w .
//go:generate gofmt -s -w .
//go:generate gci write -s "standard,prefix(github.com/sagernet/),default" .