Switch to using goroutines rather than a select()

More complex code since now we have to synchronize between threads,
but a good way to teach myself more about goroutines.
This commit is contained in:
Kovid Goyal
2022-08-26 08:37:14 +05:30
parent ee12349a50
commit 4a49c3940a
5 changed files with 418 additions and 387 deletions

View File

@@ -2,39 +2,6 @@
package utils
import (
"io"
"time"
)
const (
DEFAULT_IO_BUFFER_SIZE = 8192
)
type BytesReader struct {
Data []byte
}
type Reader interface {
ReadWithTimeout(b []byte, timeout time.Duration) (n int, err error)
GetBuf() []byte
}
func (self *BytesReader) Read(b []byte) (n int, err error) {
if len(self.Data) == 0 {
return 0, io.EOF
}
n = copy(b, self.Data)
self.Data = self.Data[n:]
return
}
func (self *BytesReader) ReadWithTimeout(b []byte, timeout time.Duration) (n int, err error) {
return self.Read(b)
}
func (self *BytesReader) GetBuf() (ans []byte) {
ans = self.Data
self.Data = make([]byte, 0)
return
}