Cancelreader - A cancelable reader for Go

Overview

CancelReader

Latest Release Go Doc Software License Build Status Go ReportCard

A cancelable reader for Go

This package is based on the fantastic work of Erik Geiser in Charm's Bubble Tea framework.

Usage

NewReader returns a reader with a Cancel function. If the input reader is an *os.File, the cancel function can be used to interrupt a blocking Read call. In this case, the cancel function returns true if the call was canceled successfully. If the input reader is not an *os.File, the cancel function does nothing and always returns false.

r, err := cancelreader.NewReader(file)
if err != nil {
    // handle error
    ...
}

// cancel after five seconds
go func() {
    time.Sleep(5 * time.Second)
    r.Cancel()
}()

// keep reading
for {
    var buf [1024]byte
    _, err := r.Read(buf[:])

    if errors.Is(err, cancelreader.ErrCanceled) {
        fmt.Println("canceled!")
        break
    }
    if err != nil {
        // handle other errors
        ...
    }

    // handle data
    ...
}

Implementations

  • The Linux implementation is based on the epoll mechanism
  • The BSD and macOS implementation is based on the kqueue mechanism
  • The generic Unix implementation is based on the posix select syscall

Caution

The Windows implementation is based on WaitForMultipleObject with overlapping reads from CONIN$. At this point it only supports canceling reads from os.Stdin.

You might also like...
csv reader/writer.

IO csv reader sample version 0.0.1-SNAPSHOT Goals: main: read huge file, hex substring, write to new file. cmd/v2 version can read and write use ony o

csv reader/writer and csv generator.

IO csv reader sample version 0.0.1-SNAPSHOT Goals: main: read huge file, hex substring, write to new file. repo has 2 version. v1 can read a file and

terminal rss reader

TODO Add author field [x] Add Source as own type to support both http sources and file types sources [x] Move Load http call to own method that Load m

yarr (yet another rss reader) is a web-based feed aggregator
yarr (yet another rss reader) is a web-based feed aggregator

yarr (yet another rss reader) is a web-based feed aggregator which can be used both as a desktop application and a personal self-hosted server.

osu! database file format flexible reader

Example usage package main import ( "fmt" "github.com/l3lackShark/reader" types "github.com/l3lackShark/reader/types" ) type agent struct { Osu

Named csv reader for go

Named csv reader Package named csv reader provides methods to easly read csv files and parse columsn to basic types. This package is not a CSV parser,

A bit reader tool written in golang

A bit reader tool written in golang

Go MBtiles reader

MBTiles Reader for Go A simple Go-based mbtiles reader. Supports JPG, PNG, WebP, and vector tile tilesets created according to the mbtiles specificati

Wrap byte read options with uniform interface for io.Reader and byte slice

nibbler Nibble chunks from Reader streams and slice in a common way Overview This is a golang module that provides an interface for treating a Reader

Simple binary reader and writer
Simple binary reader and writer

Simple Binary Stream Reader/Writer This package contains a set of simple utility reader and writer that can be used to efficiently read/write binary i

TXQR (Animated QR data transfer) demo reader app for iOS
TXQR (Animated QR data transfer) demo reader app for iOS

TXQR Reader for iOS txqr-reader is a demo app in Swift that reads animated QR code and shows file. Currently it expects file to be an JPEG image. Just

Multi producer and multi-reader lockless ring buffer in go using generics from the go 1.18beta release

LocklessGenericRingBuffer This is an implementation of a multi producer, multi r

HLF smart-bft WAL files reader

WAL Reader Utility for reading Hyperledger Fabric SmartBFT WAL files. Install go install gitlab.n-t.io/atmz/[email protected] Read WAL file walreader

Go-sql-reader - Go utility to read the externalised sql with predefined tags

go-sql-reader go utility to read the externalised sql with predefined tags Usage

Vtterm - An raw-mode vt100 screen reader

#VT100 TERMINAL This is a vt100 screen reader ( clone of jaguilar/v100 ) and inc

Perform external sorting on io.Reader

external-sort This repo describes an algorithm to sort data from a stream with a fixed amount of memory necessary that can be specified. The overall i

A cli HackerNews reader, using HackerNews API.

HackerReader STILL WIP A cli HackerNews reader, using the HackerNews API. I mainly created this as a way to play around with Bubble Tea, Lip Gloss, an

Hntoebook - Converts the best HN stories to .mobi format to be read using an e-reader
Hntoebook - Converts the best HN stories to .mobi format to be read using an e-reader

HN to E-Book What? This program converts the best HN stories to .mobi format to

A Hacker News reader focused on content and readability.

HN.HSBLHSN.ME A HackerNews reader written in Go and React. It focuses on content and readability. Demo You can check the final result here at https://

Comments
  • Fix support for redirected stdin on Windows

    Fix support for redirected stdin on Windows

    Fixes #7.

    I updated the README here, and added a check to NewReader to ensure the supplied reader is a console.

    I also created a unit test that runs NewReader in a child process with a redirected stdin. It should work on both unix and Windows platforms. Without this fix, the test will fail on Windows.

    opened by craig65535 3
  • Redirected stdin not handled properly on Windows

    Redirected stdin not handled properly on Windows

    On Windows, NewReader(os.Stdin) will replace stdin with a handle to the console, even when stdin was previously redirected from a pipe.

    This can be fixed by returning newFallbackCancelReader(reader) when stdin is not a console.

    opened by craig65535 0
Releases(v0.2.2)
  • v0.2.2(Jun 22, 2022)

    What's Changed

    • Don't leak epoll/kqueue fd on error by @tklauser in https://github.com/muesli/cancelreader/pull/10

    New Contributors

    • @tklauser made their first contribution in https://github.com/muesli/cancelreader/pull/10

    Full Changelog: https://github.com/muesli/cancelreader/compare/v0.2.1...v0.2.2

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Jun 21, 2022)

    What's Changed

    • fix: handle cancels mid read in the fallback by @caarlos0 in https://github.com/muesli/cancelreader/pull/9

    New Contributors

    • @caarlos0 made their first contribution in https://github.com/muesli/cancelreader/pull/9

    Full Changelog: https://github.com/muesli/cancelreader/compare/v0.2.0...v0.2.1

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Apr 5, 2022)

    What's Changed

    • Add test for cancelreader by @hinshun in https://github.com/muesli/cancelreader/pull/1
    • Use file interface instead of requiring os.File by @hinshun in https://github.com/muesli/cancelreader/pull/2

    New Contributors

    • @hinshun made their first contribution in https://github.com/muesli/cancelreader/pull/1

    Full Changelog: https://github.com/muesli/cancelreader/compare/v0.1.0...v0.2.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Feb 6, 2022)

Owner
Christian Muehlhaeuser
Geek, Gopher, Software Developer, Maker, Opensource Advocate, Tech Enthusiast, Photographer, Board and Card Gamer
Christian Muehlhaeuser
A simple and light excel file reader to read a standard excel as a table faster | 一个轻量级的Excel数据读取库,用一种更`关系数据库`的方式解析Excel。

Intro | 简介 Expect to create a reader library to read relate-db-like excel easily. Just like read a config. This library can read all xlsx file correct

Back Yu 167 Dec 19, 2022
Optimized bit-level Reader and Writer for Go.

bitio Package bitio provides an optimized bit-level Reader and Writer for Go. You can use Reader.ReadBits() to read arbitrary number of bits from an i

András Belicza 205 Dec 1, 2022
Load environment variables from `.env` or `io.Reader` in Go.

gotenv Load environment variables from .env or io.Reader in Go. Usage Put the gotenv package on your import statement: import "github.com/subosito/got

Alif Rachmawadi 243 Dec 30, 2022
yet another rss reader

yarr yarr (yet another rss reader) is a web-based feed aggregator which can be used both as a desktop application and a personal self-hosted server. I

null 1.5k Dec 29, 2022
Pure Go SQLite file reader

Package SQLittle provides pure Go, read-only, access to SQLite (version 3) database files. What SQLittle reads SQLite3 tables and indexes. It iterates

Harmen 186 Oct 12, 2022
Golang io.Reader and io.Writer but with limits

LimitIO io.Reader and io.Writer with limit.

LI Zhennan 72 Dec 14, 2022
Glow is a terminal based markdown reader designed from the ground up to bring out the beauty—and power—of the CLI.💅🏻

Glow Render markdown on the CLI, with pizzazz! What is it? Glow is a terminal based markdown reader designed from the ground up to bring out the beaut

Charm 11.3k Dec 30, 2022
Minimalist and opinionated feed reader

Miniflux 2 Miniflux is a minimalist and opinionated feed reader: Written in Go (Golang) Works only with Postgresql Doesn't use any ORM Doesn't use any

Miniflux 4.4k Jan 3, 2023
Use Google Cloud KMS as an io.Reader and rand.Source.

Google Cloud KMS Go io.Reader and rand.Source This package provides a struct that implements Go's io.Reader and math/rand.Source interfaces, using Goo

Seth Vargo 5 Dec 1, 2022
go-linereader: A small library for streaming lines from an io.Reader.

go-linereader: A small library for streaming lines from an io.Reader.

Conner Douglass 1 Oct 25, 2021