Skip to content

Commit

Permalink
Revert to using service interface, rename package to micro
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpio committed Dec 14, 2022
1 parent ba87618 commit dc44f93
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package micro

import (
"fmt"
Expand All @@ -19,7 +19,7 @@ func Example() {
}
defer nc.Close()

// Service handler is a function which takes *service.Request as argument.
// Service handler is a function which takes Service.Request as argument.
// req.Respond or req.Error should be used to respond to the request.
incrementHandler := func(req *Request) {
val, err := strconv.Atoi(string(req.Data))
Expand All @@ -46,7 +46,7 @@ func Example() {
// 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 := Add(nc, config)
svc, err := AddService(nc, config)
if err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 12 additions & 12 deletions service/example_test.go → micro/example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package micro

import (
"fmt"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/nats-io/nats.go"
)

func ExampleAdd() {
func ExampleAddService() {
nc, err := nats.Connect("127.0.0.1:4222")
if err != nil {
log.Fatal(err)
Expand All @@ -28,17 +28,17 @@ func ExampleAdd() {
},

// DoneHandler can be set to customize behavior on stopping a service.
DoneHandler: func(srv *Service) {
fmt.Printf("stopped service %q with ID %q\n", srv.Name, srv.ID())
DoneHandler: func(srv Service) {
fmt.Printf("stopped service %q with ID %q\n", srv.Name(), srv.ID())
},

// ErrorHandler can be used to customize behavior on service execution error.
ErrorHandler: func(srv *Service, err *NATSError) {
fmt.Printf("Service %q returned an error on subject %q: %s", srv.Name, err.Subject, err.Description)
ErrorHandler: func(srv Service, err *NATSError) {
fmt.Printf("Service %q returned an error on subject %q: %s", srv.Name(), err.Subject, err.Description)
},
}

srv, err := Add(nc, config)
srv, err := AddService(nc, config)
if err != nil {
log.Fatal(err)
}
Expand All @@ -60,7 +60,7 @@ func ExampleService_ID() {
},
}

srv, _ := Add(nc, config)
srv, _ := AddService(nc, config)

// unique service ID
id := srv.ID()
Expand All @@ -82,7 +82,7 @@ func ExampleService_Stats() {
},
}

srv, _ := Add(nc, config)
srv, _ := AddService(nc, config)

// stats of a service instance
stats := srv.Stats()
Expand All @@ -108,7 +108,7 @@ func ExampleService_Stop() {
},
}

srv, _ := Add(nc, config)
srv, _ := AddService(nc, config)

// stop a service
err = srv.Stop()
Expand Down Expand Up @@ -138,7 +138,7 @@ func ExampleService_Stopped() {
},
}

srv, _ := Add(nc, config)
srv, _ := AddService(nc, config)

// stop a service
err = srv.Stop()
Expand Down Expand Up @@ -166,7 +166,7 @@ func ExampleService_Reset() {
},
}

srv, _ := Add(nc, config)
srv, _ := AddService(nc, config)

// reset endpoint stats on this service
srv.Reset()
Expand Down
2 changes: 1 addition & 1 deletion service/request.go → micro/request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package micro

import (
"encoding/json"
Expand Down
Loading

0 comments on commit dc44f93

Please sign in to comment.