Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosedSemaphore = errors.New("gtunnel semaphore is closed")
ErrClosedSemaphore is returned when semaphore is already closed.
var ErrClosedTunnel = errors.New("gtunnel tunnel is closed")
ErrClosedTunnel is returned when tunnel is already closed.
Functions ¶
This section is empty.
Types ¶
type Semaphore ¶
Simple Semaphore implementation with buffer and closer. Acquire() is guaranteed to succeed as long as buffer is not empty. If buffer is empty, Acquire() blocks until either a resource has been released back or Semaphore is closed, which will return an error. Closing a Semaphore does not guarantee future Acquire() calls to fail immediately, as it depends on the size of the underlying buffer and the thread scheduling. However, once Close() returns, all future calls to Acquire() is guaranteed to fail.
func NewSemaphore ¶
func (*Semaphore) Acquire ¶
Non-blocking unless buffer is drained. Returns error if buffer is drained and Semaphore has been closed. If this function returns nil, it is guaranteed that thread has acquired a resource, such that a call to Wait() will block until the resource is released.
func (*Semaphore) Close ¶
func (sem *Semaphore) Close()
Close this Semaphore, and flush out all unused resources. After this function returns, all future calls to Acquire is guaranteed to fail.
type Tunnel ¶
type Tunnel struct {
// contains filtered or unexported fields
}
Tunnel is a clean wrapper around native Go channel to allow cleanly closing the channel without throwing a panic. When Tunnel is closed, it waits until all goroutines waiting to send data into the tunnel are unblocked.
func NewBuffered ¶
NewBuffered creates a new Tunnel with buffer.
func NewUnbuffered ¶
func NewUnbuffered() *Tunnel
NewUnbuffered creates a new Tunnel with no buffer.
Example ¶
tn := tunnel.NewUnbuffered()
for i := 0; i < 10; i++ {
go func() {
err := tn.Send("something")
if err != nil {
fmt.Println("Channel is closed!")
}
}()
}
go func() {
for data := range tn.Out() {
fmt.Println(data.(string))
}
}()
tn.Close()
tn.Wait()