@@ -73,39 +73,13 @@ type adsStreamEventHandler interface {
73
73
onResponse (response , func ()) ([]string , error ) // Called when a response is received on the ADS stream.
74
74
}
75
75
76
- // watchState is a enum that describes the watch state of a particular
77
- // resource.
78
- type watchState int
79
-
80
- const (
81
- // resourceWatchStateStarted is the state where a watch for a resource was
82
- // started, but a request asking for that resource is yet to be sent to the
83
- // management server.
84
- resourceWatchStateStarted watchState = iota
85
- // resourceWatchStateRequested is the state when a request has been sent for
86
- // the resource being watched.
87
- resourceWatchStateRequested
88
- // ResourceWatchStateReceived is the state when a response has been received
89
- // for the resource being watched.
90
- resourceWatchStateReceived
91
- // resourceWatchStateTimeout is the state when the watch timer associated
92
- // with the resource expired because no response was received.
93
- resourceWatchStateTimeout
94
- )
95
-
96
- // resourceWatchState is the state corresponding to a resource being watched.
97
- type resourceWatchState struct {
98
- State watchState // Watch state of the resource.
99
- ExpiryTimer * time.Timer // Timer for the expiry of the watch.
100
- }
101
-
102
76
// state corresponding to a resource type.
103
77
type resourceTypeState struct {
104
- version string // Last acked version. Should not be reset when the stream breaks.
105
- nonce string // Last received nonce. Should be reset when the stream breaks.
106
- bufferedRequests chan struct {} // Channel to buffer requests when writing is blocked.
107
- subscribedResources map [string ]* resourceWatchState // Map of subscribed resource names to their state.
108
- pendingWrite bool // True if there is a pending write for this resource type.
78
+ version string // Last acked version. Should not be reset when the stream breaks.
79
+ nonce string // Last received nonce. Should be reset when the stream breaks.
80
+ bufferedRequests chan struct {} // Channel to buffer requests when writing is blocked.
81
+ subscribedResources map [string ]* xdsresource. ResourceWatchState // Map of subscribed resource names to their state.
82
+ pendingWrite bool // True if there is a pending write for this resource type.
109
83
}
110
84
111
85
// adsStreamImpl provides the functionality associated with an ADS (Aggregated
@@ -198,15 +172,15 @@ func (s *adsStreamImpl) subscribe(typ ResourceType, name string) {
198
172
// An entry in the type state map is created as part of the first
199
173
// subscription request for this type.
200
174
state = & resourceTypeState {
201
- subscribedResources : make (map [string ]* resourceWatchState ),
175
+ subscribedResources : make (map [string ]* xdsresource. ResourceWatchState ),
202
176
bufferedRequests : make (chan struct {}, 1 ),
203
177
}
204
178
s .resourceTypeState [typ ] = state
205
179
}
206
180
207
181
// Create state for the newly subscribed resource. The watch timer will
208
182
// be started when a request for this resource is actually sent out.
209
- state .subscribedResources [name ] = & resourceWatchState {State : resourceWatchStateStarted }
183
+ state .subscribedResources [name ] = & xdsresource. ResourceWatchState {State : xdsresource . ResourceWatchStateStarted }
210
184
state .pendingWrite = true
211
185
212
186
// Send a request for the resource type with updated subscriptions.
@@ -616,8 +590,8 @@ func (s *adsStreamImpl) onRecv(stream clients.Stream, names []string, url, versi
616
590
s .logger .Warningf ("ADS stream received a response for resource %q, but no state exists for it" , name )
617
591
continue
618
592
}
619
- if ws := rs .State ; ws == resourceWatchStateStarted || ws == resourceWatchStateRequested {
620
- rs .State = resourceWatchStateReceived
593
+ if ws := rs .State ; ws == xdsresource . ResourceWatchStateStarted || ws == xdsresource . ResourceWatchStateRequested {
594
+ rs .State = xdsresource . ResourceWatchStateReceived
621
595
if rs .ExpiryTimer != nil {
622
596
rs .ExpiryTimer .Stop ()
623
597
rs .ExpiryTimer = nil
@@ -652,14 +626,14 @@ func (s *adsStreamImpl) onError(err error, msgReceived bool) {
652
626
s .mu .Lock ()
653
627
for _ , state := range s .resourceTypeState {
654
628
for _ , rs := range state .subscribedResources {
655
- if rs .State != resourceWatchStateRequested {
629
+ if rs .State != xdsresource . ResourceWatchStateRequested {
656
630
continue
657
631
}
658
632
if rs .ExpiryTimer != nil {
659
633
rs .ExpiryTimer .Stop ()
660
634
rs .ExpiryTimer = nil
661
635
}
662
- rs .State = resourceWatchStateStarted
636
+ rs .State = xdsresource . ResourceWatchStateStarted
663
637
}
664
638
}
665
639
s .mu .Unlock ()
@@ -691,23 +665,38 @@ func (s *adsStreamImpl) startWatchTimersLocked(typ ResourceType, names []string)
691
665
if ! ok {
692
666
continue
693
667
}
694
- if resourceState .State != resourceWatchStateStarted {
668
+ if resourceState .State != xdsresource . ResourceWatchStateStarted {
695
669
continue
696
670
}
697
- resourceState .State = resourceWatchStateRequested
671
+ resourceState .State = xdsresource . ResourceWatchStateRequested
698
672
699
673
rs := resourceState
700
674
resourceState .ExpiryTimer = time .AfterFunc (s .watchExpiryTimeout , func () {
701
675
s .mu .Lock ()
702
- rs .State = resourceWatchStateTimeout
676
+ rs .State = xdsresource . ResourceWatchStateTimeout
703
677
rs .ExpiryTimer = nil
704
678
s .mu .Unlock ()
705
679
s .eventHandler .onWatchExpiry (typ , name )
706
680
})
707
681
}
708
682
}
709
683
710
- func resourceNames (m map [string ]* resourceWatchState ) []string {
684
+ func (s * adsStreamImpl ) adsResourceWatchStateForTesting (rType ResourceType , resourceName string ) (xdsresource.ResourceWatchState , error ) {
685
+ s .mu .Lock ()
686
+ defer s .mu .Unlock ()
687
+
688
+ state , ok := s .resourceTypeState [rType ]
689
+ if ! ok {
690
+ return xdsresource.ResourceWatchState {}, fmt .Errorf ("unknown resource type: %v" , rType )
691
+ }
692
+ resourceState , ok := state .subscribedResources [resourceName ]
693
+ if ! ok {
694
+ return xdsresource.ResourceWatchState {}, fmt .Errorf ("unknown resource name: %v" , resourceName )
695
+ }
696
+ return * resourceState , nil
697
+ }
698
+
699
+ func resourceNames (m map [string ]* xdsresource.ResourceWatchState ) []string {
711
700
ret := make ([]string , len (m ))
712
701
idx := 0
713
702
for name := range m {
0 commit comments