1515
1616package org .sqlflow .client .impl ;
1717
18- import com .google .common .annotations .VisibleForTesting ;
1918import io .grpc .ManagedChannel ;
2019import io .grpc .ManagedChannelBuilder ;
2120import io .grpc .StatusRuntimeException ;
@@ -32,14 +31,8 @@ public class SQLFlowImpl implements SQLFlow {
3231 private ManagedChannel channel ;
3332 private SQLFlowGrpc .SQLFlowBlockingStub blockingStub ;
3433
35- public void init (String serverUrl ) {
36- this .channel = ManagedChannelBuilder .forTarget (serverUrl ).usePlaintext ().build ();
37- blockingStub = SQLFlowGrpc .newBlockingStub (channel );
38- }
39-
40- @ VisibleForTesting
41- public SQLFlowImpl (ManagedChannel channel ) {
42- this .channel = channel ;
34+ private SQLFlowImpl (Builder builder ) {
35+ this .channel = builder .channel ;
4336 blockingStub = SQLFlowGrpc .newBlockingStub (channel );
4437 }
4538
@@ -80,4 +73,31 @@ public void release() throws InterruptedException {
8073 throw e ;
8174 }
8275 }
76+
77+ public static class Builder {
78+ private ManagedChannel channel ;
79+
80+ public static Builder newInstance () {
81+ return new Builder ();
82+ }
83+
84+ public Builder withChannel (ManagedChannel channel ) {
85+ this .channel = channel ;
86+ return this ;
87+ }
88+
89+ /**
90+ * Open a channel to the SQLFlow server. The serverUrl argument always ends with a port.
91+ *
92+ * @param serverUrl an address the SQLFlow server exposed.
93+ * <p>Example: "localhost:50051"
94+ */
95+ public Builder forTarget (String serverUrl ) {
96+ return withChannel (ManagedChannelBuilder .forTarget (serverUrl ).usePlaintext ().build ());
97+ }
98+
99+ public SQLFlow build () {
100+ return new SQLFlowImpl (this );
101+ }
102+ }
83103}
0 commit comments