Conditions | 14 |
Total Lines | 126 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like balancer.*Balancer.UpdateClientConnState often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package balancer |
||
69 | func (b *Balancer) UpdateClientConnState(s balancer.ClientConnState) error { |
||
70 | // Log the new ClientConn state. |
||
71 | slog.Info("Received new ClientConn state", |
||
72 | slog.Any("state", s), |
||
73 | ) |
||
74 | |||
75 | // Reset any existing resolver error. |
||
76 | b.lastResolverError = nil |
||
77 | |||
78 | // Handle changes to the balancer configuration. |
||
79 | if s.BalancerConfig != nil { |
||
80 | svcConfig := s.BalancerConfig.(*Config) |
||
81 | if b.config == nil || svcConfig.ReplicationFactor != b.config.ReplicationFactor { |
||
82 | slog.Info("Updating consistent hashing configuration", |
||
83 | slog.Int("partition_count", svcConfig.PartitionCount), |
||
84 | slog.Int("replication_factor", svcConfig.ReplicationFactor), |
||
85 | slog.Float64("load", svcConfig.Load), |
||
86 | slog.Int("picker_width", svcConfig.PickerWidth), |
||
87 | ) |
||
88 | b.consistent = consistent.New(consistent.Config{ |
||
89 | PartitionCount: svcConfig.PartitionCount, |
||
90 | ReplicationFactor: svcConfig.ReplicationFactor, |
||
91 | Load: svcConfig.Load, |
||
92 | PickerWidth: svcConfig.PickerWidth, |
||
93 | Hasher: b.hasher, |
||
94 | }) |
||
95 | b.config = svcConfig |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // Check if the consistent hashing configuration exists. |
||
100 | if b.consistent == nil { |
||
101 | slog.Error("No consistent hashing configuration found") |
||
102 | b.picker = base.NewErrPicker(errors.Join(b.lastConnectionError, b.lastResolverError)) |
||
103 | b.clientConn.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker}) |
||
104 | return fmt.Errorf("no consistent hashing configuration found") |
||
105 | } |
||
106 | |||
107 | // Maintain a set of addresses provided by the resolver. |
||
108 | addrsSet := resolver.NewAddressMap() |
||
109 | for _, addr := range s.ResolverState.Addresses { |
||
110 | addrsSet.Set(addr, nil) |
||
111 | |||
112 | // Add new SubConns for addresses that are not already tracked. |
||
113 | if _, ok := b.addressSubConns.Get(addr); !ok { |
||
114 | sc, err := b.clientConn.NewSubConn([]resolver.Address{addr}, balancer.NewSubConnOptions{HealthCheckEnabled: false}) |
||
115 | if err != nil { |
||
116 | slog.Warn("Failed to create new SubConn", |
||
117 | slog.String("address", addr.Addr), |
||
118 | slog.String("server_name", addr.ServerName), |
||
119 | slog.String("error", err.Error()), |
||
120 | ) |
||
121 | continue |
||
122 | } |
||
123 | |||
124 | b.addressSubConns.Set(addr, sc) |
||
125 | b.subConnStates[sc] = connectivity.Idle |
||
126 | b.connectivityEvaluator.RecordTransition(connectivity.Shutdown, connectivity.Idle) |
||
127 | sc.Connect() |
||
128 | |||
129 | b.consistent.Add(ConsistentMember{ |
||
130 | SubConn: sc, |
||
131 | name: fmt.Sprintf("%s|%s", addr.ServerName, addr.Addr), |
||
132 | }) |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // Remove SubConns that are no longer part of the resolved addresses. |
||
137 | for _, addr := range b.addressSubConns.Keys() { |
||
138 | sci, _ := b.addressSubConns.Get(addr) |
||
139 | sc := sci.(balancer.SubConn) |
||
140 | if _, ok := addrsSet.Get(addr); !ok { |
||
141 | slog.Info("Removing SubConn", |
||
142 | slog.String("address", addr.Addr), |
||
143 | slog.String("server_name", addr.ServerName), |
||
144 | ) |
||
145 | b.clientConn.RemoveSubConn(sc) |
||
146 | b.addressSubConns.Delete(addr) |
||
147 | b.consistent.Remove(ConsistentMember{ |
||
148 | SubConn: sc, |
||
149 | name: fmt.Sprintf("%s|%s", addr.ServerName, addr.Addr), |
||
150 | }.String()) |
||
151 | } |
||
152 | } |
||
153 | |||
154 | // Log the current members in the consistent hashing ring. |
||
155 | slog.Info("Current consistent members", |
||
156 | slog.Int("member_count", len(b.consistent.Members())), |
||
157 | ) |
||
158 | for _, m := range b.consistent.Members() { |
||
159 | slog.Info("Consistent member", slog.String("member", m.String())) |
||
160 | } |
||
161 | |||
162 | // Handle the case where the resolver produces zero addresses. |
||
163 | if len(s.ResolverState.Addresses) == 0 { |
||
164 | err := errors.New("resolver produced zero addresses") |
||
165 | b.ResolverError(err) |
||
166 | slog.Error("Resolver produced zero addresses") |
||
167 | return balancer.ErrBadResolverState |
||
168 | } |
||
169 | |||
170 | // Update the picker based on the current balancer state. |
||
171 | if b.state == connectivity.TransientFailure { |
||
172 | slog.Warn("Transient failure detected, using error picker") |
||
173 | b.picker = base.NewErrPicker(errors.Join(b.lastConnectionError, b.lastResolverError)) |
||
174 | } else { |
||
175 | width := b.config.PickerWidth |
||
176 | if width < 1 { |
||
177 | width = 1 |
||
178 | } |
||
179 | slog.Info("Creating new picker", |
||
180 | slog.Int("width", width), |
||
181 | ) |
||
182 | b.picker = &picker{ |
||
183 | consistent: b.consistent, |
||
184 | width: width, |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // Update the ClientConn state with the new picker. |
||
189 | slog.Info("Updating ClientConn state", |
||
190 | slog.String("connectivity_state", b.state.String()), |
||
191 | ) |
||
192 | b.clientConn.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker}) |
||
193 | |||
194 | return nil |
||
195 | } |
||
251 |