first commit
This commit is contained in:
commit
3306e3b5ce
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 William Dillon
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# package channels
|
||||||
|
package channels provides some helper functions for working with channels, including ways to convert them into slices. none of the functions are blocking - if a context is not provided, context.Background() is used.
|
136
channels.go
Normal file
136
channels.go
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package channels
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrChannelClosed = errors.New("channel is closed (returned !ok)")
|
||||||
|
)
|
||||||
|
|
||||||
|
type ChannelPipelineWorkFunction[T any] func(T) T
|
||||||
|
|
||||||
|
func ProcessChannelThroughFunction[T any](ctx context.Context, c <-chan T, workCallback ChannelPipelineWorkFunction[T]) <-chan T {
|
||||||
|
results := make(chan T, cap(c))
|
||||||
|
go func() {
|
||||||
|
defer close(results)
|
||||||
|
for {
|
||||||
|
if t, err := TrySelectFromChannel(ctx, c); err != nil {
|
||||||
|
return
|
||||||
|
} else if err := TryAddToChannel(ctx, results, workCallback(t)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// could be problematic in certain situations because it launches one goroutine per incoming channel.
|
||||||
|
// this shouldn't typically be a problem with a small number of channels, or when the channels producers
|
||||||
|
// aren't very busy, but it's worth noting.
|
||||||
|
func MergeChannelsWithContext[T any](ctx context.Context, bufferSize int, channels ...<-chan T) <-chan T {
|
||||||
|
results := make(chan T, bufferSize)
|
||||||
|
go func() {
|
||||||
|
defer close(results)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for _, c := range channels {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for {
|
||||||
|
if t, err := TrySelectFromChannel(ctx, c); err != nil {
|
||||||
|
return
|
||||||
|
} else if err := TryAddToChannel(ctx, results, t); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}()
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func MergeChannels[T any](bufferSize int, channels ...<-chan T) <-chan T {
|
||||||
|
return MergeChannelsWithContext(context.Background(), bufferSize, channels...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TryAddToChannel[T any](ctx context.Context, c chan<- T, t T) error {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return context.DeadlineExceeded
|
||||||
|
case c <- t:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TrySelectFromChannel[T any](ctx context.Context, c <-chan T) (T, error) {
|
||||||
|
var t T
|
||||||
|
var ok bool
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return t, context.DeadlineExceeded
|
||||||
|
case t, ok = <-c:
|
||||||
|
if !ok {
|
||||||
|
return t, ErrChannelClosed
|
||||||
|
} else {
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChannelToSliceWithContext[T any](ctx context.Context, c <-chan T) []T {
|
||||||
|
results := make([]T, 0, cap(c))
|
||||||
|
loop:
|
||||||
|
for {
|
||||||
|
if t, err := TrySelectFromChannel(ctx, c); err != nil {
|
||||||
|
break loop
|
||||||
|
} else {
|
||||||
|
results = append(results, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChannelToSlice[T any](c <-chan T) []T {
|
||||||
|
return ChannelToSliceWithContext(context.Background(), c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SliceToChannelWithContext[T any](ctx context.Context, s []T, bufferSize int) <-chan T {
|
||||||
|
results := make(chan T, bufferSize)
|
||||||
|
go func() {
|
||||||
|
defer close(results)
|
||||||
|
for _, t := range s {
|
||||||
|
if err := TryAddToChannel(ctx, results, t); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func SliceToChannel[T any](s []T, bufferSize int) <-chan T {
|
||||||
|
return SliceToChannelWithContext(context.Background(), s, bufferSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CopyChannelWithContext[T any](ctx context.Context, c <-chan T) (a, b <-chan T) {
|
||||||
|
left, right := make(chan T, cap(c)), make(chan T, cap(c))
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
close(left)
|
||||||
|
close(right)
|
||||||
|
}()
|
||||||
|
for {
|
||||||
|
if t, err := TrySelectFromChannel(ctx, c); err != nil {
|
||||||
|
return
|
||||||
|
} else if err := TryAddToChannel(ctx, left, t); err != nil {
|
||||||
|
return
|
||||||
|
} else if err := TryAddToChannel(ctx, right, t); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return left, right
|
||||||
|
}
|
99
channels_test.go
Normal file
99
channels_test.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package channels
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.wmdillon.com/GoApi/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TheWordsFilename = "words"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
TheWords = func() []string {
|
||||||
|
f, err := os.Open(TheWordsFilename)
|
||||||
|
if err != nil {
|
||||||
|
panic("error opening " + TheWordsFilename + ": " + err.Error())
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
set := set.New[string](false)
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
if line := strings.ToLower(strings.TrimSpace(scanner.Text())); len(line) > 0 {
|
||||||
|
set.Insert(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return set.ToSlice()
|
||||||
|
}()
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSliceToChannelAndChannelToSlice(t *testing.T) {
|
||||||
|
wordsChannel := SliceToChannel(TheWords, len(TheWords))
|
||||||
|
wordsSlice := ChannelToSlice(wordsChannel)
|
||||||
|
if !slices.Equal(wordsSlice, TheWords) {
|
||||||
|
t.Fatalf("error: words != words slice\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ReverseString ChannelPipelineWorkFunction[string] = func(s string) string {
|
||||||
|
r := []rune(s)
|
||||||
|
slices.Reverse(r)
|
||||||
|
return string(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessChannelThroughFunction(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
f1 := func(s string) string {
|
||||||
|
results := strings.ToUpper(s)
|
||||||
|
//fmt.Printf("f1: %s => %s\n", s, results)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
f2 := func(s string) string {
|
||||||
|
results := ReverseString(s)
|
||||||
|
//fmt.Printf("f2: %s => %s\n", s, results)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
words, words_copy := CopyChannelWithContext(ctx, SliceToChannel(TheWords, len(TheWords)))
|
||||||
|
modified := ProcessChannelThroughFunction(ctx, ProcessChannelThroughFunction(ctx, words, f1), f2)
|
||||||
|
for {
|
||||||
|
want, wantErr := TrySelectFromChannel(ctx, words_copy)
|
||||||
|
got, gotErr := TrySelectFromChannel(ctx, modified)
|
||||||
|
if wantErr != nil || gotErr != nil {
|
||||||
|
if !errors.Is(gotErr, wantErr) {
|
||||||
|
t.Fatalf("error: wanted %v; got %v\n", wantErr, gotErr)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
want = f2(f1(want))
|
||||||
|
if want != got {
|
||||||
|
t.Fatalf("error: wanted %s; got %s\n", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergeChannels(t *testing.T) {
|
||||||
|
channels := make([]<-chan string, 0, 26)
|
||||||
|
for slice := range slices.Chunk(TheWords, len(TheWords)/25) {
|
||||||
|
channels = append(channels, SliceToChannel(slice, len(slice)))
|
||||||
|
}
|
||||||
|
got := ChannelToSlice(MergeChannels(len(TheWords), channels...))
|
||||||
|
want := TheWords
|
||||||
|
slices.Sort(want)
|
||||||
|
slices.Sort(got)
|
||||||
|
if len(want) != len(got) {
|
||||||
|
t.Fatalf("error: wanted %d words; got %d\n", len(want), len(got))
|
||||||
|
}
|
||||||
|
for i := range len(want) {
|
||||||
|
if want, got := want[i], got[i]; want != got {
|
||||||
|
t.Fatalf("error: wanted %s; got %s\n", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module code.wmdillon.com/GoApi/channels
|
||||||
|
|
||||||
|
go 1.24.3
|
||||||
|
|
||||||
|
require code.wmdillon.com/GoApi/set v0.0.0-20250507164311-92b5c07cfe79 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
code.wmdillon.com/GoApi/set v0.0.0-20250507164311-92b5c07cfe79 h1:wZ8m8hNBcbloOf8eMXBLQx3XVoUscp9eXa0LUK2geOM=
|
||||||
|
code.wmdillon.com/GoApi/set v0.0.0-20250507164311-92b5c07cfe79/go.mod h1:plTVmwcnxECX/pgFB1kWemxqPrWm/K/8wV6TWDgWlLY=
|
Loading…
x
Reference in New Issue
Block a user