Compare commits
	
		
			2 Commits
		
	
	
		
			f40eca261b
			...
			9fd40a37b8
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9fd40a37b8 | |||
| 709f41a974 | 
							
								
								
									
										29
									
								
								sync/gate.go
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								sync/gate.go
									
									
									
									
									
								
							@ -2,12 +2,23 @@ package usync
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import "sync"
 | 
					import "sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Error defines errors that this package can produce
 | 
				
			||||||
 | 
					type Error string; const (
 | 
				
			||||||
 | 
						ErrAlreadyClosed Error = "AlreadyClosed"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Error fullfills the error interface.
 | 
				
			||||||
 | 
					func (err Error) Error () string {
 | 
				
			||||||
 | 
						return string(err)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Gate wraps a channel and allows the receiver to abruptly stop receiving
 | 
					// Gate wraps a channel and allows the receiver to abruptly stop receiving
 | 
				
			||||||
// messages without causing the sender to lock up.
 | 
					// messages without causing the sender to lock up.
 | 
				
			||||||
type Gate[T any] struct {
 | 
					type Gate[T any] struct {
 | 
				
			||||||
	channel    chan T
 | 
						channel    chan T
 | 
				
			||||||
	lock       sync.RWMutex
 | 
						lock       sync.RWMutex
 | 
				
			||||||
	open       bool
 | 
						open       bool
 | 
				
			||||||
 | 
						bufferSize int
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewGate creates a new gate with no buffer.
 | 
					// NewGate creates a new gate with no buffer.
 | 
				
			||||||
@ -23,24 +34,30 @@ func NewBufferedGate[T any] (buffer int) Gate[T] {
 | 
				
			|||||||
	return Gate[T] {
 | 
						return Gate[T] {
 | 
				
			||||||
		channel:    make(chan T, buffer),
 | 
							channel:    make(chan T, buffer),
 | 
				
			||||||
		open:       true,
 | 
							open:       true,
 | 
				
			||||||
 | 
							bufferSize: buffer,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Send sends and item to the channel, returning whether the item was sent.
 | 
					// Send sends and item to the channel, returning whether the item was sent.
 | 
				
			||||||
func (this *Gate[T]) Send (item T) bool {
 | 
					func (this *Gate[T]) Send (item T) bool {
 | 
				
			||||||
	if !this.Open() { return false }
 | 
						this.lock.RLock()
 | 
				
			||||||
 | 
						defer this.lock.RUnlock()
 | 
				
			||||||
 | 
						if !this.open { return false }
 | 
				
			||||||
	this.channel <- item
 | 
						this.channel <- item
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Receive returns a receive-only channel that can be used to receive items.
 | 
					// Receive returns a receive-only channel that can be used to receive items.
 | 
				
			||||||
func (this *Gate[T]) Receive () <- chan T {
 | 
					func (this *Gate[T]) Receive () <- chan T {
 | 
				
			||||||
 | 
						this.lock.RLock()
 | 
				
			||||||
 | 
						defer this.lock.RUnlock()
 | 
				
			||||||
	return this.channel
 | 
						return this.channel
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Close closes the gate, drains all remaining messages, and closes the channel.
 | 
					// Close closes the gate, drains all remaining messages, and closes the channel.
 | 
				
			||||||
func (this *Gate[T]) Close () error {
 | 
					func (this *Gate[T]) Close () error {
 | 
				
			||||||
	this.lock.Lock()
 | 
						this.lock.Lock()
 | 
				
			||||||
 | 
						if !this.open { return ErrAlreadyClosed }
 | 
				
			||||||
	this.open = false
 | 
						this.open = false
 | 
				
			||||||
	this.lock.Unlock()
 | 
						this.lock.Unlock()
 | 
				
			||||||
	for len(this.channel) > 0 { <- this.channel }
 | 
						for len(this.channel) > 0 { <- this.channel }
 | 
				
			||||||
@ -48,6 +65,16 @@ func (this *Gate[T]) Close () error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Reset re-opens the gate if it is closed, and creates a new channel.
 | 
				
			||||||
 | 
					func (this *Gate[T]) Reset () error {
 | 
				
			||||||
 | 
						this.lock.Lock()
 | 
				
			||||||
 | 
						defer this.lock.Unlock()
 | 
				
			||||||
 | 
						this.open = true
 | 
				
			||||||
 | 
						if this.channel != nil { close(this.channel) }
 | 
				
			||||||
 | 
						this.channel = make(chan T, this.bufferSize)
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Open returns whether the gate is open.
 | 
					// Open returns whether the gate is open.
 | 
				
			||||||
func (this *Gate[T]) Open () bool {
 | 
					func (this *Gate[T]) Open () bool {
 | 
				
			||||||
	this.lock.RLock()
 | 
						this.lock.RLock()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user