Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from im-kulikov/prepare-new-release
Browse files Browse the repository at this point in the history
Prepare new release v0.13.0
  • Loading branch information
im-kulikov authored Nov 27, 2019
2 parents 2c0b51e + 10d9621 commit 8eb7c60
Show file tree
Hide file tree
Showing 8 changed files with 695 additions and 83 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.11

require (
bou.ke/monkey v1.0.1
github.com/chapsuk/mserv v1.1.0
github.com/chapsuk/worker v1.0.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0
Expand All @@ -13,6 +12,8 @@ require (
go.uber.org/atomic v1.4.0
go.uber.org/dig v1.7.0
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980
google.golang.org/grpc v1.21.0
)

// Blocked in Russia
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bouk/monkey v1.0.1 h1:82kWEtyEjyfkRZb0DaQ5+7O5dJfe3GzF/o97+yUo5d0=
github.com/bouk/monkey v1.0.1/go.mod h1:PG/63f4XEUlVyW1ttIeOJmJhhe1+t9EC/je3eTjvFhE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chapsuk/mserv v1.1.0 h1:NlGeyiEDc8VhLfZQgAFQVLhjquH+gr6frjSiq9bqFLA=
github.com/chapsuk/mserv v1.1.0/go.mod h1:0U/EEhGItAHX8XOSegQayMKA/nZqGZX3/8yo76rMr1E=
github.com/chapsuk/wait v0.3.1 h1:jjdGq+0cpfOjNgyH1ueJLwRve9wAyvlejDOUewR1xP8=
github.com/chapsuk/wait v0.3.1/go.mod h1:NC0xxH0aNuAwaM/KZ/cB188PLbpHG60Dm7yNM71uDH4=
github.com/chapsuk/worker v1.0.0 h1:gygj7laai2B8l6qTChif6WFb2TEycS3qOt2mR7N8cu4=
Expand All @@ -38,6 +36,7 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand All @@ -55,10 +54,6 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -162,6 +157,7 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -182,8 +178,10 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0 h1:G+97AoqBnmZIT91cLG/EkCoK9NSelj64P8bOHHNmGn0=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
129 changes: 129 additions & 0 deletions web/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package web

import (
"fmt"
"net"
"time"

"github.com/im-kulikov/helium/internal"
"google.golang.org/grpc"
)

type (
gRPC struct {
skipErrors bool
address string
network string
server *grpc.Server
shutdownTimeout time.Duration
}

// GRPCOption allows changing default gRPC
// service settings.
GRPCOption func(g *gRPC)
)

const (
// ErrEmptyGRPC is raised when called NewGRPCService
// or gRPC methods with empty grpc.Server.
ErrEmptyGRPC = internal.Error("empty gRPC server")

// ErrEmptyGRPCAddress is raised when passed empty address to NewGRPCService.
ErrEmptyGRPCAddress = internal.Error("empty gRPC address")
)

// GRPCSkipErrors allows to skip any errors
func GRPCSkipErrors() GRPCOption {
return func(g *gRPC) {
g.skipErrors = true
}
}

// GRPCListenAddress allows to change network for net.Listener.
func GRPCListenAddress(addr string) GRPCOption {
return func(g *gRPC) {
g.address = addr
}
}

// GRPCListenNetwork allows to change default (tcp)
// network for net.Listener.
func GRPCListenNetwork(network string) GRPCOption {
return func(g *gRPC) {
g.network = network
}
}

// GRPCShutdownTimeout changes default shutdown timeout.
func GRPCShutdownTimeout(v time.Duration) GRPCOption {
return func(g *gRPC) {
g.shutdownTimeout = v
}
}

// NewGRPCService creates gRPC service with passed gRPC options.
// If something went wrong it returns an error.
func NewGRPCService(serve *grpc.Server, opts ...GRPCOption) (Service, error) {
if serve == nil {
return nil, ErrEmptyGRPC
}

s := &gRPC{
server: serve,
network: "tcp",
skipErrors: false,
shutdownTimeout: time.Second * 30,
}

for i := range opts {
opts[i](s)
}

if s.address == "" {
return nil, ErrEmptyGRPCAddress
}

return s, nil
}

// Start tries to start gRPC service.
// If something went wrong it returns an error.
// If could not start server panics.
func (g *gRPC) Start() error {
var (
err error
lis net.Listener
)

if g.server == nil {
return g.catch(ErrEmptyHTTPServer)
} else if lis, err = net.Listen(g.network, g.address); err != nil {
return g.catch(err)
}

go func() {
if err := g.catch(g.server.Serve(lis)); err != nil {
panic(fmt.Sprintf("could not start grpc.Server: %v", err))
}
}()

return nil
}

// Stop tries to stop gRPC service.
func (g *gRPC) Stop() error {
if g.server == nil {
return g.catch(ErrEmptyHTTPServer)
}

g.server.GracefulStop()
return nil
}

func (g *gRPC) catch(err error) error {
if g.skipErrors || err == grpc.ErrServerStopped {
return nil
}

return err
}
144 changes: 144 additions & 0 deletions web/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package web

import (
"context"
"fmt"
"net"
"net/http"
"time"

"github.com/im-kulikov/helium/internal"
)

type (
httpService struct {
skipErrors bool
address string
network string
server *http.Server
shutdownTimeout time.Duration
}

// HTTPOption interface that allows
// to change default http-service options.
HTTPOption func(s *httpService)
)

const (
// ErrEmptyHTTPServer is raised when called New
// or httpService methods with empty http.Server.
ErrEmptyHTTPServer = internal.Error("empty http server")

// ErrEmptyHTTPAddress is raised when passed empty address to NewHTTPService.
ErrEmptyHTTPAddress = internal.Error("empty http address")
)

// HTTPShutdownTimeout changes default shutdown timeout.
func HTTPShutdownTimeout(v time.Duration) HTTPOption {
return func(s *httpService) {
s.shutdownTimeout = v
}
}

// HTTPListenNetwork allows to change default (tcp)
// network for net.Listener.
func HTTPListenNetwork(network string) HTTPOption {
return func(s *httpService) {
s.network = network
}
}

// HTTPListenAddress allows to change network for net.Listener.
// By default it takes address from http.Server.
func HTTPListenAddress(address string) HTTPOption {
return func(s *httpService) {
s.address = address
}
}

// HTTPSkipErrors allows to skip any errors
func HTTPSkipErrors() HTTPOption {
return func(s *httpService) {
s.skipErrors = true
}
}

// NewHTTPService creates Service from http.Server and HTTPOption's.
func NewHTTPService(serve *http.Server, opts ...HTTPOption) (Service, error) {
if serve == nil {
return nil, ErrEmptyHTTPServer
}

s := &httpService{
skipErrors: false,
server: serve,
network: "tcp",
address: serve.Addr,
shutdownTimeout: time.Second * 30,
}

for i := range opts {
opts[i](s)
}

if s.address == "" {
return nil, ErrEmptyHTTPAddress
}

return s, nil
}

// Start runs http.Server and returns error
// if something went wrong.
func (s *httpService) Start() error {
var (
err error
lis net.Listener
)

if s.server == nil {
return s.catch(ErrEmptyHTTPServer)
} else if lis, err = net.Listen(s.network, s.address); err != nil {
return s.catch(err)
}

go func() {
var err error

switch {
case s.server.TLSConfig == nil:
err = s.server.Serve(lis)
default:
// provide cert and key from TLSConfig
err = s.server.ServeTLS(lis, "", "")
}

// ignores known error
if err = s.catch(err); err != nil {
panic(fmt.Sprintf("could not start http.Server: %v", err))
}
}()

return nil
}

// Stop tries to stop http.Server and returns error
// if something went wrong.
func (s *httpService) Stop() error {
if s.server == nil {
return s.catch(ErrEmptyHTTPServer)
}

ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer cancel()

return s.catch(s.server.Shutdown(ctx))
}

func (s *httpService) catch(err error) error {
if s.skipErrors || err == http.ErrServerClosed {
return nil
}

return err
}
Loading

0 comments on commit 8eb7c60

Please sign in to comment.