add panic if internal watcher creation fails for gfsnotify; improve codes and change comment from chinese to english for gdb

This commit is contained in:
John
2020-02-10 20:37:53 +08:00
parent 784983806a
commit 88684ca00a
12 changed files with 344 additions and 188 deletions

View File

@ -61,16 +61,25 @@ const (
)
const (
REPEAT_EVENT_FILTER_DURATION = time.Millisecond // Duration for repeated event filter.
gFSNOTIFY_EVENT_EXIT = "exit" // Custom exit event for internal usage.
repeatEventFilterDuration = time.Millisecond // Duration for repeated event filter.
callbackExitEventPanicStr = "exit" // Custom exit event for internal usage.
)
var (
defaultWatcher, _ = New() // Default watcher.
defaultWatcher *Watcher // Default watcher.
callbackIdMap = gmap.NewIntAnyMap(true) // Id to callback mapping.
callbackIdGenerator = gtype.NewInt() // Atomic id generator for callback.
)
func init() {
var err error
defaultWatcher, err = New()
if err != nil {
// Default watcher object must be created, or else it panics.
panic(fmt.Sprintf(`creating default fsnotify watcher failed: %s`, err.Error()))
}
}
// New creates and returns a new watcher.
// Note that the watcher number is limited by the file handle setting of the system.
// Eg: fs.inotify.max_user_instances system variable in linux systems.
@ -125,7 +134,8 @@ func RemoveCallback(callbackId int) error {
return nil
}
// Exit is only used in the callback function, which can be used to remove current callback from the watcher.
// Exit is only used in the callback function, which can be used to remove current callback
// of itself from the watcher.
func Exit() {
panic(gFSNOTIFY_EVENT_EXIT)
panic(callbackExitEventPanicStr)
}

View File

@ -22,7 +22,6 @@ func (w *Watcher) startWatchLoop() {
// Event listening.
case ev := <-w.watcher.Events:
//intlog.Print(ev.String())
// Filter the repeated event in custom duration.
w.cache.SetIfNotExist(ev.String(), func() interface{} {
w.events.Push(&Event{
@ -32,7 +31,7 @@ func (w *Watcher) startWatchLoop() {
Watcher: w,
})
return struct{}{}
}, REPEAT_EVENT_FILTER_DURATION)
}, repeatEventFilterDuration)
case err := <-w.watcher.Errors:
intlog.Error(err)
@ -148,7 +147,7 @@ func (w *Watcher) startEventLoop() {
defer func() {
if err := recover(); err != nil {
switch err {
case gFSNOTIFY_EVENT_EXIT:
case callbackExitEventPanicStr:
w.RemoveCallback(callback.Id)
default:
panic(err)

View File

@ -28,7 +28,7 @@ func New(t ...time.Time) *Time {
}
}
// Now returns a time object for now.
// Now creates and returns a time object of now.
func Now() *Time {
return &Time{
time.Now(),
@ -50,7 +50,8 @@ func NewFromStr(str string) *Time {
return nil
}
// NewFromStrFormat creates and returns a Time object with given string and custom format like: Y-m-d H:i:s.
// NewFromStrFormat creates and returns a Time object with given string and
// custom format like: Y-m-d H:i:s.
func NewFromStrFormat(str string, format string) *Time {
if t, err := StrToTimeFormat(str, format); err == nil {
return t
@ -58,7 +59,8 @@ func NewFromStrFormat(str string, format string) *Time {
return nil
}
// NewFromStrLayout creates and returns a Time object with given string and stdlib layout like: 2006-01-02 15:04:05.
// NewFromStrLayout creates and returns a Time object with given string and
// stdlib layout like: 2006-01-02 15:04:05.
func NewFromStrLayout(str string, layout string) *Time {
if t, err := StrToTimeLayout(str, layout); err == nil {
return t
@ -66,7 +68,8 @@ func NewFromStrLayout(str string, layout string) *Time {
return nil
}
// NewFromTimeStamp creates and returns a Time object with given timestamp, which can be in seconds to nanoseconds.
// NewFromTimeStamp creates and returns a Time object with given timestamp,
// which can be in seconds to nanoseconds.
func NewFromTimeStamp(timestamp int64) *Time {
if timestamp == 0 {
return &Time{}