cores

package
v1.1.33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 24 Imported by: 2

README

go-puzzles/cores

简单易用、足够轻量、性能好的 Golang 服务、任务的管理、监控核心

Easy to use, light enough, good performance Golang worker or service manager and monitor core library

特性

简单易用、足够轻量,避免过多的外部依赖

目前实现了以下特性:

  • 任务管理
  • 定时任务
  • 守护进程任务
  • 优雅终止
  • 服务发现
  • 服务注册

支持各种外部扩展:

  • httpServer
  • grpcServer
  • gprcuiHandler

快速上手

安装
go get github.com/go-puzzles/puzzles/cores
Worker
package main

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

	"github.com/go-puzzles/puzzles/cores"
)


func main() {
	core := cores.NewPuzzleCore(
		cores.WithWorker(func(ctx context.Context) error {
			t := time.NewTicker(time.Second * 3)
			for {
				select {
				case <-ctx.Done():
					return ctx.Err()
				case <-t.C:
				}

				fmt.Println("hello world")
			}
		}),
	)

	cores.Run(core)
}
DaemonWorker

DaemonWorker 若返回了错误,则整个服务都将停止, 但是 cores.WithWorker 则不会

package main

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

	"github.com/go-puzzles/puzzles/cores"
)


func main() {
	core := cores.NewPuzzleCore(
		cores.WithDaemonWorker(func(ctx context.Context) error {
			t := time.NewTicker(time.Second * 3)
			for {
				select {
				case <-ctx.Done():
					return ctx.Err()
				case <-t.C:
				}

				fmt.Println("hello world")
			}
		}),
	)

	cores.Run(core)
}

CronWorker

package main

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

	"github.com/go-puzzles/puzzles/cores"
)


func main() {
	core := cores.NewPuzzleCore(
		cores.WithCronWorker("0 */1 * * *", func(ctx context.Context) error {
		    fmt.Println("hello world")	
		}),
	)

	cores.Run(core)
}
Http服务
package main

import (
	"net/http"
	
	"github.com/go-puzzles/puzzles/cores"
	httppuzzle "github.com/go-puzzles/puzzles/cores/puzzles/http-puzzle"
	"github.com/go-puzzles/puzzles/plog"
	"github.com/gorilla/mux"
)

func main() {
	pflags.Parse()
	router := mux.NewRouter()
	
	router.Path("/hello").Methods(http.MethodGet).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})
	
	core := cores.NewPuzzleCore(
		httppuzzle.WithCoreHttpPuzzle("/api", router),
	)
	
    if err := cores.Start(core, port()); err != nil {
        panic(err)
    }
}
Grpc服务
package main

import (
	"github.com/go-puzzles/puzzles/cores"
	grpcpuzzle "github.com/go-puzzles/puzzles/cores/puzzles/grpc-puzzle"
	grpcuipuzzle "github.com/go-puzzles/puzzles/cores/puzzles/grpcui-puzzle"
	"github.com/go-puzzles/puzzles/example/cores-with-grpc/examplepb"
	srv "github.com/go-puzzles/puzzles/example/cores-with-grpc/service"
	"github.com/go-puzzles/puzzles/example/cores-with-grpc/testpb"
	"google.golang.org/grpc"
)

func main() {
	example := srv.NewExampleService()
	test := srv.NewTestService()

	srv := cores.NewPuzzleCore(	
		grpcpuzzle.WithCoreGrpcPuzzle(func(srv *grpc.Server) {
			examplepb.RegisterExampleHelloServiceServer(srv, example)
			testpb.RegisterExampleHelloServiceServer(srv, test)
		}),
	)

	if err := cores.Start(srv, 0); err != nil {
		panic(err)
	}
}
开启GRPCUI
srv := cores.NewPuzzleCore(	
    grpcuipuzzle.WithCoreGrpcUI(),
	grpcpuzzle.WithCoreGrpcPuzzle(func(srv *grpc.Server) {
		examplepb.RegisterExampleHelloServiceServer(srv, example)
		testpb.RegisterExampleHelloServiceServer(srv, test)
	}),
)
Consul服务注册
package main

import (
	"github.com/go-puzzles/puzzles/cores"
	consulpuzzle "github.com/go-puzzles/puzzles/cores/puzzles/consul-puzzle"
)

func main() {
	pflags.Parse(
		pflags.WithConsulEnable(),
	)

	core := cores.NewPuzzleCore(
		cores.WithService(pflags.GetServiceName()),
		consulpuzzle.WithConsulRegsiter(),
	)

	cores.Start(core, port())
}
Consul服务发现
package main

import (
	"github.com/go-puzzles/puzzles/cores/discover"
)


func main() {
    // ....
    discover.GetServiceFinder().GetAddress("serviceName")
    discover.GetServiceFinder().GetAddressWithTag("serviceName", "v.0.0")
    // ....
}

go-puzzles其他工具集

plog日志工具

pflags flags工具

pgorm数据库orm工具

更多请详见: go-puzzles

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTP1FastMatchWriter added in v1.1.31

func HTTP1FastMatchWriter() cmux.MatchWriter

func HTTP2MatchWithHeaderExclude added in v1.1.31

func HTTP2MatchWithHeaderExclude(name, value string) cmux.MatchWriter

func HTTP2MatchWriter added in v1.1.31

func HTTP2MatchWriter() cmux.MatchWriter

func HttpPrefixMatcher added in v1.0.18

func HttpPrefixMatcher(prefix string) cmux.Matcher

HttpPrefixMatcher returns a cmux.MatcherFunc that matches based on HTTP path prefix.

func Run

func Run(srv *CoreService) error

func Start

func Start[T listenEntry](srv *CoreService, addr T) error

func StructName

func StructName(i any) string

Types

type CoreService

type CoreService struct {
	// contains filtered or unexported fields
}

func NewPuzzleCore

func NewPuzzleCore(opts ...ServiceOption) *CoreService

func (*CoreService) GracefulStopPuzzle

func (c *CoreService) GracefulStopPuzzle()

type Options

type Options struct {
	Ctx          context.Context
	ListenerAddr string
	ServiceName  string
	Tags         []string
	WaitPprof    chan struct{}

	GrpcListener listenerGetter
	HttpListener listenerGetter

	HttpMux     *http.ServeMux
	HttpHandler http.Handler
	// contains filtered or unexported fields
}

func (*Options) RegisterPuzzle

func (o *Options) RegisterPuzzle(p Puzzle)

RegisterPuzzle will put Puzzle into cores service It replaces the previously registered Puzzle with the same name

type Puzzle

type Puzzle interface {
	Name() string
	Before(*Options) error
	StartPuzzle(context.Context, *Options) error
	Stop() error
}

type ServiceOption

type ServiceOption func(*Options)

func WithCronWorker

func WithCronWorker(cron string, fn WorkerFunc) ServiceOption

func WithDaemonNameWorker added in v1.0.14

func WithDaemonNameWorker(name string, fn WorkerFunc) ServiceOption

func WithDaemonWorker

func WithDaemonWorker(fn WorkerFunc) ServiceOption

func WithNameWorker

func WithNameWorker(name string, fn WorkerFunc) ServiceOption

func WithService

func WithService(name string) ServiceOption

func WithTag

func WithTag(tag string) ServiceOption

func WithWorker

func WithWorker(fn WorkerFunc) ServiceOption

type Worker

type Worker interface {
	Name() string
	Fn(ctx context.Context) error
}

type WorkerFunc

type WorkerFunc func(ctx context.Context) error

Directories

Path Synopsis
puzzles

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL