Skip to content

Commit

Permalink
lsps0: check service impl satisfies handler type.
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Aug 17, 2023
1 parent d2862d3 commit fafedd8
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lsps0/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"log"
"reflect"
"sync"
"time"

Expand Down Expand Up @@ -139,17 +140,17 @@ func (s *Server) Serve(lis lightning.CustomMsgClient) error {
defer func() { <-guard }()

// Call the method handler for the requested method.
r, err := m.method.Handler(m.service.serviceImpl, context.TODO(), df)
if err != nil {
s, ok := status.FromError(err)
if !ok {
log.Printf("Internal error when processing custom message '%s' from peer '%s': %v", string(msg.Data), msg.PeerId, err)
s = status.New(codes.InternalError, InternalError)
}
r, err := m.method.Handler(m.service.serviceImpl, context.TODO(), df)
if err != nil {
s, ok := status.FromError(err)
if !ok {
log.Printf("Internal error when processing custom message '%s' from peer '%s': %v", string(msg.Data), msg.PeerId, err)
s = status.New(codes.InternalError, InternalError)
}

sendError(lis, msg, req, s)
return
}
}

sendResponse(lis, msg, req, r)
}()
Expand Down Expand Up @@ -234,6 +235,16 @@ func sendError(
}

func (s *Server) RegisterService(desc *ServiceDesc, impl interface{}) {
if impl == nil {
log.Fatalf("lsps0: Server.RegisterService got nil service implementation")
}

ht := reflect.TypeOf(desc.HandlerType).Elem()
st := reflect.TypeOf(impl)
if !st.Implements(ht) {
log.Fatalf("lsps0: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht)
}

s.mu.Lock()
defer s.mu.Unlock()
log.Printf("RegisterService(%q)", desc.ServiceName)
Expand Down

0 comments on commit fafedd8

Please sign in to comment.