-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
service.go
1187 lines (1034 loc) · 32.9 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package res
import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/jirenius/go-res/logger"
"github.com/jirenius/timerqueue"
nats "github.com/nats-io/nats.go"
)
// Supported RES protocol version.
const protocolVersion = "1.2.3"
// The default size of the in channel receiving messages from NATS Server.
const defaultInChannelSize = 1024
// The default number of workers handling resource requests.
const defaultWorkerCount = 32
// The default duration for which the service will listen for query requests
// sent on a query event
const defaultQueryEventDuration = time.Second * 3
// Common panic messages
const (
serviceAlreadyStarted = "res: service already started"
)
var (
errNotStopped = errors.New("res: service is not stopped")
errNotStarted = errors.New("res: service is not started")
)
// Option set one or more of the handler functions for a resource Handler.
type Option interface{ SetOption(*Handler) }
// The OptionFunc type is an adapter to allow the use of ordinary functions as
// options. If f is a function with the appropriate signature, OptionFunc(f) is
// an Option that calls f.
type OptionFunc func(*Handler)
// SetOption calls f(hs)
func (f OptionFunc) SetOption(hs *Handler) { f(hs) }
// AccessHandler is a function called on resource access requests
type AccessHandler func(AccessRequest)
// GetHandler is a function called on untyped get requests
type GetHandler func(GetRequest)
// ModelHandler is a function called on model get requests
type ModelHandler func(ModelRequest)
// CollectionHandler is a function called on collection get requests
type CollectionHandler func(CollectionRequest)
// CallHandler is a function called on resource call requests
type CallHandler func(CallRequest)
// NewHandler is a function called on new resource call requests
//
// Deprecated: Use CallHandler with Resource response instead; deprecated in RES
// protocol v1.2.0
type NewHandler func(NewRequest)
// AuthHandler is a function called on resource auth requests
type AuthHandler func(AuthRequest)
// ApplyChangeHandler is a function called to apply a model change event. Must
// return a map with the values to apply to revert the changes, or error.
type ApplyChangeHandler func(r Resource, changes map[string]interface{}) (map[string]interface{}, error)
// ApplyAddHandler is a function called to apply a collection add event. Must
// return an error if the add event couldn't be applied to the resource.
type ApplyAddHandler func(r Resource, value interface{}, idx int) error
// ApplyRemoveHandler is a function called to apply a collection remove event.
// Must return the value being removed, or error.
type ApplyRemoveHandler func(r Resource, idx int) (interface{}, error)
// ApplyCreateHandler is a function called to apply a resource create event.
// Must return an error if the resource couldn't be created.
type ApplyCreateHandler func(r Resource, data interface{}) error
// ApplyDeleteHandler is a function called to apply a resource delete event.
// Must return the resource data being removed, or error.
type ApplyDeleteHandler func(r Resource) (interface{}, error)
// Handler contains handler functions for a given resource pattern.
type Handler struct {
// Resource type
Type ResourceType
// Access handler for access requests
Access AccessHandler
// Get handler for get requests.
Get GetHandler
// Call handlers for call requests
Call map[string]CallHandler
// New handler for new call requests
//
// Deprecated: Use Call with Resource response instead; deprecated in RES
// protocol v1.2.0
New NewHandler
// Auth handler for auth requests
Auth map[string]AuthHandler
// ApplyChange handler for applying change event mutations
ApplyChange ApplyChangeHandler
// ApplyAdd handler for applying add event mutations
ApplyAdd ApplyAddHandler
// ApplyRemove handler for applying remove event mutations
ApplyRemove ApplyRemoveHandler
// ApplyCreate handler for applying create event
ApplyCreate ApplyCreateHandler
// ApplyDelete handler for applying delete event
ApplyDelete ApplyDeleteHandler
// Group is the identifier of the group the resource belongs to. All
// resources of the same group will be handled on the same goroutine. The
// group may contain tags, ${tagName}, where the tag name matches a
// parameter placeholder name in the resource pattern. If empty, the
// resource name will be used as identifier.
Group string
// Parallel is a flag telling that all requests to the handler may be
// handled in parallel on different goroutines. If set to true, any value in
// Group will be ignored.
Parallel bool
// OnRegister is callback that is to be call when the handler has been
// registered to a service.
//
// The pattern is the full resource pattern for the resource, including any
// service name or mount paths.
//
// The handler is the handler being registered, and should be considered
// immutable.
OnRegister func(service *Service, pattern Pattern, rh Handler)
// Listeners is a map of event listeners, where the key is the resource
// pattern being listened on, and the value being the callback called on
// events.
//
// The callback will be called in the context of the resource emitting the
// event.
Listeners map[string]func(*Event)
}
const (
stateStopped = iota
stateStarting
stateStarted
stateStopping
)
var (
// Model sets handler type to model
Model = OptionFunc(func(hs *Handler) {
if hs.Type != TypeUnset {
panic("res: resource type set multiple times")
}
hs.Type = TypeModel
})
// Collection sets handler type to collection
Collection = OptionFunc(func(hs *Handler) {
if hs.Type != TypeUnset {
panic("res: resource type set multiple times")
}
hs.Type = TypeCollection
})
)
// Option sets handler fields by passing one or more handler options.
func (h *Handler) Option(hf ...Option) {
for _, f := range hf {
f.SetOption(h)
}
}
// A Service handles incoming requests from NATS Server and calls the
// appropriate callback on the resource handlers.
type Service struct {
*Mux
state int32
nc Conn // NATS Server connection
inCh chan *nats.Msg // Channel for incoming nats messages
rwork map[string]*work // map of resource work
workqueue []*work // Resource work queue.
workbuf []*work // Underlying buffer of the workqueue
workcond sync.Cond // Cond waited on by workers and signaled when work is added to workqueue
wg sync.WaitGroup // WaitGroup for all workers
mu sync.Mutex // Mutex to protect rwork map
logger logger.Logger // Logger
queueGroup string // Queue group to use with CharQueueSubscribe
resetResources []string // List of resource name patterns used on system.reset for resources. Defaults to serviceName+">"
resetAccess []string // List of resource name patterns used system.reset for access. Defaults to serviceName+">"
queryTQ *timerqueue.Queue // Timer queue for query events duration
queryDuration time.Duration // Duration to listen for query requests on a query event
workerCount int // Number of workers handling resource requests
inChannelSize int // Size of the in channel receiving messages from NATS Server
onServe func(*Service) // Handler called after the starting to serve prior to calling system.reset
onDisconnect func(*Service) // Handler called after the service has been disconnected from NATS server.
onReconnect func(*Service) // Handler called after the service has reconnected to NATS server and sent a system reset event.
onError func(*Service, string) // Handler called on errors within the service, or incoming messages not complying with the RES protocol.
}
// NewService creates a new Service.
//
// The name is the service name which will be prefixed to all resources. It must
// be an alphanumeric string with no embedded whitespace, or empty. If name is
// an empty string, the Service will by default handle all resources for all
// namespaces. Use SetReset to limit the namespace scope.
func NewService(name string) *Service {
s := &Service{
Mux: NewMux(name),
queueGroup: name,
logger: logger.NewStdLogger(),
queryDuration: defaultQueryEventDuration,
workerCount: defaultWorkerCount,
inChannelSize: defaultInChannelSize,
}
s.Mux.Register(s)
return s
}
// SetLogger sets the logger. Panics if service is already started.
func (s *Service) SetLogger(l logger.Logger) *Service {
if s.nc != nil {
panic(serviceAlreadyStarted)
}
s.logger = l
return s
}
// SetQueryEventDuration sets the duration for which the service will listen for
// query requests sent on a query event. Default is 3 seconds
func (s *Service) SetQueryEventDuration(d time.Duration) *Service {
if s.nc != nil {
panic(serviceAlreadyStarted)
}
s.queryDuration = d
return s
}
// SetWorkerCount sets the number of workers handling incoming requests. Default
// is 32 workers.
//
// If count is less or equal to zero, the default value is used.
func (s *Service) SetWorkerCount(count int) *Service {
if s.nc != nil {
panic(serviceAlreadyStarted)
}
if count <= 0 {
count = defaultWorkerCount
}
s.workerCount = count
return s
}
// SetInChannelSize sets the size of the in channel receiving messages from NATS
// Server. Default is 1024.
//
// If size is less or equal to zero, the default value is used.
func (s *Service) SetInChannelSize(size int) *Service {
if s.nc != nil {
panic(serviceAlreadyStarted)
}
if size <= 0 {
size = defaultInChannelSize
}
s.inChannelSize = size
return s
}
// SetQueueGroup sets the queue group to use when subscribing to resources. By
// default it will be the same as the service name.
//
// If queue is set to an empty string, the service will not belong to any queue
// group.
func (s *Service) SetQueueGroup(queue string) *Service {
s.queueGroup = queue
return s
}
// SetOnServe sets a function to call when the service has started after sending
// the initial system reset event.
func (s *Service) SetOnServe(f func(*Service)) {
s.onServe = f
}
// SetOnDisconnect sets a function to call when the service has been
// disconnected from NATS server.
func (s *Service) SetOnDisconnect(f func(*Service)) {
s.onDisconnect = f
}
// SetOnReconnect sets a function to call when the service has reconnected to
// NATS server and sent a system reset event.
func (s *Service) SetOnReconnect(f func(*Service)) {
s.onReconnect = f
}
// SetOnError sets a function to call on errors within the service, or incoming
// messages not complying with the RES protocol.
func (s *Service) SetOnError(f func(*Service, string)) {
s.onError = f
}
// Logger returns the logger.
func (s *Service) Logger() logger.Logger {
return s.logger
}
// ProtocolVersion returns the supported RES protocol version.
func (s *Service) ProtocolVersion() string {
return protocolVersion
}
// Conn returns the connection instance used by the service.
//
// If the service is not started, nil is returned.
//
// If the service was started using ListenAndServe, the connection will be of
// type *nats.Conn:
//
// nc := service.Conn().(*nats.Conn)
func (s *Service) Conn() Conn {
return s.nc
}
// infof logs a formatted info entry.
func (s *Service) infof(format string, v ...interface{}) {
if s.logger == nil {
return
}
s.logger.Infof(format, v...)
}
// errorf logs a formatted error entry.
func (s *Service) errorf(format string, v ...interface{}) {
if s.logger == nil {
return
}
s.logger.Errorf(format, v...)
if s.onError != nil {
s.onError(s, fmt.Sprintf(format, v...))
}
}
// tracef logs a formatted trace entry.
func (s *Service) tracef(format string, v ...interface{}) {
if s.logger == nil {
return
}
s.logger.Tracef(format, v...)
}
// Access sets a handler for resource access requests.
func Access(h AccessHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.Access != nil {
panic("res: multiple access handlers")
}
hs.Access = h
})
}
// GetModel sets a handler for model get requests.
func GetModel(h ModelHandler) Option {
return OptionFunc(func(hs *Handler) {
Model(hs)
validateGetHandler(*hs)
hs.Get = func(r GetRequest) { h(ModelRequest(r)) }
})
}
// GetCollection sets a handler for collection get requests.
func GetCollection(h CollectionHandler) Option {
return OptionFunc(func(hs *Handler) {
Collection(hs)
validateGetHandler(*hs)
hs.Get = func(r GetRequest) { h(CollectionRequest(r)) }
})
}
// GetResource sets a handler for untyped resource get requests.
func GetResource(h GetHandler) Option {
return OptionFunc(func(hs *Handler) {
validateGetHandler(*hs)
hs.Get = h
})
}
// Call sets a handler for resource call requests.
//
// Panics if the method is the pre-defined call method set.
//
// For pre-defined set call methods, the handler Set should be used instead.
func Call(method string, h CallHandler) Option {
if method != "*" && !isValidPart(method) {
panic("res: invalid method name: " + method)
}
return OptionFunc(func(hs *Handler) {
if hs.Call == nil {
hs.Call = make(map[string]CallHandler)
}
if _, ok := hs.Call[method]; ok {
panic("res: multiple call handlers for method " + method)
}
hs.Call[method] = h
})
}
// Set sets a handler for set resource requests.
//
// Is a n alias for Call("set", h)
func Set(h CallHandler) Option {
return Call("set", h)
}
// New sets a handler for new resource requests.
//
// Deprecated: Use Call with Resource response instead; deprecated in RES
// protocol v1.2.0
func New(h NewHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.New != nil {
panic("res: multiple new handlers")
}
hs.New = h
})
}
// Auth sets a handler for resource auth requests.
func Auth(method string, h AuthHandler) Option {
if method != "*" && !isValidPart(method) {
panic("res: invalid method name: " + method)
}
return OptionFunc(func(hs *Handler) {
if hs.Auth == nil {
hs.Auth = make(map[string]AuthHandler)
}
if _, ok := hs.Auth[method]; ok {
panic("res: multiple auth handlers for method " + method)
}
hs.Auth[method] = h
})
}
// ApplyChange sets a handler for applying change events.
func ApplyChange(h ApplyChangeHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.ApplyChange != nil {
panic("res: multiple apply change handlers")
}
hs.ApplyChange = h
})
}
// ApplyAdd sets a handler for applying add events.
func ApplyAdd(h ApplyAddHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.ApplyAdd != nil {
panic("res: multiple apply add handlers")
}
hs.ApplyAdd = h
})
}
// ApplyRemove sets a handler for applying remove events.
func ApplyRemove(h ApplyRemoveHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.ApplyRemove != nil {
panic("res: multiple apply remove handlers")
}
hs.ApplyRemove = h
})
}
// ApplyCreate sets a handler for applying create events.
func ApplyCreate(h ApplyCreateHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.ApplyCreate != nil {
panic("res: multiple apply create handlers")
}
hs.ApplyCreate = h
})
}
// ApplyDelete sets a handler for applying delete events.
func ApplyDelete(h ApplyDeleteHandler) Option {
return OptionFunc(func(hs *Handler) {
if hs.ApplyDelete != nil {
panic("res: multiple apply delete handlers")
}
hs.ApplyDelete = h
})
}
// Group sets a group ID. All resources of the same group will be handled on the
// same goroutine.
//
// The group may contain tags, ${tagName}, where the tag name matches a
// parameter placeholder name in the resource pattern.
func Group(group string) Option {
return OptionFunc(func(hs *Handler) {
hs.Group = group
})
}
// Parallel sets the parallel flag. All requests for the handler may be handled
// in parallel on different worker goroutines.
//
// If set to true, any value in Group will be ignored.
func Parallel(parallel bool) Option {
return OptionFunc(func(hs *Handler) {
hs.Parallel = parallel
})
}
// OnRegister sets a callback to be called when the handler is registered to a
// service.
//
// If a callback is already registered, the new callback will be called after
// the previous one.
func OnRegister(callback func(service *Service, pattern Pattern, rh Handler)) Option {
return OptionFunc(func(hs *Handler) {
if hs.OnRegister != nil {
prevcb := hs.OnRegister
hs.OnRegister = func(service *Service, pattern Pattern, rh Handler) {
prevcb(service, pattern, rh)
callback(service, pattern, rh)
}
} else {
hs.OnRegister = callback
}
})
}
// SetReset is an alias for SetOwnedResources.
//
// Deprecated: Renamed to SetOwnedResources to match API of similar libraries.
func (s *Service) SetReset(resources, access []string) *Service {
return s.SetOwnedResources(resources, access)
}
// SetOwnedResources sets the patterns which the service will handle requests
// for. The resources slice patterns ill be listened to for get, call, and auth
// requests. The access slice patterns will be listened to for access requests.
// These patterns will be used when a ResetAll is made.
//
// // Handle all requests for resources prefixed "library."
// service.SetOwnedResources([]string{"library.>"}, []string{"library.>"})
// // Handle access requests for any resource
// service.SetOwnedResources([]string{}, []string{">"})
// // Handle non-access requests for a subset of resources
// service.SetOwnedResources([]string{"library.book", "library.books.*"}, []string{})
//
// If set to nil (default), the service will default to set ownership of all
// resources prefixed with its own path if one was provided when creating the
// service (eg. "serviceName.>"), or to all resources if no name was provided.
// It will take resource ownership if it has at least one registered handler has
// a Get, Call, or Auth handler method not being nil. It will take access
// ownership if it has at least one registered handler with the Access method
// not being nil.
//
// For more details on system reset, see:
// https://github.com/resgateio/resgate/blob/master/docs/res-service-protocol.md#system-reset-event
func (s *Service) SetOwnedResources(resources, access []string) *Service {
s.resetResources = resources
s.resetAccess = access
return s
}
// ListenAndServe connects to the NATS server at the url. Once connected, it
// subscribes to incoming requests. For each request, it calls the appropriate
// handler, or replies with the appropriate error if no handler is available.
//
// In case of disconnect, it will try to reconnect until Close is called, or
// until successfully reconnecting, upon which Reset will be called.
//
// ListenAndServe returns an error if failes to connect or subscribe. Otherwise,
// nil is returned once the connection is closed using Close.
func (s *Service) ListenAndServe(url string, options ...nats.Option) error {
if !atomic.CompareAndSwapInt32(&s.state, stateStopped, stateStarting) {
return errNotStopped
}
opts := []nats.Option{
nats.MaxReconnects(-1),
nats.ReconnectHandler(s.handleReconnect),
nats.DisconnectHandler(s.handleDisconnect),
nats.ClosedHandler(s.handleClosed),
}
if s.Mux.path != "" {
opts = append(opts, nats.Name(s.Mux.path))
}
opts = append(opts, options...)
s.infof("Connecting to NATS server")
nc, err := nats.Connect(url, opts...)
if err != nil {
s.errorf("Failed to connect to NATS server: %s", err)
return err
}
return s.serve(nc)
}
// Serve starts serving incoming requests received on the connection conn. For
// each request, it calls the appropriate handler, or replies with the
// appropriate error if no handler is available.
//
// If the connection conn is of type *nats.Conn, Service will call
// SetReconnectHandler, SetDisconnectHandler, and SetClosedHandler, replacing
// any existing event handlers.
//
// In case of disconnect, it will try to reconnect until Close is called, or
// until successfully reconnecting, upon which Reset will be called.
//
// Serve returns an error if failes to subscribe. Otherwise, nil is returned
// once the connection is closed.
func (s *Service) Serve(conn Conn) error {
if !atomic.CompareAndSwapInt32(&s.state, stateStopped, stateStarting) {
return errNotStopped
}
if nc, ok := conn.(*nats.Conn); ok {
nc.SetReconnectHandler(s.handleReconnect)
nc.SetDisconnectHandler(s.handleDisconnect)
nc.SetClosedHandler(s.handleClosed)
}
return s.serve(conn)
}
func (s *Service) serve(nc Conn) error {
s.infof("Starting service")
// Validate that there are resources registered
// for all the event listeners.
err := s.ValidateListeners()
if err != nil {
return err
}
// Initialize fields
inCh := make(chan *nats.Msg, s.inChannelSize)
workCh := make(chan *work, 1)
s.nc = nc
s.inCh = inCh
s.workcond = sync.Cond{L: &s.mu}
s.workbuf = make([]*work, s.inChannelSize)
s.workqueue = s.workbuf[:0]
s.rwork = make(map[string]*work, s.inChannelSize)
s.queryTQ = timerqueue.New(s.queryEventExpire, s.queryDuration)
// Start workers
s.wg.Add(s.workerCount)
for i := 0; i < s.workerCount; i++ {
go s.startWorker()
}
atomic.StoreInt32(&s.state, stateStarted)
err = s.subscribe()
if err != nil {
s.errorf("Failed to subscribe: %s", err)
go s.Shutdown()
} else {
// Send a system.reset
s.ResetAll()
// Call onServe callback
if s.onServe != nil {
s.onServe(s)
}
s.infof("Listening for requests")
s.startListener(inCh)
}
// Stop all workers by closing worker channel
close(workCh)
// Wait for all workers to be done
s.wg.Wait()
return nil
}
// Shutdown closes any existing connection to NATS Server.
// Returns an error if service is not started.
func (s *Service) Shutdown() error {
if !atomic.CompareAndSwapInt32(&s.state, stateStarted, stateStopping) {
return errNotStarted
}
s.infof("Stopping service...")
s.close()
// Wait for all workers to be done
s.wg.Wait()
s.inCh = nil
s.nc = nil
atomic.StoreInt32(&s.state, stateStopped)
s.infof("Stopped")
return nil
}
// close calls Close on the NATS connection, and closes the incoming channel
func (s *Service) close() {
s.mu.Lock()
s.workqueue = nil
s.mu.Unlock()
s.workcond.Broadcast()
s.nc.Close()
close(s.inCh)
}
// Reset sends a system reset for the provided resource patterns.
func (s *Service) Reset(resources []string, access []string) {
if atomic.LoadInt32(&s.state) != stateStarted {
s.errorf("Failed to reset: service not started")
return
}
s.reset(resources, access)
}
func (s *Service) reset(resources []string, access []string) {
lr := len(resources)
la := len(access)
// Quick escape
if lr == 0 && la == 0 {
return
}
if lr == 0 {
resources = nil
}
if la == 0 {
access = nil
}
s.event("system.reset", resetEvent{
Resources: resources,
Access: access,
})
}
// ResetAll will send a system.reset to trigger any gateway to update their
// cache for all resources handled by the service.
//
// The method is automatically called on server start and reconnects.
func (s *Service) ResetAll() {
if atomic.LoadInt32(&s.state) != stateStarted {
s.errorf("Failed to reset: service not started")
return
}
s.setDefaultOwnership()
s.reset(s.resetResources, s.resetAccess)
}
// TokenEvent sends a connection token event that sets the connection's access
// token, discarding any previously set token.
//
// A change of token will invalidate any previous access response received using
// the old token.
//
// A nil token clears any previously set token.
func (s *Service) TokenEvent(cid string, token interface{}) {
if atomic.LoadInt32(&s.state) != stateStarted {
s.errorf("Failed to send token event: service not started")
return
}
if !isValidPart(cid) {
panic(`res: invalid connection ID`)
}
s.event("conn."+cid+".token", tokenEvent{Token: token})
}
// TokenEventWithID sends a connection token event in the same way as
// TokenEvent, but includes a token ID (tid).
//
// The token ID is a string that identifies the token, used when calling
// TokenReset to update or clear a token.
func (s *Service) TokenEventWithID(cid string, tokenID string, token interface{}) {
if atomic.LoadInt32(&s.state) != stateStarted {
s.errorf("Failed to send token event: service not started")
return
}
if !isValidPart(cid) {
panic(`res: invalid connection ID`)
}
s.event("conn."+cid+".token", tokenEvent{Token: token, TID: tokenID})
}
// TokenReset sends a token reset event for the provided token IDs.
//
// The subject string is a message subject that will receive auth requests for
// any connections with a token matching any of the token IDs.
func (s *Service) TokenReset(subject string, tokenID ...string) {
if atomic.LoadInt32(&s.state) != stateStarted {
s.errorf("Failed to send token reset event: service not started")
return
}
if subject == "" || !isValidPath(subject) {
panic(`res: invalid token reset subject`)
}
// Don't send events without token ID
if len(tokenID) == 0 {
return
}
s.event("system.tokenReset", tokenResetEvent{
TIDs: tokenID,
Subject: subject,
})
}
func (s *Service) setDefaultOwnership() {
if s.resetResources == nil {
if s.Contains(func(h Handler) bool {
return h.Get != nil || len(h.Call) > 0 || len(h.Auth) > 0 || h.New != nil
}) {
s.resetResources = []string{s.Mux.path, mergePattern(s.Mux.path, ">")}
} else {
s.resetResources = []string{}
}
}
if s.resetAccess == nil {
if s.Contains(func(h Handler) bool {
return h.Access != nil
}) {
s.resetAccess = []string{s.Mux.path, mergePattern(s.Mux.path, ">")}
} else {
s.resetAccess = []string{}
}
}
}
// subscribe makes a nats subscription for each required request type, based on
// the patterns used for ResetAll.
func (s *Service) subscribe() error {
var err error
s.setDefaultOwnership()
if len(s.resetResources) == 0 && len(s.resetAccess) == 0 {
return errors.New("res: no resources to serve")
}
var patterns []string
for _, t := range []string{RequestTypeGet, RequestTypeCall, RequestTypeAuth} {
for _, p := range s.resetResources {
pattern := t + "." + p
if pattern[len(pattern)-1] != '>' && t != RequestTypeGet {
pattern += ".*"
}
patterns = append(patterns, pattern)
}
}
for _, p := range s.resetAccess {
pattern := "access." + p
s.tracef("sub %s", pattern)
if s.queueGroup == "" {
_, err = s.nc.ChanSubscribe(pattern, s.inCh)
} else {
_, err = s.nc.ChanQueueSubscribe(pattern, s.queueGroup, s.inCh)
}
if err != nil {
return err
}
}
next:
for i, pattern := range patterns {
// Skip patterns that overlap one another
for j, mpattern := range patterns {
if i != j && Pattern(mpattern).Matches(pattern) {
continue next
}
}
s.tracef("sub %s", pattern)
if s.queueGroup == "" {
_, err = s.nc.ChanSubscribe(pattern, s.inCh)
} else {
_, err = s.nc.ChanQueueSubscribe(pattern, s.queueGroup, s.inCh)
}
if err != nil {
return err
}
}
return nil
}
// startListener listens for nats messages and passes them on to a worker.
func (s *Service) startListener(ch chan *nats.Msg) {
for m := range ch {
s.handleRequest(m)
}
}
// handleRequest is called by the nats listener on incoming messages.
func (s *Service) handleRequest(m *nats.Msg) {
subj := m.Subject
s.tracef("==> %s: %s", subj, m.Data)
// Assert there is a reply subject
if m.Reply == "" {
s.errorf("Missing reply subject on request: %s", subj)
return
}
// Get request type
idx := strings.IndexByte(subj, '.')
if idx < 0 {
// Shouldn't be possible unless NATS is really acting up
s.errorf("Invalid request subject: %s", subj)
return
}
var method string
rtype := subj[:idx]
rname := subj[idx+1:]
if rtype == "call" || rtype == "auth" {
idx = strings.LastIndexByte(rname, '.')
if idx < 0 {
// No method? Resgate must be acting up
s.errorf("Invalid request subject: %s", subj)
return
}
method = rname[idx+1:]
rname = rname[:idx]
}
group := rname
mh := s.GetHandler(rname)
if mh != nil {
group = mh.Group
}
s.runWith(group, func() {
s.processRequest(m, rtype, rname, method, mh)
})
}
// runWith enqueues the callback, cb, to be called by the worker goroutine
// defined by the worker ID (wid).
func (s *Service) runWith(wid string, cb func()) {
if atomic.LoadInt32(&s.state) != stateStarted {
return
}
s.mu.Lock()
// Get current work queue for the resource
var w *work
var ok bool
if wid != "" {
w, ok = s.rwork[wid]
}
if !ok {
// Create a new work queue and pass it to a worker
w = &work{
s: s,
wid: wid,
single: [1]func(){cb},
}
w.queue = w.single[:1]
if wid != "" {
s.rwork[wid] = w
}
s.workqueue = append(s.workqueue, w)
s.mu.Unlock()
s.workcond.Signal()