notify/client_record.go

69 lines
1.7 KiB
Go
Raw Normal View History

package notify
import "context"
func (c *ClientCommon) SetRecordStreamHandler(fn func(RecordAcceptInfo) error) {
runtime := c.getRecordRuntime()
if runtime == nil {
return
}
runtime.setHandler(fn)
}
func (c *ClientCommon) OpenRecordStream(ctx context.Context, opt RecordOpenOptions) (RecordStream, error) {
if c == nil {
return nil, errStreamClientNil
}
opt = normalizeRecordOpenOptions(opt)
stream, err := c.OpenStream(ctx, opt.Stream)
if err != nil {
return nil, err
}
record, err := WrapStreamAsRecord(stream, opt)
if err != nil {
_ = stream.Reset(err)
return nil, err
}
bindRecordRuntime(record, c.getRecordRuntime())
return record, nil
}
func (c *ClientCommon) claimInboundRecordStream(stream *streamHandle) (bool, error) {
if stream == nil || stream.Channel() != StreamRecordChannel {
return false, nil
}
runtime := c.getRecordRuntime()
if runtime == nil {
return true, errRecordRuntimeNil
}
handler := runtime.handlerSnapshot()
if handler == nil {
return true, errRecordHandlerNotConfigured
}
record, err := WrapStreamAsRecord(stream, RecordOpenOptions{
Stream: StreamOpenOptions{
ID: stream.ID(),
Channel: stream.Channel(),
Metadata: stream.Metadata(),
ReadTimeout: stream.readTimeoutSnapshot(),
WriteTimeout: stream.writeTimeoutSnapshot(),
},
})
if err != nil {
return true, err
}
bindRecordRuntime(record, runtime)
info := RecordAcceptInfo{
ID: stream.ID(),
Metadata: stream.Metadata(),
TransportGeneration: stream.TransportGeneration(),
RecordStream: record,
}
go func() {
if err := handler(info); err != nil {
_ = record.Reset(err)
}
}()
return true, nil
}