lsps0: check service impl satisfies handler type.

This commit is contained in:
Jesse de Wit
2023-08-17 09:38:43 +02:00
parent 0d2253ac44
commit e9a1e569f9

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"log" "log"
"reflect"
"sync" "sync"
"time" "time"
@@ -234,6 +235,16 @@ func sendError(
} }
func (s *Server) RegisterService(desc *ServiceDesc, impl interface{}) { 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() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
log.Printf("RegisterService(%q)", desc.ServiceName) log.Printf("RegisterService(%q)", desc.ServiceName)