Skip to content

Commit

Permalink
use ProcessorFunctionMap instead of GetProcessorFunction in Processor…
Browse files Browse the repository at this point in the history
… interface

Summary:
use ProcessorFunctionMap instead of GetProcessorFunction in Processor interface

This aligns better with thwork, which we are refactoring to bring into the open source

Reviewed By: podtserkovskiy

Differential Revision: D64172011

fbshipit-source-id: 6c799d1d5ad1f57fdf0a021b404b269babc7bcfc
  • Loading branch information
Walter Schulze authored and facebook-github-bot committed Oct 10, 2024
1 parent c1e99d8 commit d4ac595
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 39 deletions.
7 changes: 2 additions & 5 deletions thrift/lib/go/thrift/header_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ type headerServerTestProcessor struct {
requests chan<- *MyTestStruct
}

func (t *headerServerTestProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
if name == "test" {
return &headerServerTestProcessorFunction{&testProcessorFunction{}, t.requests}
}
return nil
func (t *headerServerTestProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
return map[string]types.ProcessorFunction{"test": &headerServerTestProcessorFunction{&testProcessorFunction{}, t.requests}}
}

type headerServerTestProcessorFunction struct {
Expand Down
19 changes: 10 additions & 9 deletions thrift/lib/go/thrift/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ func WrapInterceptor(interceptor Interceptor, p types.Processor) types.Processor
}
}

func (p *interceptorProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
pf := p.Processor.GetProcessorFunction(name)
if pf == nil {
return nil // see ProcessContext, this semantic means 'no such function'.
}
return &interceptorProcessorFunction{
interceptor: p.interceptor,
methodName: name,
ProcessorFunction: pf,
func (p *interceptorProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
m := p.Processor.ProcessorFunctionMap()
mi := make(map[string]types.ProcessorFunction)
for name, pf := range m {
mi[name] = &interceptorProcessorFunction{
interceptor: p.interceptor,
methodName: name,
ProcessorFunction: pf,
}
}
return mi
}

type interceptorProcessorFunction struct {
Expand Down
4 changes: 2 additions & 2 deletions thrift/lib/go/thrift/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type exampleProcessor struct {
hit *bool
}

func (ep *exampleProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
func (ep *exampleProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
*ep.hit = true
return nil // happens in "no such method" case
}
Expand All @@ -38,7 +38,7 @@ func TestInterceptorWrapperNilFunctionContext(t *testing.T) {
proc := &exampleProcessor{nil, &hit}

derivedProc := WrapInterceptor(emptyInterceptor, proc)
pFunc := derivedProc.GetProcessorFunction("blah")
pFunc := derivedProc.ProcessorFunctionMap()["blah"]
if hit != true {
t.Fatalf("interceptor should have called underlying processor function handler.")
}
Expand Down
7 changes: 5 additions & 2 deletions thrift/lib/go/thrift/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ func getProcessorFunction(processor types.Processor, messageType types.MessageTy
// case one: invalid message type
return nil, types.NewApplicationException(types.UNKNOWN_METHOD, fmt.Sprintf("unexpected message type: %d", messageType))
}
if pf := processor.GetProcessorFunction(name); pf != nil {
return pf, nil
pmap := processor.ProcessorFunctionMap()
if pmap != nil {
if pf := pmap[name]; pf != nil {
return pf, nil
}
}
return nil, types.NewApplicationException(types.UNKNOWN_METHOD, fmt.Sprintf("no such function: %q", name))
}
Expand Down
7 changes: 2 additions & 5 deletions thrift/lib/go/thrift/rocket_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ type rocketServerTestProcessor struct {
requests chan<- *MyTestStruct
}

func (t *rocketServerTestProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
if name == "test" {
return &rocketServerTestProcessorFunction{&testProcessorFunction{}, t.requests}
}
return nil
func (t *rocketServerTestProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
return map[string]types.ProcessorFunction{"test": &rocketServerTestProcessorFunction{&testProcessorFunction{}, t.requests}}
}

type rocketServerTestProcessorFunction struct {
Expand Down
17 changes: 10 additions & 7 deletions thrift/lib/go/thrift/rocket_upgrade_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package thrift

import (
"context"
"maps"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
)
Expand All @@ -31,19 +32,20 @@ func newRocketUpgradeProcessor(processor types.Processor) *rocketUpgradeProcesso
return &rocketUpgradeProcessor{processor: processor}
}

func (r *rocketUpgradeProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
func (r *rocketUpgradeProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
m := r.processor.ProcessorFunctionMap()
// The upgradeToRocket function in thrift/lib/thrift/RocketUpgrade.thrift
// asks the server to upgrade to using the rocket protocol.
// If the server does not respond with an error,
// it is assumed that the server has upgraded to rocket.
if name == "upgradeToRocket" {
r.upgraded = true
return &rocketUpgradeProcessorFunction{}
}
return r.processor.GetProcessorFunction(name)
mr := map[string]types.ProcessorFunction{"upgradeToRocket": &rocketUpgradeProcessorFunction{&r.upgraded}}
maps.Copy(mr, m)
return mr
}

type rocketUpgradeProcessorFunction struct{}
type rocketUpgradeProcessorFunction struct {
upgraded *bool
}

func (r *rocketUpgradeProcessorFunction) Read(prot types.Decoder) (types.Struct, types.Exception) {
args := &reqServiceUpgradeToRocket{}
Expand Down Expand Up @@ -77,5 +79,6 @@ func (r *rocketUpgradeProcessorFunction) Write(seqID int32, result types.Writabl
if err2 = prot.Flush(); err == nil && err2 != nil {
err = err2
}
*r.upgraded = true
return err
}
7 changes: 2 additions & 5 deletions thrift/lib/go/thrift/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ func TestSimpleServer(t *testing.T) {
type testProcessor struct {
}

func (t *testProcessor) GetProcessorFunction(name string) types.ProcessorFunction {
if name == "test" {
return &testProcessorFunction{}
}
return nil
func (t *testProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
return map[string]types.ProcessorFunction{"test": &testProcessorFunction{}}
}

type testProcessorFunction struct{}
Expand Down
8 changes: 4 additions & 4 deletions thrift/lib/go/thrift/types/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
// manage I/O and processing of a input message for a specific
// server function
type Processor interface {
// GetProcessorFunction is given the name of a thrift function and
// the type of the inbound thrift message. It is expected to return
// GetProcessorFunctionMap is given the name of a thrift function
// of the inbound thrift message. It is expected to return
// a non-nil GetProcessorFunction when the function can be successfully
// found.
//
// If ProcessorFunction is nil, a generic error will be
// If GetProcessorFunctionMap is nil or a value in the map is nil, a generic error will be
// sent which explains that no processor function exists with the specified
// name on this server.
GetProcessorFunction(name string) ProcessorFunction
ProcessorFunctionMap() map[string]ProcessorFunction
}

// ProcessorFunction is the interface that must be implemented in
Expand Down

0 comments on commit d4ac595

Please sign in to comment.