2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-02-02 16:56:53 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-02-02 16:56:53 +08:00
|
|
|
|
2019-06-09 10:33:16 +08:00
|
|
|
// Package gchan provides graceful channel for no panic operations.
|
2019-01-16 13:35:16 +08:00
|
|
|
//
|
2019-04-03 23:39:31 +08:00
|
|
|
// It's safe to call Chan.Push/Close functions repeatedly.
|
2018-02-02 16:56:53 +08:00
|
|
|
package gchan
|
|
|
|
|
|
|
|
import (
|
2019-06-12 11:21:10 +08:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/g/container/gtype"
|
2018-02-02 16:56:53 +08:00
|
|
|
)
|
|
|
|
|
2019-06-09 10:33:16 +08:00
|
|
|
// Graceful channel.
|
2018-02-02 16:56:53 +08:00
|
|
|
type Chan struct {
|
2019-06-12 11:21:10 +08:00
|
|
|
channel chan interface{}
|
|
|
|
closed *gtype.Bool
|
2018-02-02 16:56:53 +08:00
|
|
|
}
|
|
|
|
|
2019-06-09 10:33:16 +08:00
|
|
|
// New creates a graceful channel with given <limit>.
|
2018-02-02 16:56:53 +08:00
|
|
|
func New(limit int) *Chan {
|
2019-06-12 11:21:10 +08:00
|
|
|
return &Chan{
|
|
|
|
channel: make(chan interface{}, limit),
|
|
|
|
closed: gtype.NewBool(),
|
|
|
|
}
|
2018-02-02 16:56:53 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
// Push pushes <value> to channel.
|
|
|
|
// It is safe to be called repeatedly.
|
|
|
|
func (c *Chan) Push(value interface{}) error {
|
2019-06-12 11:21:10 +08:00
|
|
|
if c.closed.Val() {
|
|
|
|
return errors.New("channel is closed")
|
|
|
|
}
|
|
|
|
c.channel <- value
|
|
|
|
return nil
|
2018-02-02 16:56:53 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
// Pop pops value from channel.
|
|
|
|
// If there's no value in channel, it would block to wait.
|
2019-06-09 10:33:16 +08:00
|
|
|
// If the channel is closed, it will return a nil value immediately.
|
2019-04-03 23:39:31 +08:00
|
|
|
func (c *Chan) Pop() interface{} {
|
2019-06-12 11:21:10 +08:00
|
|
|
return <-c.channel
|
2018-02-02 16:56:53 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
// Close closes the channel.
|
|
|
|
// It is safe to be called repeatedly.
|
|
|
|
func (c *Chan) Close() {
|
2019-06-12 11:21:10 +08:00
|
|
|
if !c.closed.Set(true) {
|
|
|
|
close(c.channel)
|
|
|
|
}
|
2018-02-02 16:56:53 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:39:31 +08:00
|
|
|
// See Len.
|
|
|
|
func (c *Chan) Size() int {
|
2019-06-12 11:21:10 +08:00
|
|
|
return c.Len()
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the channel.
|
|
|
|
func (c *Chan) Len() int {
|
|
|
|
return len(c.channel)
|
2019-06-12 11:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cap returns the capacity of the channel.
|
|
|
|
func (c *Chan) Cap() int {
|
|
|
|
return cap(c.channel)
|
|
|
|
}
|