Skip to content

Commit

Permalink
Add grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpio committed Jan 9, 2023
1 parent a8d695d commit 3985969
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 392 deletions.
23 changes: 17 additions & 6 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 @@ -33,16 +46,14 @@ func ExampleHandler() {
Name: "RectangleAreaService",
Version: "0.1.0",
RootSubject: "area",
Endpoints: map[string]micro.Endpoint{
"Rectangle": {
Subject: "rec",
Handler: rec,
},
},
}
svc, err := micro.AddService(nc, config)
if err != nil {
log.Fatal(err)
}
defer svc.Stop()
_, err = svc.AddEndpoint("Rectangle", rec)
if err != nil {
log.Fatal(err)
}
}
45 changes: 23 additions & 22 deletions micro/example_package_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 @@ -60,31 +60,32 @@ func Example() {
Version: "0.1.0",
Description: "Increment numbers",
RootSubject: "numbers",
Endpoints: map[string]micro.Endpoint{
"Increment": {
// service handler
Handler: micro.HandlerFunc(incrementHandler),
// a unique subject serving as a service endpoint
Subject: "increment",
},
"Multiply": {
Handler: micro.HandlerFunc(multiply),
Subject: "multiply",
},
},
}
// 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()

// register endpoints on a service
_, err = svc.AddEndpoint("Increment", micro.HandlerFunc(incrementHandler))
if err != nil {
log.Fatal(err)
}
_, err = svc.AddEndpoint("Multiply", micro.HandlerFunc(multiply))
if err != nil {
log.Fatal(err)
}

// add a group
v1 := svc.AddGroup("v1")
_, err = v1.AddEndpoint("Increment", micro.HandlerFunc(incrementHandler))
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.v1.increment", []byte("3"), 1*time.Second)
if err != nil {
log.Fatal(err)
}
Expand Down
110 changes: 65 additions & 45 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 @@ -29,22 +29,11 @@ func ExampleAddService() {
}
defer nc.Close()

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

config := micro.Config{
Name: "EchoService",
Version: "1.0.0",
Description: "Send back what you receive",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
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 @@ -65,6 +54,67 @@ 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",
RootSubject: "svc",
}

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

endpoint, err := srv.AddEndpoint("Echo", micro.HandlerFunc(echoHandler))
if err != nil {
log.Fatal(err)
}
// Endpoints can be stopped individually or all at once using srv.Stop()
defer endpoint.Stop()
}

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",
RootSubject: "svc",
}

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 @@ -75,12 +125,6 @@ func ExampleService_Info() {
config := micro.Config{
Name: "EchoService",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

srv, _ := micro.AddService(nc, config)
Expand All @@ -106,21 +150,15 @@ func ExampleService_Stats() {
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

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

srv.AddEndpoint("Echo", micro.HandlerFunc(func(r micro.Request) {}))
// stats of a service instance
stats := srv.Stats()

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

}

Expand All @@ -135,12 +173,6 @@ func ExampleService_Stop() {
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

srv, _ := micro.AddService(nc, config)
Expand Down Expand Up @@ -169,12 +201,6 @@ func ExampleService_Stopped() {
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

srv, _ := micro.AddService(nc, config)
Expand All @@ -201,12 +227,6 @@ func ExampleService_Reset() {
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r 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 3985969

Please sign in to comment.