1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00

remove function

This commit is contained in:
asim 2022-07-22 20:12:24 +01:00
parent 678c227061
commit 4d63d61c20
3 changed files with 0 additions and 164 deletions

View File

@ -1,81 +0,0 @@
package micro
import (
"context"
"time"
"go-micro.dev/v4/server"
)
type function struct {
cancel context.CancelFunc
Service
}
func fnHandlerWrapper(f Function) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
defer f.Done()
return h(ctx, req, rsp)
}
}
}
func fnSubWrapper(f Function) server.SubscriberWrapper {
return func(s server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
defer f.Done()
return s(ctx, msg)
}
}
}
func newFunction(opts ...Option) Function {
ctx, cancel := context.WithCancel(context.Background())
// force ttl/interval
fopts := []Option{
RegisterTTL(time.Minute),
RegisterInterval(time.Second * 30),
}
// prepend to opts
fopts = append(fopts, opts...)
// make context the last thing
fopts = append(fopts, Context(ctx))
service := newService(fopts...)
fn := &function{
cancel: cancel,
Service: service,
}
service.Server().Init(
// ensure the service waits for requests to finish
server.Wait(nil),
// wrap handlers and subscribers to finish execution
server.WrapHandler(fnHandlerWrapper(fn)),
server.WrapSubscriber(fnSubWrapper(fn)),
)
return fn
}
func (f *function) Done() error {
f.cancel()
return nil
}
func (f *function) Handle(v interface{}) error {
return f.Service.Server().Handle(
f.Service.Server().NewHandler(v),
)
}
func (f *function) Subscribe(topic string, v interface{}) error {
return f.Service.Server().Subscribe(
f.Service.Server().NewSubscriber(topic, v),
)
}

View File

@ -1,66 +0,0 @@
package micro
import (
"context"
"sync"
"testing"
"go-micro.dev/v4/debug/handler"
proto "go-micro.dev/v4/debug/proto"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/util/test"
)
func TestFunction(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
r := registry.NewMemoryRegistry(registry.Services(test.Data))
// create service
fn := NewFunction(
Registry(r),
Name("test.function"),
AfterStart(func() error {
wg.Done()
return nil
}),
)
fn.Handle(handler.NewHandler(nil))
// we can't test fn.Init as it parses the command line
// fn.Init()
ch := make(chan error, 2)
go func() {
// run service
ch <- fn.Run()
}()
// wait for start
wg.Wait()
// test call debug
req := fn.Client().NewRequest(
"test.function",
"Debug.Health",
new(proto.HealthRequest),
)
rsp := new(proto.HealthResponse)
err := fn.Client().Call(context.TODO(), req, rsp)
if err != nil {
t.Fatal(err)
}
if rsp.Status != "ok" {
t.Fatalf("function response: %s", rsp.Status)
}
if err := <-ch; err != nil {
t.Fatal(err)
}
}

View File

@ -30,18 +30,6 @@ type Service interface {
String() string
}
// Function is a one time executing Service
type Function interface {
// Inherits Service interface
Service
// Done signals to complete execution
Done() error
// Handle registers an RPC handler
Handle(v interface{}) error
// Subscribe registers a subscriber
Subscribe(topic string, v interface{}) error
}
// Event is used to publish messages to a topic
type Event interface {
// Publish publishes a message to the event topic
@ -69,11 +57,6 @@ func NewContext(ctx context.Context, s Service) context.Context {
return context.WithValue(ctx, serviceKey{}, s)
}
// NewFunction returns a new Function for a one time executing Service
func NewFunction(opts ...Option) Function {
return newFunction(opts...)
}
// NewEvent creates a new event publisher
func NewEvent(topic string, c client.Client) Event {
if c == nil {