Skip to content

Commit

Permalink
refactor: Reduce namespace polution by moving service examples into a…
Browse files Browse the repository at this point in the history
… package
  • Loading branch information
mpolitzer committed Dec 4, 2024
1 parent 3db4a7e commit 2a9e216
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 83 deletions.
23 changes: 0 additions & 23 deletions pkg/service/dummy.go

This file was deleted.

27 changes: 27 additions & 0 deletions pkg/service/dummy/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// A Service implementation that does nothing
package dummy

import (
"github.com/cartesi/rollups-node/pkg/service"
)

type Service struct {
service.Service
}

type CreateInfo struct {
service.CreateInfo
}

func Create(c *CreateInfo, s *Service) error {
return service.Create(&c.CreateInfo, &s.Service)
}

func (s *Service) Alive() bool { return true }
func (s *Service) Ready() bool { return true }
func (s *Service) Reload() []error { return nil }
func (s *Service) Tick() []error { return nil }
func (s *Service) Stop(bool) []error { return nil }
34 changes: 17 additions & 17 deletions pkg/service/list.go → pkg/service/list/list.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// A service implementation of a list of services
package service

import (
"fmt"
"net/http"
"strings"
"time"
"github.com/cartesi/rollups-node/pkg/service"
)

// A Service implementation in terms of a list of other services
type ListService struct {
Service
type Service struct {
service.Service
Children []*Service
}

type CreateListInfo struct {
CreateInfo
service.CreateInfo
}

func CreateList(ci CreateListInfo, list *ListService) error {
err := Create(&ci.CreateInfo, &list.Service)
func CreateList(c *CreateListInfo, list *Service) error {
err := service.Create(&c.CreateInfo, &list.Service)
if err != nil {
return err
}

if ci.CreateInfo.TelemetryCreate {
if c.CreateInfo.TelemetryCreate {
list.ServeMux.Handle("/"+list.Name+"/readyz",
http.HandlerFunc(list.ReadyHandler))
list.ServeMux.Handle("/"+list.Name+"/livez",
Expand All @@ -35,15 +37,15 @@ func CreateList(ci CreateListInfo, list *ListService) error {
return nil
}

func (me *ListService) Alive() bool {
func (me *Service) Alive() bool {
alive := true
for _, s := range me.Children {
alive = alive && s.Alive()
}
return alive
}

func (me *ListService) Ready() bool {
func (me *Service) Ready() bool {
allReady := true
for _, s := range me.Children {
ready := s.Ready()
Expand All @@ -52,7 +54,7 @@ func (me *ListService) Ready() bool {
return allReady
}

func (me *ListService) Reload() []error {
func (me *Service) Reload() []error {
var all []error
for _, s := range me.Children {
es := s.Reload()
Expand All @@ -63,7 +65,7 @@ func (me *ListService) Reload() []error {

/*
// Simple tick runs children one at a time
func (me *ListService) Tick() []error {
func (me *Service) Tick() []error {
var all []error
for _, s := range me.Children {
all = append(all, s.Tick()...)
Expand All @@ -72,8 +74,8 @@ func (me *ListService) Tick() []error {
}
*/

// *
func (me *ListService) Tick() []error {
// Tick runs all registered services in parallel.
func (me *Service) Tick() []error {
c := make(chan []error)
var all []error
for _, s := range me.Children {
Expand All @@ -98,9 +100,7 @@ func (me *ListService) Tick() []error {
return all
}

//*/

func (me *ListService) Stop(force bool) []error {
func (me *Service) Stop(force bool) []error {
var all []error
for _, s := range me.Children {
es := s.Stop(force)
Expand All @@ -109,7 +109,7 @@ func (me *ListService) Stop(force bool) []error {
return all
}

func (me *ListService) AliveHandler(w http.ResponseWriter, r *http.Request) {
func (me *Service) AliveHandler(w http.ResponseWriter, r *http.Request) {
ss := []string{}

allAlive := true
Expand All @@ -128,7 +128,7 @@ func (me *ListService) AliveHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (me *ListService) ReadyHandler(w http.ResponseWriter, r *http.Request) {
func (me *Service) ReadyHandler(w http.ResponseWriter, r *http.Request) {
ss := []string{}

allReady := true
Expand Down
43 changes: 0 additions & 43 deletions pkg/service/slow.go

This file was deleted.

47 changes: 47 additions & 0 deletions pkg/service/slow/slow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// A service implementation that simulate a long running Tick
package slow

import (
"time"
"github.com/cartesi/rollups-node/pkg/service"
)

// A Service implementation that takes time to Tick
type Service struct {
service.Service
Time time.Duration
}

type CreageInfo struct {
service.CreateInfo
Time time.Duration
}

func Creage(c *CreageInfo, s *Service) error {
s.Time = c.Time
return service.Create(&c.CreateInfo, &s.Service)
}

func (me *Service) Alive() bool {
return true
}

func (me *Service) Ready() bool {
return true
}

func (me *Service) Reload() []error {
return nil
}

func (me *Service) Tick() []error {
time.Sleep(me.Time)
return nil
}

func (me *Service) Stop(bool) []error {
return nil
}

0 comments on commit 2a9e216

Please sign in to comment.