Files
upstream-ory-kratos/oryx/watcherx/definitions.go
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

79 lines
1.8 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package watcherx
import (
"context"
"fmt"
"net/url"
"github.com/pkg/errors"
)
type (
errSchemeUnknown struct {
scheme string
}
EventChannel chan Event
Watcher interface {
// DispatchNow fires the watcher and causes an event.
//
// WARNING: The returned channel must be read or no further events will
// be propagated due to a deadlock.
DispatchNow() (<-chan int, error)
}
dispatcher struct {
ctx context.Context
trigger chan struct{}
done chan int
}
)
var (
// ErrSchemeUnknown is just for checking with errors.Is()
ErrSchemeUnknown = &errSchemeUnknown{}
ErrWatcherNotRunning = fmt.Errorf("watcher is not running")
)
func (e *errSchemeUnknown) Is(other error) bool {
_, ok := other.(*errSchemeUnknown)
return ok
}
func (e *errSchemeUnknown) Error() string {
return fmt.Sprintf("unknown scheme '%s' to watch", e.scheme)
}
func newDispatcher(ctx context.Context) *dispatcher {
return &dispatcher{
ctx: ctx,
trigger: make(chan struct{}),
done: make(chan int),
}
}
func (d *dispatcher) DispatchNow() (<-chan int, error) {
if d.trigger == nil {
return nil, ErrWatcherNotRunning
}
// The trigger send must respect cancellation. Once the watcher's context is
// done its receiver goroutine has returned, so a bare send would block
// forever and wedge the caller.
select {
case d.trigger <- struct{}{}:
return d.done, nil
case <-d.ctx.Done():
return nil, errors.WithStack(ErrWatcherNotRunning)
}
}
func Watch(ctx context.Context, u *url.URL, c EventChannel) (Watcher, error) {
switch u.Scheme {
// see urlx.Parse for why the empty string is also file
case "file", "":
return WatchFile(ctx, u.Path, c)
}
return nil, &errSchemeUnknown{u.Scheme}
}