~egtann/sjs

sjs/error.go -rw-r--r-- 503 bytes
c9f368d1 — Evan Tann adopt go1.13 stdlib errors 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sjs

import "sync"

// OptErr is a channel that we can modify in a threadsafe way. The mutex is
// required to read or modify the underlying channel, which can be created at
// any time by the caller of the library. The exported "C" fieldname
// representating the channel is inspired by the standard library's time.Ticker
// design.
type OptErr struct {
	C chan error
	sync.RWMutex
}

func (o *OptErr) Send(err error) {
	o.RLock()
	defer o.RUnlock()

	if o.C == nil {
		return
	}
	o.C <- err
}