@@ -33,6 +33,98 @@ func (p *{{> svc/proc_func_name}}) NewReqArgs() thrift.ReadableStruct {
3333func (p *{ {> svc/proc_func_name} }) RunContext(ctx context.Context, reqStruct thrift.ReadableStruct) (thrift.WritableStruct, error) {
3434 return nil, thrift.NewApplicationException(thrift.INTERNAL_ERROR, " not supported" )
3535}
36+
37+ func (p *{ {> svc/proc_func_name} }) RunStreamContext(
38+ ctx context.Context,
39+ reqStruct thrift.ReadableStruct,
40+ onFirstResponse func(thrift.WritableStruct),
41+ onStreamNext func(thrift.WritableStruct),
42+ onStreamComplete func(),
43+ ) {
44+ {{#function:params.fields?} }
45+ args := reqStruct.(*req{ {service:go_name} }{ {function:go_name} })
46+ { {/function:params.fields?} }
47+ firstResponse := newResp{ {service:go_name} }{ {function:go_name} }()
48+ { {#function:stream_has_first_response?} }retval, { {/function:stream_has_first_response?} }elemProducerFunc, initialErr := p.handler.{ {function:go_name} }({ {!
49+ } }ctx{ {#if function:params.fields?} }, { {/if } }{ {!
50+ } }{ {#function:args} }{ {!
51+ } }args.{ {field:go_name} }{ {!
52+ } }{ {^last?} }, { {/last?} }{ {!
53+ } }{ {/function:args} })
54+ if initialErr != nil {
55+ {{#function:exceptions?} }
56+ switch v := initialErr.(type) {
57+ {{#function:exceptions} }
58+ case *{ {#field:type} }{ {> common/type} }{ {/field:type} }:
59+ firstResponse.{ {field:go_name} } = v
60+ onFirstResponse(firstResponse)
61+ { {/function:exceptions} }
62+ default:
63+ internalErr := fmt.Errorf("Internal error processing { {function:go_name} }: %w", initialErr)
64+ x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, internalErr.Error())
65+ onFirstResponse(x)
66+ }
67+ { {/function:exceptions?} }
68+ { {^function:exceptions?} }
69+ internalErr := fmt.Errorf("Internal error processing { {function:go_name} }: %w", initialErr)
70+ x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, internalErr.Error())
71+ onFirstResponse(x)
72+ { {/function:exceptions?} }
73+ onStreamComplete()
74+ return
75+ }
76+
77+ { {#function:stream_has_first_response?} }
78+ { {#function:stream_first_response_type} }
79+ firstResponse.{ {function:retval_field_name} } = { {^type:nilable?} }& { {/type:nilable?} }retval
80+ { {/function:stream_first_response_type} }
81+ { {/function:stream_has_first_response?} }
82+ onFirstResponse(firstResponse)
83+
84+ { {#function:stream_elem_type} }
85+ fbthriftElemChan := make(chan { {#type:structured?} }*{ {/type:structured?} }{ {> common/type} }, thrift.DefaultStreamBufferSize)
86+ { {/function:stream_elem_type} }
87+ var senderWg sync.WaitGroup
88+ senderWg.Add(1)
89+ // Sender goroutine (receives elements on the channel and sends them out via onStreamNext)
90+ go func() {
91+ defer senderWg.Done()
92+ for elem := range fbthriftElemChan {
93+ streamWrapStruct := newStream{{service:go_name} }{ {function:go_name} }()
94+ { {#function:stream_elem_type} }
95+ streamWrapStruct.{ {function:retval_field_name} } = { {^type:nilable?} }& { {/type:nilable?} }elem
96+ { {/function:stream_elem_type} }
97+ onStreamNext(streamWrapStruct)
98+ }
99+ }()
100+
101+ streamErr := elemProducerFunc(ctx, fbthriftElemChan)
102+ // Stream is complete. Close the channel and wait for the sender goroutine to finish.
103+ close(fbthriftElemChan)
104+ senderWg.Wait()
105+ if streamErr != nil {
106+ {{#function:stream_exceptions?} }
107+ streamWrapStruct := newStream{ {service:go_name} }{ {function:go_name} }()
108+ switch v := streamErr.(type) {
109+ {{#function:stream_exceptions} }
110+ case *{ {#field:type} }{ {> common/type} }{ {/field:type} }:
111+ streamWrapStruct.{ {field:go_name} } = v
112+ onStreamNext(streamWrapStruct)
113+ { {/function:stream_exceptions} }
114+ default:
115+ internalErr := fmt.Errorf("Internal stream handler error { {function:go_name} }: %w", streamErr)
116+ x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, internalErr.Error())
117+ onStreamNext(x)
118+ }
119+ { {/function:stream_exceptions?} }
120+ { {^function:stream_exceptions?} }
121+ internalErr := fmt.Errorf("Internal stream handler error { {function:go_name} }: %w", streamErr)
122+ x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, internalErr.Error())
123+ onStreamNext(x)
124+ { {/function:stream_exceptions?} }
125+ }
126+ onStreamComplete()
127+ }
36128{ {#else } }
37129func (p *{ {> svc/proc_func_name} }) RunContext(ctx context.Context, reqStruct thrift.ReadableStruct) (thrift.WritableStruct, error) {
38130 {{#function:params.fields?} }
0 commit comments