WithNoticeHandler() just calls a function, no channel stuff anymore.
probably fixes https://t.me/nostr_protocol/145832 I think we were using this channel for legacy reasons, it feels completely unnecessary now.
This commit is contained in:
20
relay.go
20
relay.go
@@ -33,8 +33,8 @@ type Relay struct {
|
|||||||
connectionContext context.Context // will be canceled when the connection closes
|
connectionContext context.Context // will be canceled when the connection closes
|
||||||
connectionContextCancel context.CancelFunc
|
connectionContextCancel context.CancelFunc
|
||||||
|
|
||||||
challenge string // NIP-42 challenge, we only keep the last
|
challenge string // NIP-42 challenge, we only keep the last
|
||||||
notices chan string // NIP-01 NOTICEs
|
noticeHandler func(string) // NIP-01 NOTICEs
|
||||||
okCallbacks *xsync.MapOf[string, func(bool, string)]
|
okCallbacks *xsync.MapOf[string, func(bool, string)]
|
||||||
writeQueue chan writeRequest
|
writeQueue chan writeRequest
|
||||||
subscriptionChannelCloseQueue chan *Subscription
|
subscriptionChannelCloseQueue chan *Subscription
|
||||||
@@ -99,12 +99,7 @@ var (
|
|||||||
type WithNoticeHandler func(notice string)
|
type WithNoticeHandler func(notice string)
|
||||||
|
|
||||||
func (nh WithNoticeHandler) ApplyRelayOption(r *Relay) {
|
func (nh WithNoticeHandler) ApplyRelayOption(r *Relay) {
|
||||||
r.notices = make(chan string)
|
r.noticeHandler = nh
|
||||||
go func() {
|
|
||||||
for notice := range r.notices {
|
|
||||||
nh(notice)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithSignatureChecker must be a function that checks the signature of an
|
// WithSignatureChecker must be a function that checks the signature of an
|
||||||
@@ -167,10 +162,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
|||||||
// to be used when the connection is closed
|
// to be used when the connection is closed
|
||||||
go func() {
|
go func() {
|
||||||
<-r.connectionContext.Done()
|
<-r.connectionContext.Done()
|
||||||
// close these things when the connection is closed
|
|
||||||
if r.notices != nil {
|
|
||||||
close(r.notices)
|
|
||||||
}
|
|
||||||
// stop the ticker
|
// stop the ticker
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
// close all subscriptions
|
// close all subscriptions
|
||||||
@@ -226,8 +218,8 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
|||||||
switch env := envelope.(type) {
|
switch env := envelope.(type) {
|
||||||
case *NoticeEnvelope:
|
case *NoticeEnvelope:
|
||||||
// see WithNoticeHandler
|
// see WithNoticeHandler
|
||||||
if r.notices != nil {
|
if r.noticeHandler != nil {
|
||||||
r.notices <- string(*env)
|
r.noticeHandler(string(*env))
|
||||||
} else {
|
} else {
|
||||||
log.Printf("NOTICE from %s: '%s'\n", r.URL, string(*env))
|
log.Printf("NOTICE from %s: '%s'\n", r.URL, string(*env))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user