diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterConstants.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterConstants.java new file mode 100644 index 00000000..0183ab72 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterConstants.java @@ -0,0 +1,44 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public final class ClusterConstants { + + public static final int MSG_TYPE_PING = 0; + public static final int MSG_TYPE_FLOW = 1; + public static final int MSG_TYPE_PARAM_FLOW = 2; + + public static final int RESPONSE_STATUS_BAD = -1; + public static final int RESPONSE_STATUS_OK = 0; + + public static final int PARAM_TYPE_INTEGER = 0; + public static final int PARAM_TYPE_LONG = 1; + public static final int PARAM_TYPE_BYTE = 2; + public static final int PARAM_TYPE_DOUBLE = 3; + public static final int PARAM_TYPE_FLOAT = 4; + public static final int PARAM_TYPE_SHORT = 5; + public static final int PARAM_TYPE_BOOLEAN = 6; + public static final int PARAM_TYPE_STRING = 7; + + public static final int DEFAULT_CLUSTER_SERVER_PORT = 8730; + public static final int DEFAULT_REQUEST_TIMEOUT = 20; + + private ClusterConstants() {} +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterErrorMessages.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterErrorMessages.java new file mode 100644 index 00000000..3e6ad416 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterErrorMessages.java @@ -0,0 +1,32 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster; + +/** + * @author jialiang.ljl + * @since 1.4.0 + */ +public final class ClusterErrorMessages { + + public static final String BAD_REQUEST = "bad request"; + public static final String UNEXPECTED_STATUS = "unexpected status"; + public static final String TOO_MANY_REQUESTS = "too many requests (client side)"; + public static final String REQUEST_TIME_OUT = "request time out"; + public static final String CLIENT_NOT_READY = "client not ready (not running or initializing)"; + public static final String NO_RULES_IN_SERVER = "no rules in token server"; + + private ClusterErrorMessages() {} +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterTransportClient.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterTransportClient.java new file mode 100644 index 00000000..26af3c20 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/ClusterTransportClient.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster; + +import com.alibaba.csp.sentinel.cluster.request.ClusterRequest; +import com.alibaba.csp.sentinel.cluster.response.ClusterResponse; + +/** + * Synchronous transport client for distributed flow control. + * + * @author Eric Zhao + * @since 1.4.0 + */ +public interface ClusterTransportClient { + + /** + * Send request to remote server and get response. + * + * @param request Sentinel cluster request + * @return response from remote server + * @throws Exception some error occurs + */ + ClusterResponse sendRequest(ClusterRequest request) throws Exception; +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityDecoder.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityDecoder.java new file mode 100644 index 00000000..1bb830ac --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityDecoder.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec; + +/** + * @param source stream type + * @param target entity type + * @author Eric Zhao + * @since 1.4.0 + */ +public interface EntityDecoder { + + /** + * Decode target object from source stream. + * + * @param source source stream + * @return decoded target object + */ + T decode(S source); +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityWriter.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityWriter.java new file mode 100644 index 00000000..03dfcbaf --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/EntityWriter.java @@ -0,0 +1,35 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec; + +/** + * A universal interface for publishing entities to a target stream. + * + * @param entity type + * @param target stream type + * @author Eric Zhao + * @since 1.4.0 + */ +public interface EntityWriter { + + /** + * Write the provided entity to target stream. + * + * @param entity entity to publish + * @param target the target stream + */ + void writeTo(E entity, T target); +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityDecoder.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityDecoder.java new file mode 100644 index 00000000..8b9fd33f --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityDecoder.java @@ -0,0 +1,27 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec.request; + +import com.alibaba.csp.sentinel.cluster.codec.EntityDecoder; +import com.alibaba.csp.sentinel.cluster.request.Request; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public interface RequestEntityDecoder extends EntityDecoder { + +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityWriter.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityWriter.java new file mode 100644 index 00000000..814bbed1 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/request/RequestEntityWriter.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec.request; + +import com.alibaba.csp.sentinel.cluster.codec.EntityWriter; +import com.alibaba.csp.sentinel.cluster.request.Request; + +/** + * A universal {@link EntityWriter} interface for publishing {@link Request} to a target stream. + * + * @author Eric Zhao + * @since 1.4.0 + */ +public interface RequestEntityWriter extends EntityWriter { + +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityDecoder.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityDecoder.java new file mode 100644 index 00000000..6bb03ae1 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityDecoder.java @@ -0,0 +1,27 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec.response; + +import com.alibaba.csp.sentinel.cluster.codec.EntityDecoder; +import com.alibaba.csp.sentinel.cluster.response.Response; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public interface ResponseEntityDecoder extends EntityDecoder { + +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityWriter.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityWriter.java new file mode 100644 index 00000000..89e5625c --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/codec/response/ResponseEntityWriter.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.codec.response; + +import com.alibaba.csp.sentinel.cluster.codec.EntityWriter; +import com.alibaba.csp.sentinel.cluster.response.Response; + +/** + * A universal {@link EntityWriter} interface for publishing {@link Response} to a target stream. + * + * @author Eric Zhao + * @since 1.4.0 + */ +public interface ResponseEntityWriter extends EntityWriter { + +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/exception/SentinelClusterException.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/exception/SentinelClusterException.java new file mode 100644 index 00000000..ddc73095 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/exception/SentinelClusterException.java @@ -0,0 +1,32 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.exception; + +/** + * @author jialiang.ljl + * @since 1.4.0 + */ +public class SentinelClusterException extends Exception { + + public SentinelClusterException(String errorMsg) { + super(errorMsg); + } + + @Override + public Throwable fillInStackTrace() { + return this; + } +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/ClusterRequest.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/ClusterRequest.java new file mode 100644 index 00000000..2e513e41 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/ClusterRequest.java @@ -0,0 +1,79 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.request; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public class ClusterRequest implements Request { + + private int id; + private int type; + + private T data; + + public ClusterRequest() {} + + public ClusterRequest(int id, int type, T data) { + this.id = id; + this.type = type; + this.data = data; + } + + public ClusterRequest(int type, T data) { + this.type = type; + this.data = data; + } + + @Override + public int getId() { + return id; + } + + public ClusterRequest setId(int id) { + this.id = id; + return this; + } + + @Override + public int getType() { + return type; + } + + public ClusterRequest setType(int type) { + this.type = type; + return this; + } + + public T getData() { + return data; + } + + public ClusterRequest setData(T data) { + this.data = data; + return this; + } + + @Override + public String toString() { + return "ClusterRequest{" + + "id=" + id + + ", type=" + type + + ", data=" + data + + '}'; + } +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/Request.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/Request.java new file mode 100644 index 00000000..cda5b9c0 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/Request.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.request; + +/** + * Cluster transport request interface. + * + * @author Eric Zhao + * @since 1.4.0 + */ +public interface Request { + + /** + * Get request type. + * + * @return request type + */ + int getType(); + + /** + * Get request ID. + * + * @return unique request ID + */ + int getId(); +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/FlowRequestData.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/FlowRequestData.java new file mode 100644 index 00000000..ab401983 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/FlowRequestData.java @@ -0,0 +1,63 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.request.data; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public class FlowRequestData { + + private long flowId; + private int count; + private boolean priority; + + public long getFlowId() { + return flowId; + } + + public FlowRequestData setFlowId(long flowId) { + this.flowId = flowId; + return this; + } + + public int getCount() { + return count; + } + + public FlowRequestData setCount(int count) { + this.count = count; + return this; + } + + public boolean isPriority() { + return priority; + } + + public FlowRequestData setPriority(boolean priority) { + this.priority = priority; + return this; + } + + @Override + public String toString() { + return "FlowRequestData{" + + "flowId=" + flowId + + ", count=" + count + + ", priority=" + priority + + '}'; + } +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/ParamFlowRequestData.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/ParamFlowRequestData.java new file mode 100644 index 00000000..c6ac6ec4 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/request/data/ParamFlowRequestData.java @@ -0,0 +1,65 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.request.data; + +import java.util.Collection; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public class ParamFlowRequestData { + + private long flowId; + private int count; + private Collection params; + + public long getFlowId() { + return flowId; + } + + public ParamFlowRequestData setFlowId(long flowId) { + this.flowId = flowId; + return this; + } + + public int getCount() { + return count; + } + + public ParamFlowRequestData setCount(int count) { + this.count = count; + return this; + } + + public Collection getParams() { + return params; + } + + public ParamFlowRequestData setParams(Collection params) { + this.params = params; + return this; + } + + @Override + public String toString() { + return "ParamFlowRequestData{" + + "flowId=" + flowId + + ", count=" + count + + ", params=" + params + + '}'; + } +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/ClusterResponse.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/ClusterResponse.java new file mode 100644 index 00000000..4f5ebf5b --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/ClusterResponse.java @@ -0,0 +1,87 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.response; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public class ClusterResponse implements Response { + + private int id; + private int type; + private int status; + + private T data; + + public ClusterResponse() {} + + public ClusterResponse(int id, int type, int status, T data) { + this.id = id; + this.type = type; + this.status = status; + this.data = data; + } + + @Override + public int getId() { + return id; + } + + public ClusterResponse setId(int id) { + this.id = id; + return this; + } + + @Override + public int getType() { + return type; + } + + public ClusterResponse setType(int type) { + this.type = type; + return this; + } + + @Override + public int getStatus() { + return status; + } + + public ClusterResponse setStatus(int status) { + this.status = status; + return this; + } + + public T getData() { + return data; + } + + public ClusterResponse setData(T data) { + this.data = data; + return this; + } + + @Override + public String toString() { + return "ClusterResponse{" + + "id=" + id + + ", type=" + type + + ", status=" + status + + ", data=" + data + + '}'; + } +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/Response.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/Response.java new file mode 100644 index 00000000..037088d9 --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/Response.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.response; + +/** + * Cluster transport response interface. + * + * @author Eric Zhao + * @since 1.4.0 + */ +public interface Response { + + /** + * Get response ID. + * + * @return response ID + */ + int getId(); + + /** + * Get response type. + * + * @return response type + */ + int getType(); + + /** + * Get response status. + * + * @return response status + */ + int getStatus(); +} diff --git a/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/data/FlowTokenResponseData.java b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/data/FlowTokenResponseData.java new file mode 100644 index 00000000..2ca2842c --- /dev/null +++ b/sentinel-cluster/sentinel-cluster-common-default/src/main/java/com/alibaba/csp/sentinel/cluster/response/data/FlowTokenResponseData.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.cluster.response.data; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public class FlowTokenResponseData { + + private int remainingCount; + private int waitInMs; + + public int getRemainingCount() { + return remainingCount; + } + + public FlowTokenResponseData setRemainingCount(int remainingCount) { + this.remainingCount = remainingCount; + return this; + } + + public int getWaitInMs() { + return waitInMs; + } + + public FlowTokenResponseData setWaitInMs(int waitInMs) { + this.waitInMs = waitInMs; + return this; + } + + @Override + public String toString() { + return "FlowTokenResponseData{" + + "remainingCount=" + remainingCount + + ", waitInMs=" + waitInMs + + '}'; + } +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java new file mode 100644 index 00000000..b057fc55 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -0,0 +1,49 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.util; + +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +public final class SpiLoader { + + private static final Map SERVICE_LOADER_MAP = new ConcurrentHashMap(); + + public static T loadFirstInstance(Class clazz) { + String key = clazz.getName(); + // Not thread-safe, as it's expected to be resolved in a thread-safe context. + ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); + if (serviceLoader == null) { + serviceLoader = ServiceLoader.load(clazz); + SERVICE_LOADER_MAP.put(key, serviceLoader); + } + + Iterator iterator = serviceLoader.iterator(); + if (iterator.hasNext()) { + return iterator.next(); + } else { + return null; + } + } + + private SpiLoader() {} +}