Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed). - CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels - New CheckinButton in both authenticated and public headers (Turnstile supported) - New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset - New /docs 12-section beginner guide with anchor navigation - New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi) - README: document custom features, live demo https://ai.88531.cn
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package common
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
func SafeSendBool(ch chan bool, value bool) (closed bool) {
|
|
defer func() {
|
|
// Recover from panic if one occured. A panic would mean the channel was closed.
|
|
if recover() != nil {
|
|
closed = true
|
|
}
|
|
}()
|
|
|
|
// This will panic if the channel is closed.
|
|
ch <- value
|
|
|
|
// If the code reaches here, then the channel was not closed.
|
|
return false
|
|
}
|
|
|
|
func SafeSendString(ch chan string, value string) (closed bool) {
|
|
defer func() {
|
|
// Recover from panic if one occured. A panic would mean the channel was closed.
|
|
if recover() != nil {
|
|
closed = true
|
|
}
|
|
}()
|
|
|
|
// This will panic if the channel is closed.
|
|
ch <- value
|
|
|
|
// If the code reaches here, then the channel was not closed.
|
|
return false
|
|
}
|
|
|
|
// SafeSendStringTimeout send, return true, else return false
|
|
func SafeSendStringTimeout(ch chan string, value string, timeout int) (closed bool) {
|
|
defer func() {
|
|
// Recover from panic if one occured. A panic would mean the channel was closed.
|
|
if recover() != nil {
|
|
closed = false
|
|
}
|
|
}()
|
|
|
|
// This will panic if the channel is closed.
|
|
select {
|
|
case ch <- value:
|
|
return true
|
|
case <-time.After(time.Duration(timeout) * time.Second):
|
|
return false
|
|
}
|
|
}
|