Skip to content

Commit

Permalink
server: Pass the assigner to the Finish method of a Service
Browse files Browse the repository at this point in the history
This is a breaking change to the Service interface. It extends the Finish
method to receive the assigner that was passed to the server, along with the
server exit status. This permits the implementation to clean up per-assigner
state when the server exits.
  • Loading branch information
creachadair committed May 18, 2021
1 parent 3b83ed0 commit ff6de6a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (Service) Assigner() (jrpc2.Assigner, error) {
})}, nil
}

func (s Service) Finish(stat jrpc2.ServerStatus) {
func (s Service) Finish(_ jrpc2.Assigner, stat jrpc2.ServerStatus) {
fmt.Printf("SERVICE FINISHED err=%v\n", stat.Err)
close(s.done)
}
Expand Down
12 changes: 7 additions & 5 deletions server/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ type Service interface {
Assigner() (jrpc2.Assigner, error)

// This method is called when the server for this service has exited.
Finish(jrpc2.ServerStatus)
// The arguments are the assigner returned by the Assigner method and the
// server exit status.
Finish(jrpc2.Assigner, jrpc2.ServerStatus)
}

// Static wraps a jrpc2.Assigner to trivially implement the Service interface.
func Static(m jrpc2.Assigner) func() Service { return static{methods: m}.New }

type static struct{ methods jrpc2.Assigner }

func (s static) New() Service { return s }
func (s static) Assigner() (jrpc2.Assigner, error) { return s.methods, nil }
func (static) Finish(jrpc2.ServerStatus) {}
func (s static) New() Service { return s }
func (s static) Assigner() (jrpc2.Assigner, error) { return s.methods, nil }
func (static) Finish(jrpc2.Assigner, jrpc2.ServerStatus) {}

// Loop obtains connections from lst and starts a server for each with the
// given service constructor and options, running in a new goroutine. If accept
Expand Down Expand Up @@ -66,7 +68,7 @@ func Loop(lst net.Listener, newService func() Service, opts *LoopOptions) error
}
srv := jrpc2.NewServer(assigner, serverOpts).Start(ch)
stat := srv.WaitStatus()
svc.Finish(stat)
svc.Finish(assigner, stat)
if stat.Err != nil {
log("Server exit: %v", stat.Err)
}
Expand Down
5 changes: 4 additions & 1 deletion server/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (t *testSession) Assigner() (jrpc2.Assigner, error) {
}, nil
}

func (t *testSession) Finish(stat jrpc2.ServerStatus) {
func (t *testSession) Finish(assigner jrpc2.Assigner, stat jrpc2.ServerStatus) {
if _, ok := assigner.(handler.Map); !ok {
t.t.Errorf("Finished assigner: got %+v, want handler.Map", assigner)
}
if !t.init {
t.t.Error("Service finished without being initialized")
}
Expand Down
2 changes: 1 addition & 1 deletion server/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func Run(ch channel.Channel, svc Service, opts *jrpc2.ServerOptions) error {
}
srv := jrpc2.NewServer(assigner, opts).Start(ch)
stat := srv.WaitStatus()
svc.Finish(stat)
svc.Finish(assigner, stat)
return stat.Err
}
2 changes: 1 addition & 1 deletion server/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (t *testService) Assigner() (jrpc2.Assigner, error) {
return t.assigner, nil
}

func (t *testService) Finish(stat jrpc2.ServerStatus) {
func (t *testService) Finish(_ jrpc2.Assigner, stat jrpc2.ServerStatus) {
t.finishCalled = true
t.stat = stat
}
Expand Down

0 comments on commit ff6de6a

Please sign in to comment.