video-server icon indicating copy to clipboard operation
video-server copied to clipboard

Allow user to better control threads

Open arwineap opened this issue 3 years ago • 2 comments
trafficstars

I wanted to propose a new blocking method to run the Stream loop.

This blocking method allows us to pull out the go routines into the caller's code so that the caller can control startup, and shutdown for it's own methods. In my case, I wanted to split out the file generator from the api, but once those services were split I needed to be able to be able to wait for the process / be able to shut it down

Really like the abstractions, thank you for the code :)

arwineap avatar Jul 10 '22 14:07 arwineap

Here's an example of it's usage, that allows the caller to configure signals to stop the process


import (
	"context"
	"fmt"
	videoserver "github.com/LdDl/video-server"
	"os"
	"os/signal"
	"providence/routes"
)

func main() {
	conf, err := routes.LoadConfigFromEnv("APP")
	if err != nil {
		panic(err)
	}

	proxyConf, err := videoserver.NewConfiguration(conf.StreamProxyConfigFile)
	if err != nil {
		panic(fmt.Errorf("could not load config for stream proxy: %w", err))
	}

	proxy, err := videoserver.NewApplication(proxyConf)
	if err != nil {
		panic(fmt.Errorf("could not create new application: %w", err))
	}

	ctx := context.Background()
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt, os.Kill)

	for k, _ := range proxy.Streams.Streams {
		go proxy.RunStream(ctx, k)
	}

	<-sigCh
}

arwineap avatar Jul 10 '22 14:07 arwineap

Hi, thank you for PR

func (sm *StreamsStorage) GetStream(id uuid.UUID) (string, []string) {
	sm.Lock()
	defer sm.Unlock()

	return sm.Streams[id].URL, sm.Streams[id].SupportedStreamTypes
}

Isn't this code could lead us to panic is case sm.Streams[id] == nil (not found)? I'd rather return ("", []string{}) in that case (or error?)

LdDl avatar Jul 14 '22 19:07 LdDl