Skip to content

Commit

Permalink
Merge 7ce2839 into eada5ec
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpio committed Jan 11, 2023
2 parents eada5ec + 7ce2839 commit 8d14209
Show file tree
Hide file tree
Showing 6 changed files with 766 additions and 277 deletions.
17 changes: 15 additions & 2 deletions micro/example_handler_test.go
@@ -1,3 +1,16 @@
// Copyright 2022-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package micro_test

import (
Expand Down Expand Up @@ -32,9 +45,9 @@ func ExampleHandler() {
config := micro.Config{
Name: "RectangleAreaService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Endpoint: &micro.EndpointConfig{
Subject: "area.rectangle",
Handler: rec,
Subject: "rectangle.area",
},
}
svc, err := micro.AddService(nc, config)
Expand Down
56 changes: 41 additions & 15 deletions micro/example_package_test.go
Expand Up @@ -30,8 +30,13 @@ func Example() {
}
defer nc.Close()

// service handler - in this case, HandlerFunc is used,
// endpoint handler - in this case, HandlerFunc is used,
// which is a built-in implementation of Handler interface
echoHandler := func(req micro.Request) {
req.Respond(req.Data())
}

// second endpoint
incrementHandler := func(req micro.Request) {
val, err := strconv.Atoi(string(req.Data()))
if err != nil {
Expand All @@ -43,29 +48,50 @@ func Example() {
req.Respond([]byte(strconv.Itoa(responseData)))
}

// third endpoint
multiplyHandler := func(req micro.Request) {
val, err := strconv.Atoi(string(req.Data()))
if err != nil {
req.Error("400", "request data should be a number", nil)
return
}

responseData := val * 2
req.Respond([]byte(strconv.Itoa(responseData)))
}

config := micro.Config{
Name: "IncrementService",
Version: "0.1.0",
Description: "Increment numbers",
Endpoint: micro.Endpoint{
// service handler
Handler: micro.HandlerFunc(incrementHandler),
// a unique subject serving as a service endpoint
Subject: "numbers.increment",

// base handler - for simple services with single endpoints this is sufficient
Endpoint: &micro.EndpointConfig{
Subject: "echo",
Handler: micro.HandlerFunc(echoHandler),
},
}
// Multiple instances of the servcice with the same name can be created.
// Requests to a service with the same name will be load-balanced.
for i := 0; i < 5; i++ {
svc, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}
defer svc.Stop()
svc, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}
defer svc.Stop()

// add a group to aggregate endpoints under common prefix
numbers := svc.AddGroup("numbers")

// register endpoints in a group
err = numbers.AddEndpoint("Increment", micro.HandlerFunc(incrementHandler))
if err != nil {
log.Fatal(err)
}
err = numbers.AddEndpoint("Multiply", micro.HandlerFunc(multiplyHandler))
if err != nil {
log.Fatal(err)
}

// send a request to a service
resp, err := nc.Request("numbers.increment", []byte("3"), 1*time.Second)
resp, err := nc.Request("numbers.Increment", []byte("3"), 1*time.Second)
if err != nil {
log.Fatal(err)
}
Expand Down
126 changes: 98 additions & 28 deletions micro/example_test.go
@@ -1,4 +1,4 @@
// Copyright 2022 The NATS Authors
// Copyright 2022-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -37,11 +37,6 @@ func ExampleAddService() {
Name: "EchoService",
Version: "1.0.0",
Description: "Send back what you receive",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(echoHandler),
},

// DoneHandler can be set to customize behavior on stopping a service.
DoneHandler: func(srv micro.Service) {
info := srv.Info()
Expand All @@ -53,6 +48,12 @@ func ExampleAddService() {
info := srv.Info()
fmt.Printf("Service %q returned an error on subject %q: %s", info.Name, err.Subject, err.Description)
},

// optional base handler
Endpoint: &micro.EndpointConfig{
Subject: "echo",
Handler: micro.HandlerFunc(echoHandler),
},
}

srv, err := micro.AddService(nc, config)
Expand All @@ -62,6 +63,92 @@ func ExampleAddService() {
defer srv.Stop()
}

func ExampleService_AddEndpoint() {
nc, err := nats.Connect("127.0.0.1:4222")
if err != nil {
log.Fatal(err)
}
defer nc.Close()

echoHandler := func(req micro.Request) {
req.Respond(req.Data())
}

config := micro.Config{
Name: "EchoService",
Version: "1.0.0",
}

srv, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}

// endpoint will be registered under "Echo" subject
err = srv.AddEndpoint("Echo", micro.HandlerFunc(echoHandler))
if err != nil {
log.Fatal(err)
}
}

func ExampleWithEndpointSubject() {
nc, err := nats.Connect("127.0.0.1:4222")
if err != nil {
log.Fatal(err)
}
defer nc.Close()

echoHandler := func(req micro.Request) {
req.Respond(req.Data())
}

config := micro.Config{
Name: "EchoService",
Version: "1.0.0",
}

srv, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}

// endpoint will be registered under "service.echo" subject
err = srv.AddEndpoint("Echo", micro.HandlerFunc(echoHandler), micro.WithEndpointSubject("service.echo"))
if err != nil {
log.Fatal(err)
}
}

func ExampleService_AddGroup() {
nc, err := nats.Connect("127.0.0.1:4222")
if err != nil {
log.Fatal(err)
}
defer nc.Close()

echoHandler := func(req micro.Request) {
req.Respond(req.Data())
}

config := micro.Config{
Name: "EchoService",
Version: "1.0.0",
}

srv, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}

v1 := srv.AddGroup("v1")

// endpoint will be registered under "v1.Echo" subject
err = v1.AddEndpoint("Echo", micro.HandlerFunc(echoHandler))
if err != nil {
log.Fatal(err)
}
}

func ExampleService_Info() {
nc, err := nats.Connect("127.0.0.1:4222")
if err != nil {
Expand All @@ -71,10 +158,6 @@ func ExampleService_Info() {

config := micro.Config{
Name: "EchoService",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
},
}

srv, _ := micro.AddService(nc, config)
Expand All @@ -86,7 +169,7 @@ func ExampleService_Info() {
fmt.Println(info.Name)
fmt.Println(info.Description)
fmt.Println(info.Version)
fmt.Println(info.Subject)
fmt.Println(info.Subjects)
}

func ExampleService_Stats() {
Expand All @@ -99,19 +182,18 @@ func ExampleService_Stats() {
config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Endpoint: &micro.EndpointConfig{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
}

srv, _ := micro.AddService(nc, config)

// stats of a service instance
stats := srv.Stats()

fmt.Println(stats.AverageProcessingTime)
fmt.Println(stats.ProcessingTime)
fmt.Println(stats.Endpoints[0].AverageProcessingTime)
fmt.Println(stats.Endpoints[0].ProcessingTime)

}

Expand All @@ -125,10 +207,6 @@ func ExampleService_Stop() {
config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
},
}

srv, _ := micro.AddService(nc, config)
Expand Down Expand Up @@ -156,10 +234,6 @@ func ExampleService_Stopped() {
config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
},
}

srv, _ := micro.AddService(nc, config)
Expand All @@ -185,10 +259,6 @@ func ExampleService_Reset() {
config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
},
}

srv, _ := micro.AddService(nc, config)
Expand Down
2 changes: 1 addition & 1 deletion micro/request.go
@@ -1,4 +1,4 @@
// Copyright 2022 The NATS Authors
// Copyright 2022-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down

0 comments on commit 8d14209

Please sign in to comment.