MCPcopy
hub / github.com/nats-io/nats.go / StreamNameBySubject

Method StreamNameBySubject

jetstream/jetstream.go:1159–1186  ·  view source on GitHub ↗

StreamNameBySubject returns the name of the stream bound to the given subject. If no stream is bound to given subject, ErrStreamNotFound is returned.

(ctx context.Context, subject string)

Source from the content-addressed store, hash-verified

1157// subject. If no stream is bound to given subject, ErrStreamNotFound
1158// is returned.
1159func (js *jetStream) StreamNameBySubject(ctx context.Context, subject string) (string, error) {
1160 ctx, cancel := js.wrapContextWithoutDeadline(ctx)
1161 if cancel != nil {
1162 defer cancel()
1163 }
1164 if err := validateSubject(subject); err != nil {
1165 return "", err
1166 }
1167
1168 r := &streamsRequest{Subject: subject}
1169 req, err := json.Marshal(r)
1170 if err != nil {
1171 return "", err
1172 }
1173 var resp streamNamesResponse
1174 _, err = js.apiRequestJSON(ctx, apiStreams, &resp, req)
1175 if err != nil {
1176 return "", err
1177 }
1178 if resp.Error != nil {
1179 return "", resp.Error
1180 }
1181 if len(resp.Streams) == 0 {
1182 return "", ErrStreamNotFound
1183 }
1184
1185 return resp.Streams[0], nil
1186}
1187
1188// Name returns a channel allowing retrieval of stream names returned by [StreamNames]
1189func (s *streamLister) Name() <-chan string {

Callers

nothing calls this directly

Calls 3

apiRequestJSONMethod · 0.95
validateSubjectFunction · 0.70

Tested by

no test coverage detected