Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quit event #1523

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ import (
type Resolver struct {
cfg *config.Instance
cache *cache.Cache
done chan bool
}

// var _ genserver.ResolverRoot = &Resolver{} // Must implement ResolverRoot

func New(cfg *config.Instance) *Resolver {
func New(cfg *config.Instance, done chan bool) *Resolver {
return &Resolver{
cfg,
cache.New(12*time.Hour, time.Hour),
done,
}
}

// Seems gqlgen supplies this so you can separate your resolver and query resolver logic
// So far no need for this, so we're pointing back at ourselves..
func (r *Resolver) Query() genserver.QueryResolver { return r }

func (r *Resolver) Subscription() genserver.SubscriptionResolver { return r }

func (r *Resolver) Version(ctx context.Context) (*graph.Version, error) {
logging.Debug("Version resolver")
return &graph.Version{
Expand Down Expand Up @@ -129,3 +133,8 @@ func (r *Resolver) Projects(ctx context.Context) ([]*graph.Project, error) {

return projects, nil
}

func (r *Resolver) Quit(ctx context.Context) (<-chan bool, error) {
logging.Debug("Quit resolver")
return r.done, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you are returning the r.done channel which is the same for every subscriber to this quit event, at most one of the subscribers would receive the true value. But probably none of them (see other comment). All of them would wake up, which we could argue is enough. In that case I would change the return channel to <-chan struct{} though.

If you actually want to return a true event on the channel, I think something like this would work here:

Suggested change
return r.done, nil
res := make (chan bool)
go func() {
<-r.done
res <- true
}()
return res, nil

}
102 changes: 102 additions & 0 deletions cmd/state-svc/internal/server/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions cmd/state-svc/internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ func (s *Server) setupRouting() {
})

s.httpServer.GET("/", func(c echo.Context) error {
playground.Handler("GraphQL", "/query").ServeHTTP(c.Response(), c.Request())
playground.Handler("GraphQL", "/").ServeHTTP(c.Response(), c.Request())
return nil
})

s.httpServer.GET("/subscriptions", func(c echo.Context) error {
s.graphServer.ServeHTTP(c.Response(), c.Request())
return nil
})

s.httpServer.GET(QuitRoute, func(c echo.Context) error {
s.shutdown()
s.quit()
return nil
})
}
32 changes: 25 additions & 7 deletions cmd/state-svc/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package server
import (
"context"
"net"
"net/http"
"strconv"
"time"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

Expand All @@ -21,21 +23,23 @@ import (
)

type Server struct {
shutdown context.CancelFunc
cancel context.CancelFunc
done chan bool
graphServer *handler.Server
listener net.Listener
httpServer *echo.Echo
port int
}

func New(cfg *config.Instance, shutdown context.CancelFunc) (*Server, error) {
func New(cfg *config.Instance, cancel context.CancelFunc) (*Server, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, errs.Wrap(err, "Failed to listen")
}

s := &Server{shutdown: shutdown}
s.graphServer = newGraphServer(cfg)
s := &Server{cancel: cancel}
s.done = make(chan bool, 1)
s.graphServer = newGraphServer(cfg, s.done)
s.listener = listener
s.httpServer = newHTTPServer(listener)

Expand All @@ -61,6 +65,12 @@ func (s *Server) Start() error {
return s.httpServer.Start(s.listener.Addr().String())
}

func (s *Server) quit() {
s.done <- true
close(s.done)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look quite right. As the done channel is non-blocking, it wouldn't wait for the reader to consume the true event, and just immediately close the channel. That of course, indicates the quite event, by waking up the channel, but most likely the client will not retrieve the true.

I think that is okay though. And we should just remove line 69 (and the buffer size of 1 on the channel).

s.cancel()
}

func (s *Server) Shutdown() error {
logging.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand All @@ -72,9 +82,17 @@ func (s *Server) Shutdown() error {
return nil
}

func newGraphServer(cfg *config.Instance) *handler.Server {
graphServer := handler.NewDefaultServer(genserver.NewExecutableSchema(genserver.Config{Resolvers: resolver.New(cfg)}))
graphServer.AddTransport(&transport.Websocket{})
func newGraphServer(cfg *config.Instance, done chan bool) *handler.Server {
graphServer := handler.NewDefaultServer(genserver.NewExecutableSchema(genserver.Config{Resolvers: resolver.New(cfg, done)}))
graphServer.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// For development. User proper CORS for prod
return true
},
},
KeepAlivePingInterval: 10 * time.Second,
})
graphServer.SetQueryCache(lru.New(1000))
graphServer.Use(extension.Introspection{})
graphServer.Use(extension.AutomaticPersistedQuery{
Expand Down
4 changes: 4 additions & 0 deletions cmd/state-svc/schema/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ type Query {
update(channel: String, version: String): DeferredUpdate
projects: [Project]!
}

type Subscription {
quit: Boolean!
}
33 changes: 31 additions & 2 deletions cmd/state-tray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ func run() (rerr error) {

systray.AddSeparator()

mQuit := systray.AddMenuItem(locale.Tl("tray_exit", "Exit"), "")
quit, err := setupQuit(model)
if err != nil {
return errs.Wrap(err, "Could not setup quit channel")
}

for {
select {
Expand Down Expand Up @@ -242,13 +245,39 @@ func run() (rerr error) {
if err := execute(updlgInfo.Exec(), nil); err != nil {
return errs.New("Could not execute: %s", updlgInfo.Name())
}
case <-mQuit.ClickedCh:
case <-quit:
logging.Debug("Quit event")
model.CloseSubscriptions()
systray.Quit()
}
}
}

func setupQuit(model *model.SvcModel) (chan struct{}, error) {
mQuit := systray.AddMenuItem(locale.Tl("tray_exit", "Exit"), "")
quitSub, err := model.Quit(context.Background())
if err != nil {
return nil, errs.Wrap(err, "Could not subscribe so service quit event")
}

quit := make(chan struct{})

go func() {
for {
select {
case <-mQuit.ClickedCh:
logging.Debug("Quit clicked")
quit <- struct{}{}
case <-quitSub:
logging.Debug("Quit from subscription")
quit <- struct{}{}
}
}
}()

return quit, nil
}

func onExit() {
logging.Debug("systray.OnExit() was called.")
cfg, err := config.New()
Expand Down
2 changes: 1 addition & 1 deletion cmd/state-tray/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

func superviseUpdate(mdl *model.SvcModel, notice *updateNotice) func() {
var done chan struct{}
done := make(chan struct{})

go func() {
for {
Expand Down
Loading