Sha256: 90bd383a3a4a7c3e96c5705e752f778730b3c5c8b5a964e6f7dc85191567a2b1

Contents?: true

Size: 1.68 KB

Versions: 110

Compression:

Stored size: 1.68 KB

Contents

package paasio

import (
	"io"
	"sync"
)

const testVersion = 3

// NewWriteCounter returns an implementation of WriteCounter.  Calls to
// w.Write() are not guaranteed to be synchronized.
func NewWriteCounter(w io.Writer) WriteCounter {
	return &writeCounter{
		w:       w,
		counter: newCounter(),
	}
}

// NewReadCounter returns an implementation of ReadCounter.  Calls to
// r.Read() are not guaranteed to be synchronized.
func NewReadCounter(r io.Reader) ReadCounter {
	return &readCounter{
		r:       r,
		counter: newCounter(),
	}
}

// NewReadWriteCounter returns an implementation of ReadWriteCounter.
// Calls to rw.Write() and rw.Read() are not guaranteed to be synchronized.
func NewReadWriteCounter(rw io.ReadWriter) ReadWriteCounter {
	return &rwCounter{
		NewWriteCounter(rw),
		NewReadCounter(rw),
	}
}

type readCounter struct {
	r io.Reader
	counter
}

func (rc *readCounter) Read(p []byte) (int, error) {
	m, err := rc.r.Read(p)
	rc.addBytes(m)
	return m, err
}

func (rc *readCounter) ReadCount() (n int64, nops int) {
	return rc.count()
}

type writeCounter struct {
	w io.Writer
	counter
}

func (wc *writeCounter) Write(p []byte) (int, error) {
	m, err := wc.w.Write(p)
	wc.addBytes(m)
	return m, err
}

func (wc *writeCounter) WriteCount() (n int64, nops int) {
	return wc.count()
}

type counter struct {
	bytes int64
	ops   int
	mutex *sync.Mutex
}

func newCounter() counter {
	return counter{mutex: new(sync.Mutex)}
}

func (c *counter) addBytes(n int) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	c.bytes += int64(n)
	c.ops += 1
}

func (c *counter) count() (int64, int) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	return c.bytes, c.ops
}

type rwCounter struct {
	WriteCounter
	ReadCounter
}

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
trackler-2.0.8.9 tracks/go/exercises/paasio/example.go
trackler-2.0.8.8 tracks/go/exercises/paasio/example.go
trackler-2.0.8.7 tracks/go/exercises/paasio/example.go
trackler-2.0.8.6 tracks/go/exercises/paasio/example.go
trackler-2.0.8.5 tracks/go/exercises/paasio/example.go
trackler-2.0.8.4 tracks/go/exercises/paasio/example.go
trackler-2.0.8.3 tracks/go/exercises/paasio/example.go
trackler-2.0.8.2 tracks/go/exercises/paasio/example.go
trackler-2.0.8.1 tracks/go/exercises/paasio/example.go
trackler-2.0.7.0 tracks/go/exercises/paasio/example.go
trackler-2.0.6.44 tracks/go/exercises/paasio/example.go
trackler-2.0.6.43 tracks/go/exercises/paasio/example.go
trackler-2.0.6.42 tracks/go/exercises/paasio/example.go
trackler-2.0.6.41 tracks/go/exercises/paasio/example.go
trackler-2.0.6.40 tracks/go/exercises/paasio/example.go
trackler-2.0.6.39 tracks/go/exercises/paasio/example.go
trackler-2.0.6.38 tracks/go/exercises/paasio/example.go
trackler-2.0.6.37 tracks/go/exercises/paasio/example.go
trackler-2.0.6.36 tracks/go/exercises/paasio/example.go
trackler-2.0.6.35 tracks/go/exercises/paasio/example.go