scanner_helper.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package helper
  2. import (
  3. "context"
  4. "github.com/milvus-io/milvus/pkg/util/syncutil"
  5. )
  6. // NewScannerHelper creates a new ScannerHelper.
  7. func NewScannerHelper(scannerName string) *ScannerHelper {
  8. return &ScannerHelper{
  9. scannerName: scannerName,
  10. notifier: syncutil.NewAsyncTaskNotifier[error](),
  11. }
  12. }
  13. // ScannerHelper is a helper for scanner implementation.
  14. type ScannerHelper struct {
  15. scannerName string
  16. notifier *syncutil.AsyncTaskNotifier[error]
  17. }
  18. // Context returns the context of the scanner, which will cancel when the scanner helper is closed.
  19. func (s *ScannerHelper) Context() context.Context {
  20. return s.notifier.Context()
  21. }
  22. // Name returns the name of the scanner.
  23. func (s *ScannerHelper) Name() string {
  24. return s.scannerName
  25. }
  26. // Error returns the error of the scanner.
  27. func (s *ScannerHelper) Error() error {
  28. return s.notifier.BlockAndGetResult()
  29. }
  30. // Done returns a channel that will be closed when the scanner is finished.
  31. func (s *ScannerHelper) Done() <-chan struct{} {
  32. return s.notifier.FinishChan()
  33. }
  34. // Close closes the scanner, block until the Finish is called.
  35. func (s *ScannerHelper) Close() error {
  36. s.notifier.Cancel()
  37. return s.notifier.BlockAndGetResult()
  38. }
  39. // Finish finishes the scanner with an error.
  40. func (s *ScannerHelper) Finish(err error) {
  41. s.notifier.Finish(err)
  42. }