Skip to content

Commit

Permalink
Merge pull request #23 from ne-sachirou/Serve-receive-no-context
Browse files Browse the repository at this point in the history
[BREAKING] Serve() 函數は Context を受け取らない
  • Loading branch information
ne-sachirou authored Dec 25, 2024
2 parents d25220e + f6a1e3a commit 995e685
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/run-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ jobs:
set -e
go generate ./...
git diff --exit-code
- name: go build
run: |
(cd cmd/example && go build -o main main.go) || exit 1
(cd cmd/example-grpc && go build -o main main.go) || exit 1
(cd cmd/example-http && go build -o main main.go) || exit 1
- name: go test
run: go test ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

cmd/*/main
156 changes: 129 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,39 @@ import (
"github.com/ne-sachirou/go-graceful"
)

type ExampleBlockingServer struct{}
type ExampleBlockingServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleBlockingServer) Serve(ctx context.Context) error {
func (s *ExampleBlockingServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
fmt.Println("example blocking serve")
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example blocking done")
close(s.shutdowned)
return nil
case <-time.After(time.Second):
fmt.Println("example blocking working")
}
}
}

func (s *ExampleBlockingServer) Shutdown(ctx context.Context) error {
return nil
close(s.done)
select {
case <-s.shutdowned:
fmt.Println("example blocking shutdown")
return nil
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
return errors.Join(errors.New("failed to shutdown blocking example"), context.DeadlineExceeded)
}
return context.Canceled
}
}

func main() {
Expand Down Expand Up @@ -158,40 +177,78 @@ import (
)

// Example of main loop in go routine.
type ExampleServer struct{}
type ExampleServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleServer) Serve(ctx context.Context) error {
func (s *ExampleServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
go func() {
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example done")
close(s.shutdowned)
return
case <-time.After(time.Second):
fmt.Println("example working")
}
}
}()
fmt.Println("example serve")
return nil
}

func (s *ExampleServer) Shutdown(ctx context.Context) error {
return nil
close(s.done)
select {
case <-s.shutdowned:
fmt.Println("example shutdown")
return nil
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
return errors.Join(errors.New("failed to shutdown example"), context.DeadlineExceeded)
}
return context.Canceled
}
}

// Example of main loop with caller blocking.
type ExampleBlockingServer struct{}
type ExampleBlockingServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleBlockingServer) Serve(ctx context.Context) error {
func (s *ExampleBlockingServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
fmt.Println("example blocking serve")
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example blocking done")
close(s.shutdowned)
return nil
case <-time.After(time.Second):
fmt.Println("example blocking working")
}
}
}

func (s *ExampleBlockingServer) Shutdown(ctx context.Context) error {
return nil
close(s.done)
select {
case <-s.shutdowned:
fmt.Println("example blocking shutdown")
return nil
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
return errors.Join(errors.New("failed to shutdown blocking example"), context.DeadlineExceeded)
}
return context.Canceled
}
}

func main() {
Expand Down Expand Up @@ -321,20 +378,39 @@ import (
"github.com/ne-sachirou/go-graceful"
)

type ExampleBlockingServer struct{}
type ExampleBlockingServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleBlockingServer) Serve(ctx context.Context) error {
func (s *ExampleBlockingServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
fmt.Println("example blocking serve")
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example blocking done")
close(s.shutdowned)
return nil
case <-time.After(time.Second):
fmt.Println("example blocking working")
}
}
}

func (s *ExampleBlockingServer) Shutdown(ctx context.Context) error {
return nil
close(s.done)
select {
case <-s.shutdowned:
fmt.Println("example blocking shutdown")
return nil
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
return errors.Join(errors.New("failed to shutdown blocking example"), context.DeadlineExceeded)
}
return context.Canceled
}
}

func main() {
Expand All @@ -359,48 +435,74 @@ package main

import (
"context"
"errors"
"fmt"
"time"

"github.com/ne-sachirou/go-graceful"
)

// main loop を go routine の中で回す例。
type ExampleServer struct{}
type ExampleServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleServer) Serve(ctx context.Context) error {
func (s *ExampleServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
go func() {
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example done")
close(s.shutdowned)
return
case <-time.After(time.Second):
fmt.Println("example working")
}
}
}()
fmt.Println("example serve")
return nil
}

func (s *ExampleServer) Shutdown(ctx context.Context) error {
return nil
close(s.done)
select {
case <-s.shutdowned:
fmt.Println("example shutdown")
return nil
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
return errors.Join(errors.New("failed to shutdown example"), context.DeadlineExceeded)
}
return context.Canceled
}
}

// 呼び出し元を blocking して main loop を回す例。
type ExampleBlockingServer struct{}
type ExampleBlockingServer struct {
done chan struct{}
shutdowned chan struct{}
}

func (s *ExampleBlockingServer) Serve(ctx context.Context) error {
func (s *ExampleBlockingServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
fmt.Println("example blocking serve")
for {
select {
case <-ctx.Done():
case <-s.done:
fmt.Println("example blocking done")
close(s.shutdowned)
return nil
case <-time.After(time.Second):
fmt.Println("example blocking working")
}
}
}

func (s *ExampleBlockingServer) Shutdown(ctx context.Context) error {
return nil
}

func main() {
ctx := context.Background()

Expand Down
4 changes: 4 additions & 0 deletions cmd/example-grpc/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eux
go build -o main main.go
./main
4 changes: 4 additions & 0 deletions cmd/example-http/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eux
go build -o main main.go
./main
4 changes: 2 additions & 2 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ExampleServer struct {
shutdowned chan struct{}
}

func (s *ExampleServer) Serve(ctx context.Context) error {
func (s *ExampleServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
go func() {
Expand Down Expand Up @@ -55,7 +55,7 @@ type ExampleBlockingServer struct {
shutdowned chan struct{}
}

func (s *ExampleBlockingServer) Serve(ctx context.Context) error {
func (s *ExampleBlockingServer) Serve() error {
s.done = make(chan struct{})
s.shutdowned = make(chan struct{})
fmt.Println("example blocking serve")
Expand Down
4 changes: 4 additions & 0 deletions cmd/example/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eux
go build -o main main.go
./main
4 changes: 2 additions & 2 deletions graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// Server is the interface that wraps the Serve and Shutdown methods.
type Server interface {
Serve(ctx context.Context) error
Serve() error
Shutdown(ctx context.Context) error
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func (s Servers) Graceful(ctx context.Context, options ...Option) error {

for _, srv := range s.Servers {
go func(ctx context.Context, srv Server) {
if err := srv.Serve(ctx); err != nil {
if err := srv.Serve(); err != nil {
cancel(err)
}
}(ctx, srv)
Expand Down
2 changes: 1 addition & 1 deletion gracefulgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Server struct {
Server *grpc.Server
}

func (s *Server) Serve(ctx context.Context) error {
func (s *Server) Serve() error {
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion gracefulhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Server struct {
Server *http.Server
}

func (s *Server) Serve(ctx context.Context) error {
func (s *Server) Serve() error {
if err := s.Server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
return err
}
Expand Down

0 comments on commit 995e685

Please sign in to comment.