forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Add a hot reloading RIE side implementation #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0cbe48
add hot reloading entrypoints
dfangl 805d1b3
add filewatcher implementation
dfangl 82b1b62
add poller based approach from hugo, to be tested
dfangl b65a6da
changes after PR comments
dfangl e766f42
some more refactoring
dfangl 20ac329
add proper close, move comments
dfangl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// This package is adapted from https://github.com/gohugoio/hugo/tree/master/watcher/filenotify, Apache-2.0 License. | ||
|
||
// Package filenotify provides a mechanism for watching file(s) for changes. | ||
// Generally leans on fsnotify, but provides a poll-based notifier which fsnotify does not support. | ||
// These are wrapped up in a common interface so that either can be used interchangeably in your code. | ||
// | ||
// This package is adapted from https://github.com/moby/moby/tree/master/pkg/filenotify, Apache-2.0 License. | ||
// Hopefully this can be replaced with an external package sometime in the future, see https://github.com/fsnotify/fsnotify/issues/9 | ||
package filenotify | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/sys/unix" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
// FileWatcher is an interface for implementing file notification watchers | ||
type FileWatcher interface { | ||
Events() <-chan fsnotify.Event | ||
Errors() <-chan error | ||
Add(name string) error | ||
Remove(name string) error | ||
Close() error | ||
} | ||
|
||
func shouldUseEventWatcher() bool { | ||
// Whether to use an event watcher or polling mechanism | ||
var utsname unix.Utsname | ||
err := unix.Uname(&utsname) | ||
release := strings.TrimRight(string(utsname.Release[:]), "\x00") | ||
log.Println("Release detected: ", release) | ||
// cheap check if we are in Docker desktop or not. | ||
// We could also inspect the mounts, but that would be more complicated and needs more parsing | ||
return err == nil && !(strings.Contains(release, "linuxkit") || strings.Contains(release, "WSL2")) | ||
} | ||
|
||
// New tries to use a fs-event watcher, and falls back to the poller if there is an error | ||
func New(interval time.Duration) (FileWatcher, error) { | ||
if shouldUseEventWatcher() { | ||
if watcher, err := NewEventWatcher(); err == nil { | ||
log.Debugln("Using event based filewatcher") | ||
return watcher, nil | ||
} | ||
} | ||
log.Debugln("Using polling based filewatcher") | ||
return NewPollingWatcher(interval), nil | ||
} | ||
|
||
// NewPollingWatcher returns a poll-based file watcher | ||
func NewPollingWatcher(interval time.Duration) FileWatcher { | ||
return &filePoller{ | ||
interval: interval, | ||
done: make(chan struct{}), | ||
events: make(chan fsnotify.Event), | ||
errors: make(chan error), | ||
} | ||
} | ||
|
||
// NewEventWatcher returns a fs-event based file watcher | ||
func NewEventWatcher() (FileWatcher, error) { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &fsNotifyWatcher{watcher}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This package is adapted from https://github.com/gohugoio/hugo/tree/master/watcher/filenotify, Apache-2.0 License. | ||
|
||
// Package filenotify is adapted from https://github.com/moby/moby/tree/master/pkg/filenotify, Apache-2.0 License. | ||
// Hopefully this can be replaced with an external package sometime in the future, see https://github.com/fsnotify/fsnotify/issues/9 | ||
package filenotify | ||
|
||
import "github.com/fsnotify/fsnotify" | ||
|
||
// fsNotifyWatcher wraps the fsnotify package to satisfy the FileNotifier interface | ||
type fsNotifyWatcher struct { | ||
*fsnotify.Watcher | ||
} | ||
|
||
// Events returns the fsnotify event channel receiver | ||
func (w *fsNotifyWatcher) Events() <-chan fsnotify.Event { | ||
return w.Watcher.Events | ||
} | ||
|
||
// Errors returns the fsnotify error channel receiver | ||
func (w *fsNotifyWatcher) Errors() <-chan error { | ||
return w.Watcher.Errors | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.